jeudi 25 juin 2015

Selenium Webdriver- How to verify if the URL accessed in Java?

The Scenario is in my Java code it need to check the URL is accessed without any issues. If any server issue or loading time out issue is there then it need to stop the code and need to say the message else it need to execute the next line.

Here is my Code:

public class OneReportsMasterScript {

    static Properties p = new Properties();
    String url = p.getProperty("url");
    private static Logger Log = Logger.getLogger(OneReportsMasterScript.class.getName());

    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, InterruptedException {
        WebDriver driver = new FirefoxDriver();
        Actions actions = new Actions(driver);
        DOMConfigurator.configure("src/log4j.xml");
        String url = OneReportsMasterScript.readXML("logindetails", "url");
        String username = OneReportsMasterScript.readXML("logindetails", "username");
        String password = OneReportsMasterScript.readXML("logindetails", "password");
        Log.info("Sign in to the OneReports website");
        try {
            driver.manage().window().maximize();
            driver.get(url);
        } catch (Exception e) {
            Reporter.log("network server is slow..check internet connection");
            Log.info("Unable to open the website");
            // currently server issue is there but it is not executing this line
            throw new Error("network server is slow..check internet connection");
        }
        Log.info("Enter Username");
        // it comes to this line and showing the error in console as 
        //**Unable to locate element: {"method":"id","selector":"loginUsername"}
        // Command duration or timeout: 55 milliseconds**
        driver.findElement(By.id("loginUsername")).sendKeys(username);
        Log.info("Enter Password");
        driver.findElement(By.id("loginPassword")).sendKeys(password);
        // submit
        Log.info("Submitting login details");
        waitforElement(driver, 120, "//*[@id='submit']");
        driver.findElement(By.id("submit")).submit();
        boolean submit = true;
        Thread.sleep(5000);
        if (submit != false) {
            System.out.println("Login sucessfully completed");

        } else {
            System.out.println("Login failed");
            System.exit(0);
        }
        Log.info("Clicking on Reports link");
        String id_r = OneReportsMasterScript.readXML("kpi", "id_r");
        WebElement menuHoverLink = driver.findElement(By.id(id_r));
        actions.moveToElement(menuHoverLink).perform();
        Thread.sleep(6000);

        String id_e = OneReportsMasterScript.readXML("kpi", "id_e");
        WebElement menuHoverLink1 = driver.findElement(By.id(id_e));
        actions.moveToElement(menuHoverLink1).perform();
        Thread.sleep(6000);

        String id_pr = OneReportsMasterScript.readXML("kpi", "id_pr");
        WebElement menuHoverLink2 = driver.findElement(By.id(id_pr));
        actions.moveToElement(menuHoverLink2).perform();
        Thread.sleep(6000);

        String id_1 = OneReportsMasterScript.readXML("prkpi", "id_1");
        WebElement menuHoverLink3 = driver.findElement(By.id(id_1));
        actions.moveToElement(menuHoverLink3).click().perform();
        Thread.sleep(6000);

    }

    private static void waitforElement(WebDriver driver, int i, String string) {

    }

    public static String readXML(String searchelement, String tag) throws SAXException, IOException, ParserConfigurationException {
        String ele = null;
        File fXmlFile = new File("D://NewFile.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName(searchelement);
        Node nNode = nList.item(0);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            ele = eElement.getElementsByTagName(tag).item(0).getTextContent();
        }
        return ele;
    }
}

Aucun commentaire:

Enregistrer un commentaire