Accessibility testing : ARIA landmarks and labels for page regions

Use ARIA landmarks and labels to identify regions of a page.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming that we are testing a web application using Selenium WebDriver in Java23import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;7import org.openqa.selenium.remote.DesiredCapabilities;8import org.openqa.selenium.remote.RemoteWebDriver;9import java.net.URL;1011public class AccessibilityTesting {1213 public static void main(String[] args) throws Exception {1415 /​/​Assuming that the website to be tested and the proper ARIA landmarks and labels are in place16 /​/​Assuming that the ChromeDriver executable is already downloaded and placed in the project folder17 System.setProperty("webdriver.chrome.driver", "./​chromedriver.exe");18 WebDriver driver = new ChromeDriver();1920 /​/​Connecting to remote client with desired capabilities21 /​/​Assuming that the remote client is running on a Windows machine with Chrome browser version 9522 DesiredCapabilities capabilities = DesiredCapabilities.chrome();23 capabilities.setCapability("platform", "WINDOWS");24 capabilities.setCapability("version", "95");2526 WebDriver remoteDriver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);2728 /​/​Navigate to the website29 driver.get("https:/​/​www.example.com");3031 /​/​Find the regions of the page using ARIA landmarks and labels32 WebElement mainNavigation = driver.findElement(By.xpath("/​/​nav[@role='navigation']"));33 WebElement footerNavigation = driver.findElement(By.xpath("/​/​footer[@role='contentinfo']"));3435 /​/​Assert that the ARIA landmarks and labels are correctly implemented36 assert mainNavigation.getAttribute("role").equals("navigation");37 assert footerNavigation.getAttribute("role").equals("contentinfo");3839 /​/​Close the driver instances40 driver.quit();41 remoteDriver.quit();42 }43} 4445/​/​Note: The desired capabilities for the remote client may vary depending on the requirements of the project and the environment it is being tested on. Additional ARIA landmarks and labels may need to be included depending on the complexity of the web application.

Language: Python

Framework: Selenium 4

copy
1# Assumptions:2# - The web application is using proper HTML tags to implement ARIA landmarks and labels3# - The webpage has regions that require ARIA landmarks and labels45from selenium import webdriver6from selenium.webdriver.common.desired_capabilities import DesiredCapabilities78# Local Driver:9driver = webdriver.Chrome()1011# Remote Driver:12# desired_capabilities = DesiredCapabilities.CHROME.copy()13# driver = webdriver.Remote(14# command_executor='http:/​/​localhost:4444/​wd/​hub',15# desired_capabilities=desired_capabilities16# )1718driver.get("https:/​/​www.example.com")1920# Find all ARIA landmarks on the page21aria_landmarks = driver.find_elements_by_css_selector("[role]")2223# Validate that all regions with ARIA landmark contain ARIA label24for landmark in aria_landmarks:25 if landmark.get_attribute("aria-label") == "":26 print("ARIA landmark {} is missing a label".format(landmark.get_attribute("role")))2728driver.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