Accessibility testing : Accessible form validation

Programmatically indicate required fields using the required or aria-required attributes. Also, visually indicate required fields in the form's instructions or form labels. Do not indicate required fields for CSS alone.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the web page has a form with some required fields, and the required fields are marked using the "required" or "aria-required" attributes, and these fields are also visually indicated in the form's instructions or labels.23/​/​Importing required packages for Selenium and WebDriver.4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.By;89public class AccessibleFormValidation {1011 public static void main(String[] args) {1213 /​/​Connecting to local Chrome browser with Selenium WebDriver.14 WebDriver driver = new ChromeDriver();15 16 /​/​Connecting to remote client with desired capabilities (assuming required).17 /​/​DesiredCapabilities capabilities = DesiredCapabilities.chrome();18 /​/​WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);1920 /​/​Launching the webpage with form to validate.21 driver.get("https:/​/​www.example.com/​form");2223 /​/​Checking that all required fields are properly marked using "required" or "aria-required" attributes.24 WebElement requiredField1 = driver.findElement(By.xpath("/​/​input[@name='name' and @required]"));25 WebElement requiredField2 = driver.findElement(By.xpath("/​/​input[@name='email' and @aria-required='true']"));26 WebElement requiredField3 = driver.findElement(By.xpath("/​/​textarea[@name='message' and @required]"));2728 /​/​Checking that all required fields are visually indicated in the form's instructions or labels.29 WebElement label1 = driver.findElement(By.xpath("/​/​label[@for='name' and contains(text(),'*')]"));30 WebElement label2 = driver.findElement(By.xpath("/​/​label[@for='email' and contains(text(),'*')]"));31 WebElement label3 = driver.findElement(By.xpath("/​/​label[@for='message' and contains(text(),'*')]"));3233 /​/​Printing a success message if all required fields are marked and visually indicated correctly.34 System.out.println("Form validation test passed!");3536 /​/​Closing the WebDriver instance when the testing is complete.37 driver.quit();38 }39}

Language: Python

Framework: Selenium 4

copy
1# Assume that:2# - The web application uses HTML forms to collect user input.3# - All form fields have a "name" attribute that corresponds to their label.4# - Required fields are those with a "required" attribute or an "aria-required" attribute set to "true".5# - The form's instructions or labels are accessible through the "for" attribute of their corresponding "label" element.6# - The visual indication of required fields is accomplished by adding an asterisk (*) to the label of all required fields.78# Import the necessary Selenium libraries9from selenium.webdriver import Chrome10from selenium.webdriver.common.by import By11from selenium.webdriver.support.ui import WebDriverWait12from selenium.webdriver.support import expected_conditions as EC1314# Set up desired capabilities for remote client connection (assumes a Selenium Grid is running)15desired_capabilities = {16 "browserName": "chrome",17 "version": "90.0",18 "platform": "WINDOWS"19}2021# Connect to a remote client22# driver = Remote(command_executor="http:/​/​127.0.0.1:4444/​wd/​hub", desired_capabilities=desired_capabilities)2324# Create a new local Chrome driver instance25driver = Chrome()2627# Navigate to the web application's form page28driver.get("https:/​/​example.com/​form")2930# Wait for the form to load31WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "form")))3233# Find all input fields in the form34input_fields = driver.find_elements(By.TAG_NAME, "input")3536# Find all labels in the form (assuming they are inside a "label" element)37labels = driver.find_elements(By.TAG_NAME, "label")3839# Track required fields that have not been filled in40missing_fields = []4142# Iterate over each input field to check if it is required43for input_field in input_fields:44 is_required = False4546 # Check if the field has a "required" attribute47 required_attr = input_field.get_attribute("required")48 if required_attr is not None and required_attr.lower() == "true":49 is_required = True5051 # Check if the field has an "aria-required" attribute set to "true"52 aria_required_attr = input_field.get_attribute("aria-required")53 if aria_required_attr is not None and aria_required_attr.lower() == "true":54 is_required = True5556 # If the field is required, check if it has been filled in57 if is_required and input_field.get_attribute("value") == "":58 missing_fields.append(input_field.get_attribute("name"))5960# Iterate over each label to add an asterisk to those for required fields61for label in labels:62 associated_field = label.get_attribute("for")63 if associated_field in missing_fields:64 label.text += " *"6566# Verify that all required fields have been filled in67assert len(missing_fields) == 0, f"The following fields are required: {', '.join(missing_fields)}"6869# Close the driver70driver.quit()

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing