Verify an error message using selenium web driver
To extract the text Invalid username or password
you have to reach to the <div>
tag as follows :
String actualMsg = driver2.findElement(By.xpath("//div[@id='statusMsg']/div[@class='alert in fade alert-error']")).getAttribute("innerHTML");
Next your expected error message is :
String errorMsg = "× Invalid username or password";
As x
is within <a>
tag and Invalid username or password
is within <div>
tag the validation process will need a bit of String
manipulation. To make the validation simpler you can reduce the expected error message as follows :
String errorMsg = "Invalid username or password";
Now you can use the following code block to verify if the actualMsg
contains errorMsg
as follows :
if(actualMsg.contains(errorMsg))
{
System.out.println("Test Case Passed");
}else
{
System.out.println("Test Case Failed");
};
![Darshani Kaushalya](https://lh4.googleusercontent.com/-h4eHkMcjtGM/AAAAAAAAAAI/AAAAAAAAAE0/PmSHFjhXKuw/photo.jpg?sz=256)
Darshani Kaushalya
Updated on June 04, 2022Comments
-
Darshani Kaushalya about 2 years
I want to verify the error message displaying, after login failed. I am always getting "Test Case Failed" from which I have tried.
I want to verify the text with "Invalid username or password". below are the codes I tried.
This is the html code.
<div id="statusMsg"> <div class="alert in fade alert-error" style="opacity: 1;"> <a class="close" data-dismiss="alert">×</a> Invalid username or password </div> </div>
These is the code I tried.
String actualMsg=driver2.findElement(By.xpath("//div[@id='statusMsg']/div")).getText() String errorMsg= "× Invalid username or password"; if(actualError.equals(errorMsg)) { System.out.println("Test Case Passed"); }else{ System.out.println("Test Case Failed"); };
The output is always "Test Case Failed".
Is There a way to fix this?