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?
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");
};
Inside div you only have text "Invalid username or password", but you are verifying "× Invalid username or password". First print text from String actualMsg and then put correct errorMsg.
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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.