2

I test web application that uses HTML5 Constraint Validation API to generate error pop-ups. How to verify text in the pop-ups in selenium tests? I cannot find the appropriate locators.

Florent B.
  • 41,537
  • 7
  • 86
  • 101
Alex
  • 362
  • 1
  • 5
  • 14

2 Answers2

2

The Selenium API doesn't support directly the constraint validation. However, you could easily get the state and the message with a piece of JavaScript (Java):

JavascriptExecutor js = (JavascriptExecutor)driver;

WebElement field = driver.findElement(By.name("email"));
Boolean is_valid = (Boolean)js.executeScript("return arguments[0].checkValidity();", field);
String message = (String)js.executeScript("return arguments[0].validationMessage;", field);

Note that it is also possible to use getAttribute to get the validationMessage even though it is a property:

String message = driver.findElement(By.name("email")).getAttribute("validationMessage");
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • Thank you very much! I used ....getAttribute("validationMessage") and got the message. – Alex May 25 '16 at 12:06
1

as per your link provided sample HTML code snippet that address your problem is :

<form id="myForm">
<input id="eid" name="email_field" type="email" />
<input type="submit" />
</form>

i have gone through above html source and looked it very clearly ,but there is no html source code in the source code for validation message that generates after clicking on "Submit query".

However i can guide you throw a workaround that will work as good as if had worked when you have the error message.

Please understand that this way:

1.Please observer source code of input boxes with Constraint Validation API in HTML5?
2.you will clearly see that its attribute defines what type of data they can take in 
  our example attribute name and type.
3.they both clearly say that above html code will take input in the form of email only.
4.so your logic will be to check value entered in the input box is of the type of 
  email or not.if its email then pass the test otherwise fail.

Hope this make sense top you and eventually helps you.

Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39