Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 35 ยท Projects

Project: Form Validation Checks

Test form validation with Selenium: required fields, pattern rules, error summaries and accessibility attributes such as aria-invalid and aria-describedby.

Forms are where users meet your application's rules: this field is required, that one must look like an email address, and when something is wrong the page must say so clearly. Good validation is more than red text. It has to work for people using keyboards and screen readers, and it has to be correct for the odd inputs real users type. That makes forms a rich target for automated checks.

In this project you will plan validation tests, learn how Selenium reads the browser's built-in validation state, and check the accessibility attributes that connect an error message to its field. As in the other projects, the real Selenium code (Python) is read-only, and a stand-in runs in the page: validation rules, a model of the markup a form would produce, and a small suite that checks it. The stand-in is plain JavaScript, not Selenium.

What to check on a form

Required Empty fields are rejected.
Patterns Emails, ZIP codes, phone numbers.
Error summary One announced list of problems.
Accessibility Errors are tied to their fields.

Behavior first, then the markup that makes it usable.

1 / 2

Two layers of validation

A form can be validated in two places, and your tests must know which one they are exercising.

Browser (HTML) validation uses attributes such as required, type="email", minlength, min, max and pattern. When the user submits, the browser checks them and shows its own message bubble, unless the form has the novalidate attribute. Selenium cannot reliably read that bubble, because it is browser UI rather than part of the page. What it can read is the validation state through the DOM, as shown below.

Custom (JavaScript or server) validation checks the same rules in code and renders its own messages into the page. Those messages are ordinary elements, so Selenium reads them like any other text. Most real applications use custom messages for consistent wording and accessibility, plus a server-side check, because browser validation alone can be bypassed.

Reading the browser's validation state

For native validation, ask the element rather than looking at pixels. The validity object has flags such as valueMissing, typeMismatch and patternMismatch, and validationMessage holds the browser's text. In Python:

Python
from selenium.webdriver.common.by import By

email = driver.find_element(By.ID, "email")
email.send_keys("not-an-email")

# Is the field valid according to the browser?
assert driver.execute_script("return arguments[0].checkValidity()", email) is False

# Which rule failed?
type_mismatch = driver.execute_script("return arguments[0].validity.typeMismatch", email)
assert type_mismatch is True

# The browser's own wording (differs by browser and language!)
print(email.get_property("validationMessage"))

Do not assert on the exact validationMessage text. It changes between browsers, versions and languages, so such a test breaks for reasons unrelated to your application. Assert on the validity flags instead.

Testing custom messages and attributes

With custom validation, the tests read what the page renders. Three attributes carry most of the accessibility meaning:

  • aria-invalid="true" on a field says that its current value is wrong. Assistive technology can announce it.
  • aria-describedby="email-error" links the field to the element with that id, so the error text is read together with the field.
  • A summary with role="alert" (or a live region) announces new problems when the form is submitted, ideally listing links that jump to each field. Moving focus to the summary is another common approach.

In Selenium, read exact attribute values with get_dom_attribute, then follow the link yourself:

Python
def error_for(driver, field):
    assert field.get_dom_attribute("aria-invalid") == "true"
    message_id = field.get_dom_attribute("aria-describedby")
    assert message_id, "field has no aria-describedby"
    message = driver.find_element(By.ID, message_id)
    assert message.is_displayed()
    return message.text


def test_empty_submit_shows_errors(driver, base_url):
    driver.get(f"{base_url}/signup")
    driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()

    name = driver.find_element(By.ID, "name")
    assert error_for(driver, name) == "Name is required."

    summary = driver.find_element(By.CSS_SELECTOR, "[role='alert']")
    assert "problems found" in summary.text
    # focus moved to the summary so keyboard and screen-reader users notice it
    assert driver.switch_to.active_element == summary

Selenium 4 also exposes the browser's computed accessibility information through element.accessible_name and element.aria_role, which is handy for checking that a control has a name a screen reader can announce. For a complete accessibility audit you still need dedicated tools and manual checks; automated attribute checks catch the common mistakes, not all of them.

Pattern rules need data-driven tests

