5

I want to verify the error message displaying after login failed. Specifically, I want to verify the text with "Invalid username or password".

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>

This 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?

2 Answers 2

4

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");
};
Sign up to request clarification or add additional context in comments.

Comments

0

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");
    }

;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.