mardi 27 octobre 2015

Selenium WebDriver ignoring the conditional logic written in JAVA

This is a simple sign-in script which I am running in Firefox. What I am trying to do is to print a message if the sign-in is successful or fails. I gave wrong user/pass to get the error (verified it against the site that it's correct).

The page-title element only displays if user was logged-in successfully. Similarly, the error-msg element only displays if user was not logged-in.

WebDriver driver = new FirefoxDriver();
    String baseUrl = "http://www.example.com";
    String actualTitle;

    // Launch Firefox and direct it to the Base URL
    driver.get(baseUrl);
    driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

    // Maximize the browser window
    driver.manage().window().maximize();

    // Get the actual value of the title
    actualTitle = driver.getTitle();


    driver.findElement(By.linkText("Signin")).click();

    WebElement email = driver.findElement(By.id("email"));
    email.click();
    email.sendKeys("abc@example.com");

    WebElement pass = driver.findElement(By.id("pass"));
    pass.click();
    pass.sendKeys("abc123456");

    driver.findElement(By.id("send2")).click();



    // Variables for Dashboard & Error Title
    WebElement dashboardTitle = driver.findElement(By.className("page-title"));
    WebElement loginFailed = driver.findElement(By.className("error-msg"));

    if(loginFailed.getText().contains("Invalid login or password."))
        System.out.println("Test passed but login failed.");
    else if(dashboardTitle.getText().contains("My Dashboard"))
        System.out.println("Logged in successfully.");
    else if(dashboardTitle.getText().contains("Account Information"))
            System.out.println("Logged in successfully.");
    else
        System.out.println("You're not logged-in.");

    System.out.println("The title of current page is: "+actualTitle);
    driver.close();
    driver.quit();

User/Pass are not correct so the 'IF' condition should run but in reality it's giving me error that other element (page-title) is not found.

If I remove the other condition (from IF statement) then the script is running without any issue.

I want the script to run and search for both elements and if it finds only one element (which it should be) then it should display it's result instead of giving me the error that other element is not found.

Aucun commentaire:

Enregistrer un commentaire