A validation rule is a tiny function: text in, valid or not out. The fastest way to test it is a table of examples, valid and invalid, including the nasty edge cases. The stand-in below runs such a table against three regular expressions, and the output contains a genuine finding.

JS Data-driven checks for pattern rules

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Nine of the ten cases pass, and the failure is useful: the phone number +1 (555) 010-9999 is valid, but the rule allows at most 15 characters and this number has 17, counting spaces and punctuation. A table of realistic examples found a bug in the rule before a real customer did. Change {7,15} to {7,20} and run again to see all ten pass. The second part shows a classic trap: an unanchored regular expression accepts 123456 because it only needs to find five digits somewhere. The HTML pattern attribute is different, because the browser treats it as if it matched the whole value, equivalent to wrapping it in ^(?:...)$. When you port a rule between HTML and JavaScript, keep that difference in mind.

Now the markup. The stand-in below produces a description of what a well-built form would render (field attributes, error elements and an error summary) and checks it with five tests. Then it runs the same suite against a version with a deliberate bug: the aria-describedby link is missing. This shows how an attribute check catches a mistake that no visual test would notice, because the red text still appears on screen.

JS Check the accessibility markup of a form

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first run passes all five tests. In the second, four tests still pass, because the messages and the summary look fine, and only one fails with name has no aria-describedby. A sighted user would see no difference, but a screen-reader user would hear the field without its error. Each assertion corresponds to something you can do in real Selenium: get_dom_attribute("aria-invalid"), get_dom_attribute("aria-describedby") followed by find_element(By.ID, ...), reading the summary's role, and checking its links. Remember the honest limit of this stand-in: it checks a description of markup that the page object builds, while real tests check the live DOM of your application.

A test plan for a sign-up form

Put everything together as a checklist for a real form:

  1. Empty submit: every required field gets an error; optional fields do not.
  2. One rule at a time: a table of valid and invalid values per field (boundary lengths, whitespace-only input, leading and trailing spaces, Unicode names like "Zoeฬˆ").
  3. Error placement: each message is tied to its field (aria-describedby), and the field is marked aria-invalid.
  4. Summary: it appears, is announced (role="alert" or focus moved to it) and its links focus the right field.
  5. Recovery: fixing a field removes its error and the invalid mark, and fixing all fields lets the form submit.
  6. Keyboard only: you can complete the form and submit using Tab, Space and Enter (see Clicking and Typing and Forms, Checkboxes and Dropdowns).
  7. No leakage: never use real personal data as test input; see Security, Ethics and Scraping Limits.

When an error appears only after a request or a debounce delay, wait for it with an explicit wait instead of a fixed sleep; see Implicit, Explicit and Fluent Waits. Keep locators stable by using ids and test ids, as covered in Choosing Stable Locators.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should a test avoid asserting the exact text of the browser's validationMessage?

    Native messages are browser-controlled. Assert on validity flags (such as valueMissing) or checkValidity() instead.
  2. 2. What does aria-describedby="email-error" on an input do?

    aria-describedby references another element by id to provide a description. It does not validate anything by itself.
  3. 3. The unanchored regex /\d{5}/ accepts 123456. How does the HTML pattern attribute differ?

    The pattern attribute is matched against the entire value, while a JavaScript test() call only needs to find a match anywhere unless you anchor it.
  4. 4. A test checks only that red text appears after an invalid submit. What can it miss?

    Visible text can look right while the accessibility links are missing. Attribute checks catch this class of bug.

Summary

  • Know the layer you test: native browser validation (read validity, checkValidity()) or custom messages rendered in the page.
  • Never assert on the browser's native message text; assert on validity flags.
  • Check the markup that makes errors usable: aria-invalid, aria-describedby pointing to an existing message, and an announced error summary with links.
  • Test pattern rules with tables of realistic valid and invalid values; they find bugs such as a too-short phone length limit.
  • The HTML pattern attribute matches the whole value, unlike an unanchored JavaScript regex.
  • The stand-ins model the checks in plain JavaScript; the Python blocks are the real Selenium code and are read-only here.

Next, continue with Project: Extracting Table Data.