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.