Accessibility testing : Non-color indications for required fields and errors

Required fields and fields with errors must include some non-color way to identify them.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the website under test is developed using HTML2/​/​Assuming the required fields are marked with attribute "required" and fields with errors are marked with attribute "error"34import org.openqa.selenium.By;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.chrome.ChromeDriver;8import org.openqa.selenium.remote.DesiredCapabilities;9import org.openqa.selenium.remote.RemoteWebDriver;10import java.net.URL;1112public class AccessibilityTesting {1314 public static void main(String[] args) throws Exception {1516 /​/​ Local driver17 WebDriver driver = new ChromeDriver();1819 /​/​ Remote driver with desired capabilities20 DesiredCapabilities capability = new DesiredCapabilities();21 capability.setBrowserName("chrome");22 WebDriver driverRemote = new RemoteWebDriver(new URL("<remote-url>"), capability);2324 /​/​ Navigate to the website under test25 driver.get("<website-url>");26 driverRemote.get("<website-url>");2728 /​/​ Find all the input fields with attribute "required"29 WebElement[] requiredFields = driver.findElements(By.cssSelector("[required]"));30 WebElement[] requiredFieldsRemote = driverRemote.findElements(By.cssSelector("[required]"));3132 /​/​ Find all the input fields with attribute "error"33 WebElement[] errorFields = driver.findElements(By.cssSelector("[error]"));34 WebElement[] errorFieldsRemote = driverRemote.findElements(By.cssSelector("[error]"));3536 /​/​ Iterate through the required fields and verify non-color indication is present37 for (WebElement field : requiredFields) {38 String ariaLabel = field.getAttribute("aria-label");39 String ariaDesc = field.getAttribute("aria-describedby");40 if (ariaLabel != null && !ariaLabel.isEmpty()) {41 System.out.println("Non-color indication present for required field: " + ariaLabel);42 } else if (ariaDesc != null && !ariaDesc.isEmpty()) {43 System.out.println("Non-color indication present for required field: " + ariaDesc);44 } else {45 System.out.println("Non-color indication missing for required field: " + field.getAttribute("name"));46 }47 }48 for (WebElement field : requiredFieldsRemote) {49 String ariaLabel = field.getAttribute("aria-label");50 String ariaDesc = field.getAttribute("aria-describedby");51 if (ariaLabel != null && !ariaLabel.isEmpty()) {52 System.out.println("Non-color indication present for required field: " + ariaLabel);53 } else if (ariaDesc != null && !ariaDesc.isEmpty()) {54 System.out.println("Non-color indication present for required field: " + ariaDesc);55 } else {56 System.out.println("Non-color indication missing for required field: " + field.getAttribute("name"));57 }58 }5960 /​/​ Iterate through the fields with errors and verify non-color indication is present61 for (WebElement field : errorFields) {62 String ariaLabel = field.getAttribute("aria-label");63 String ariaDesc = field.getAttribute("aria-describedby");64 if (ariaLabel != null && !ariaLabel.isEmpty()) {65 System.out.println("Non-color indication present for error field: " + ariaLabel);66 } else if (ariaDesc != null && !ariaDesc.isEmpty()) {67 System.out.println("Non-color indication present for error field: " + ariaDesc);68 } else {69 System.out.println("Non-color indication missing for error field: " + field.getAttribute("name"));70 }71 }72 for (WebElement field : errorFieldsRemote) {73 String ariaLabel = field.getAttribute("aria-label");74 String ariaDesc = field.getAttribute("aria-describedby");75 if (ariaLabel != null && !ariaLabel.isEmpty()) {76 System.out.println("Non-color indication present for error field: " + ariaLabel);77 } else if (ariaDesc != null && !ariaDesc.isEmpty()) {78 System.out.println("Non-color indication present for error field: " + ariaDesc);79 } else {80 System.out.println("Non-color indication missing for error field: " + field.getAttribute("name"));81 }82 }8384 /​/​ Close the driver instances85 driver.quit();86 driverRemote.quit();8788 }8990}

Language: Python

Framework: Selenium 4

copy
1#Assuming the web application is located at the URL "https:/​/​example.com" 23#Importing the necessary libraries4from selenium.webdriver import Chrome #To use ChromeDriver for WebDriver5from selenium.webdriver.chrome.options import Options #To set up ChromeDriver options67#Creating a ChromeDriver instance with local driver8chromedriver_path = "Path to chromedriver.exe" #Assuming chromedriver.exe is located at the working directory9driver = Chrome(executable_path=chromedriver_path)1011#Connecting to remote client with desired capabilities12#Remote driver is not required for this test case1314#Assuming the web page contains a form with required fields as marked with asterisks and alert messages for invalid input15#Locating all the required fields and error messages on the web page16required_fields = driver.find_elements_by_xpath("/​/​input[@required]") #Assuming all required fields are input elements17alert_messages = driver.find_elements_by_class_name("alert") #Assuming all error messages have the "alert" class1819#Checking if all required fields have non-color indications20for field in required_fields:21 label = field.get_attribute("aria-label") #Assuming all required fields have aria-label attribute to display label text22 assert label.endswith("required") #Verifying if the label text ends with "required"23 assert label.startswith("*") #Verifying if the label text starts with an asterisk (*)24 #Assuming that the field has a red asterisk icon next to the label text for non-color indication2526#Checking if all error messages have non-color indications27for message in alert_messages:28 assert message.get_attribute("role") == "alert" #Verifying if the message has role attribute set to "alert"29 assert "error" in message.get_attribute("class") #Verifying if the message has "error" in its class name30 #Assuming that the message has an exclamation icon next to the message text for non-color indication3132#Closing the browser33driver.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