Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an HTML5 validation was triggered using selenium?

Tags:

html

selenium

If testing my webapp using Selenium. All the form validation in the webapp is done using HTML5 form validations. Is there any way to assert if a form/input validation was triggered using Selenium?

like image 206
GuidoMB Avatar asked Feb 07 '12 14:02

GuidoMB


People also ask

How does HTML5 validation work?

The basic idea behind HTML5 validation is, that you tell the browser which fields you want validated but don't actually do the tedious implementation yourself. As you define what state your input field is in you also asks the browser to validate the field client-side based on the type of input field.

What is validator in HTML5?

The simplest HTML5 validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this attribute is set, the element matches the :required UI pseudo-class and the form won't submit, displaying an error message on submission when the input is empty.

How do you validate input in HTML?

To validate the form using HTML, we will use HTML <input> required attribute. The <input> required attribute is a Boolean attribute that is used to specify the input element must be filled out before submitting the Form.


1 Answers

A search like this does the trick in HTML5-aware browsers (using Java and WebDriver, but should be usable anywhere else, too):

// looks for an element that has been marked as required on a submit attempt
WebElement elem1 = driver.findElement(By.cssSelector("input:required"));
// looks for an element that has been marked as invalid on a submit attempt
WebElement elem2 = driver.findElement(By.cssSelector("input:invalid"));

The CSS pseudo classes available are:

  • :required
  • :optional
  • :valid
  • :invalid
like image 158
Petr Janeček Avatar answered Sep 23 '22 05:09

Petr Janeček