1

I'm learning selenium and writing automation script for this website.

I'm trying to get value of the error message, I identified Error Message WebElement using xpath:

".//*[@id='ajax_loginform']/p[@class='response']"

I tried using the getText() and getAttribute("innerhtml") functions but both return null.

I am sure that the Error Message is identified because TestNG told me that.

By the way when I use following xpath:

".//*[@id='ajax_loginform']/p[contains(text(), 'ERROR')]"

the Error Message is not identified.

Here is my java test code:

public class LoginVerification {

private WebDriver driver;
private String baseURL = "http://store.demoqa.com/";

@DataProvider(name = "Login Data")
public static Object[][] loginData(){

    return new Object[][]{{"admin", "password"}, {"tester", "password"}};
}

@BeforeTest
public void configure(){
    driver = new FirefoxDriver();
    driver.get(baseURL);
}

//Verify that user cannot login with invalid account
@Test(dataProvider = "Login Data")
public void verifyUserCannotLoginWithInvalidAccount(String username, String password){
    //Click on My account link
    WebElement myAccountLink = driver.findElement(By.xpath(".//*[@id='account']/a"));
    myAccountLink.click();

    //input username, password
    WebElement usernameTextbox = driver.findElement(By.id("log"));
    usernameTextbox.sendKeys(username);

    WebElement passwordTextbox = driver.findElement(By.id("pwd"));
    passwordTextbox.sendKeys(password);

    //click login button
    WebElement loginButton = driver.findElement(By.id("login"));
    loginButton.click();

    //verify that "ERROR: Invalid login credentials." display
    WebElement errorMessage = driver.findElement(By.xpath(".//*[@id='ajax_loginform']/p[@class='response']"));
    String innerHTML = errorMessage.getAttribute("innerhtml");
    System.out.println(innerHTML);
    System.out.println(errorMessage.getText());
    Assert.assertEquals(errorMessage.getText(), "ERROR: Invalid login credentials.");
}

@AfterTest
public void cleanUp(){
    //driver.quit();
}
}

Here is my project.

  • If the problem is locating, try to use a simpler xpath. Try to minimize the number of other elements you need to rely on to locate the one you care about. This will strengthen your tests and the result will be less likely to break from peripheral changes to the page. You could also try using CSS selectors instead, they tend to be a little more elegant in my opinion. – Julian Nov 6 '15 at 22:50
  • CSS selectors are great. You can get it using Chrome and inspecting the element. Right-click the element and select Copy CSS Path. – kirbycope Nov 7 '15 at 2:15
  • I'm using firebug & firepath to locate WebElement and i find it useful – Nicky Ten Nov 7 '15 at 3:35
1

Your Xpath for the error appears incorrect. and should be

//*[@id="ajax_loginform"]/p[1]/strong

You will notice in the HTML the "Error" is within tags and not the

itself

<strong>ERROR</strong>

Your Assert will need to be updated accordingly

Assert.assertEquals(errorMessage.getText(), "ERROR", "Expected error message not returned, instead returned" + errorMessage.getText();

You can then add an additional assert to the remaining text if required. I have also added in an error message as this will be invaluable for debugging as your project expands and is generally good practice.

1

Use Explicit wait, Comment all your code after loginButton.click(); and add following 2 lines.

WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath(".//*[@id='ajax_loginform']/p[1]"), "ERROR: Invalid login credentials."));

Explicit wait will wait until your text condition appears, if your text does not appear within 30 sec it will throw an error

1

I solved the first problem by using explicitly Wait.

//wait until error message display
    expliciticWait.until(
            ExpectedConditions.visibilityOfElementLocated(
                    By.xpath(".//*[@id='ajax_loginform']/p[@class='response']")));

    //verify that "ERROR: Invalid login credentials." display
    WebElement errorMessage = driver.findElement(By.xpath(".//*[@id='ajax_loginform']/p[@class='response']"));
    String innerHTML = errorMessage.getAttribute("innerhtml");
    System.out.println(innerHTML);
    System.out.println(errorMessage.getText());
    Assert.assertEquals(errorMessage.getText(), "ERROR: Invalid login credentials.");

But the second problem is when I identify error message using following XPath expression still not solved.

".//*[@id='ajax_loginform']/p[contains(text(), 'ERROR']"

I would like to use this XPath expression because the web updates.

  • 2
    If you were able to solve the issue, can you accept your answer? If not, what's remaining? – demouser123 Nov 9 '15 at 3:22
  • There is still this problem:".//*[@id='ajax_loginform']/p[contains(text(), 'ERROR']" – Nicky Ten Nov 10 '15 at 3:31
  • explicit waits are a bad idea without a timeout. Also using xpath on a page that loads dynamically is a bad idea as it doesn't allways have an update representation of the page. – Amias Mar 2 '18 at 10:45
0

Can you try using the PageName method in following code:

public static void PageName() throws InterruptedException {
    String actualTitle = "Change Password";
    String title =Driver.dr.findElement(Xpath).getText();
    if (title.equals(actualTitle)) {
        System.out.println("Title is Matched");
    } else {
        System.out.println("Title is Mismatched.");
    }
    Thread.sleep(2000);
}
0
public class LoginVerification {

    private WebDriver driver;
    public WebDriverWait wait;
    private String baseURL = "http://store.demoqa.com/";

    @DataProvider(name = "Login Data")
    public static Object[][] loginData(){

        return new Object[][]{{"admin", "password"}, {"tester", "password"}};
    }

    @BeforeTest
    public void configure(){
        System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(baseURL);
    }

    //Verify that user cannot login with invalid account
    @Test(dataProvider = "Login Data")
    public void verifyUserCannotLoginWithInvalidAccount(String username, String password){
        //Click on My account link
        WebElement myAccountLink = driver.findElement(By.xpath(".//*[@id='account']/a"));
        myAccountLink.click();

        //input username, password
        WebElement usernameTextbox = driver.findElement(By.id("log"));
        usernameTextbox.sendKeys(username);

        WebElement passwordTextbox = driver.findElement(By.id("pwd"));
        passwordTextbox.sendKeys(password);

        WebElement checkbox =driver.findElement(By.name("rememberme"));
        boolean checkClick = checkbox.isSelected();
        if(!checkClick==true){

            checkbox.click();
            System.out.println("Clicked the checkbox");

        }else if(!checkClick==false){

            checkbox.click();
            System.out.println("Unclicked the checkbox");
        }else{
            System.out.println("No checkbox");

        }

        //click login button
        WebElement loginButton = driver.findElement(By.id("login"));
        loginButton.click();

        //verify that "ERROR: Invalid login credentials." display
        wait = new  WebDriverWait(driver,30);
        wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(".//*[@id='ajax_loginform']/p[1]")));

        WebElement errorMessage = driver.findElement(By.xpath("//*[@id='ajax_loginform']/p"));
        /* String innerHTML = errorMessage.getText();
    System.out.println(innerHTML);*/
        if(username.equalsIgnoreCase("admin")){
            System.out.println(errorMessage.getText());
            Assert.assertEquals(errorMessage.getText(),"ERROR: The password you entered for the username admin is incorrect. Lost your password?","Invalid credential");
        }

        if(username.equalsIgnoreCase("tester")){
            System.out.println(errorMessage.getText());
            Assert.assertEquals(errorMessage.getText(),"ERROR: The password you entered for the username tester is incorrect. Lost your password?","Invalid credential");

        }
    }

    @AfterTest
    public void cleanUp(){
        driver.quit();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.