Accessibility testing : Inclusive accessible name for UI elements

The accessible name for a UI element must contain any visual label for the element. Accessible names for UI elements should match visual labels as closely as possible.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the driver is available and imported23/​/​Test scenario: Accessibility testing.4/​/​Test case: Inclusive accessible name for UI elements.5/​/​Description: The accessible name for a UI element must contain any visual label for the element.6/​/​Accessible names for UI elements should match visual labels as closely as possible.78public class AccessibilityTesting {910 public static void main(String[] args) {11 /​/​Create driver instance12 WebDriver driver = new ChromeDriver();1314 /​/​maximizing the browser window15 driver.manage().window().maximize();1617 /​/​Navigate to the website under test18 driver.get("https:/​/​www.example.com/​");1920 /​/​Find UI element and get the accessible name21 WebElement uiElement = driver.findElement(By.id("element-name"));22 String accessibleName = uiElement.getAttribute("aria-label");2324 /​/​Find the label for the UI element and get its text25 WebElement labelElement = driver.findElement(By.id("label-name"));26 String label = labelElement.getText();2728 /​/​Compare the accessible name with the label and print result29 if(accessibleName.equalsIgnoreCase(label)){30 System.out.println("The accessible name for UI element matches the visual label");31 }32 else{33 System.out.println("The accessible name for UI element does not match the visual label");34 }3536 /​/​close the driver instance37 driver.quit();38 }39 40 /​/​Add code for connecting to remote client with desired capabilities41 /​/​Assuming remote client url is "http:/​/​localhost:4444/​wd/​hub"42 /​* 43 DesiredCapabilities desiredCapabilities = new DesiredCapabilties();44 desiredCapabilities.setBrowserName("chrome");45 desiredCapabilities.setPlatform(Platform.WINDOWS);46 47 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), desiredCapabilities);48 */​49}

Language: Python

Framework: Selenium 4

copy
1# Assumptions:2# 1. The UI elements are properly labeled with visual labels.3# 2. The website being tested is already loaded.45from selenium.webdriver import Chrome6from selenium.webdriver.chrome.service import Service7from selenium.webdriver.chrome.options import Options8from selenium.webdriver.common.desired_capabilities import DesiredCapabilities910# Creating a new Selenium driver instance11chrome_driver_path = '/​path/​to/​chromedriver'12service = Service(chrome_driver_path)13options = Options()14options.add_argument('--headless') # So that the browser is not opened15driver = Chrome(service=service, options=options)1617# Navigate to the website being tested18driver.get('https:/​/​example.com')1920# Retrieve all the UI elements and their accessible names21ui_elements = driver.find_elements_by_xpath('/​/​*[@role="button" or @role="link" or @role="menuitem"]')22accessible_names = [element.get_attribute('aria-label') for element in ui_elements]2324# Ensure that the accessible name matches the visual label25for element in ui_elements:26 visual_label = element.get_attribute('aria-label')27 accessible_name = element.get_attribute('aria-labelledby')28 assert visual_label == accessible_name2930# Close the Selenium driver instance31driver.quit()3233# To connect to a remote client with desired capabilities, uncomment the following code:34# desired_caps = DesiredCapabilities.CHROME.copy()35# desired_caps['version'] = 'latest'36# driver = Remote(command_executor='http:/​/​localhost:4444/​wd/​hub', desired_capabilities=desired_caps)

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