Accessibility testing : ARIA as supplement to HTML

Use ARIA to enhance accessibility only when HTML is not sufficient. Use caution when providing ARIA roles, states, and properties.

Language: Java

Framework: Selenium 4

copy
1/​/​Assumptions: 2/​/​1. The website under test is built with HTML3/​/​2. ARIA can be used to enhance the accessibility of the website 4/​/​3. ARIA roles, states, and properties should be added only when HTML is not sufficient 56/​/​Connect to remote client with desired capabilities 7WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), DesiredCapabilities.chrome());89/​/​Launch the website under test10driver.get("https:/​/​www.example.com/​");1112/​/​Check if HTML is sufficient for accessibility13boolean isHtmlSufficient = ((JavascriptExecutor) driver).executeScript("return document.querySelector('[role]') == null");1415/​/​If HTML is not sufficient, add ARIA roles, states, and properties16if(!isHtmlSufficient){17 /​/​Add ARIA roles, states, and properties using Selenium's Actions class18 Actions builder = new Actions(driver);19 WebElement element = driver.findElement(By.id("elementId"));20 builder.moveToElement(element).click().sendKeys(Keys.TAB).build().perform();21 element.sendKeys(Keys.chord(Keys.ALT, "1")); /​/​Add an ARIA role22 element.sendKeys(Keys.chord(Keys.ALT, "2")); /​/​Add an ARIA state23 element.sendKeys(Keys.chord(Keys.ALT, "3")); /​/​Add an ARIA property24}2526/​/​Assert that accessibility is enhanced27Assert.assertTrue(((JavascriptExecutor)driver).executeScript("return document.querySelector('[role]') != null"));2829/​/​Close the browser30driver.quit();

Language: Python

Framework: Selenium 4

copy
1#Assumptions: 2#1. The web application uses HTML and ARIA roles for accessibility, and the ARIA roles are only added when HTML is insufficient.3#2. The Selenium web driver is installed.4#3. The web page has already been loaded and the driver is on the correct web page.56# Import necessary selenium classes7from selenium.webdriver.common.keys import Keys8from selenium.webdriver.common.by import By9from selenium.webdriver.support.ui import WebDriverWait10from selenium.webdriver.support import expected_conditions as EC11from selenium.webdriver.common.desired_capabilities import DesiredCapabilities1213# Create a local driver14driver = webdriver.Chrome()1516# Uncomment the code below to connect to a remote client with desired capabilities 17#capabilities = DesiredCapabilities.CHROME18#driver = webdriver.Remote(19# command_executor='http:/​/​127.0.0.1:4444/​wd/​hub',20# desired_capabilities=capabilities)2122# Navigate to the page to be tested23driver.get('https:/​/​www.example.com')2425# Find elements by aria-label attribute26aria_label_elements = driver.find_elements(By.CSS_SELECTOR, "[aria-label]")2728# Find elements by aria-labelledby attribute29aria_lablledby_elements = driver.find_elements(By.CSS_SELECTOR, "[aria-labelledby]")3031# Find elements by aria-describedby attribute32aria_describedby_elements = driver.find_elements(By.CSS_SELECTOR, "[aria-describedby]")3334# Display elements that have ARIA roles35for element in aria_label_elements:36 if element.get_attribute("role"):37 print("Element with ARIA role found:")38 print(element)39for element in aria_lablledby_elements:40 if element.get_attribute("role"):41 print("Element with ARIA role found:")42 print(element)43for element in aria_describedby_elements:44 if element.get_attribute("role"):45 print("Element with ARIA role found:")46 print(element)47 48# Close the driver49driver.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