UserQA
UserQA

Reputation: 17

How to verify error message displayed in page using selenium webdriver-Java

I am using this code to verify if error message is present in page.

field_required = driver.findElements(
    By.xpath("//*[@id='tab1']/fieldset/div/div/*[text()='This field is required']")
);

and checking if field_required.size() > 0

The error message should appear only if I leave a field blank and click submit. I noticed that even before I click submit field_required.size() is greater than zero .

Hence am guessing that my validation is not done by this code. Please give me another way that would work. Not sure why I am getting incorrect results here.

Upvotes: 0

Views: 2316

Answers (1)

Amol Chavan
Amol Chavan

Reputation: 3980

Seems like element is already present in the DOM and invisible to end user.

Would you please modify your code as below and try?

field_required.size() && field_required.isDisplayed()

Here is the link for isDisplayed method

Upvotes: 1

Related Questions