Accessibility testing : Logical tab order through links and form controls

Create a logical tab order through links, form controls, and interactive objects.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the web application is already launched in the browser23/​/​Assuming the page has form controls, links, and interactive objects45import org.openqa.selenium.WebDriver;67import org.openqa.selenium.WebElement;89import org.openqa.selenium.By;1011import org.openqa.selenium.Keys;1213import org.openqa.selenium.interactions.Actions;1415import org.openqa.selenium.chrome.ChromeDriver;1617public class AccessibilityTesting {1819public static void main(String[] args) {2021/​/​Using local driver2223System.setProperty("webdriver.chrome.driver", "local/​path/​to/​chromedriver");2425WebDriver driver = new ChromeDriver();2627/​/​Remote client with desired capabilities2829/​/​DesiredCapabilities capabilities = DesiredCapabilities.chrome();3031/​/​capabilities.setCapability("version", "latest");3233/​/​capabilities.setCapability("platform", "MAC");3435/​/​driver = new RemoteWebDriver(new URL("https:/​/​localhost:4444/​wd/​hub"),capabilities);3637/​/​Assuming the logical tab order is from top to bottom and left to right3839/​/​First clicking on the first form control4041WebElement firstFormControl = driver.findElement(By.id("first-form-control"));4243firstFormControl.click();4445/​/​Simulating tab key press4647Actions actions = new Actions(driver);4849actions.sendKeys(Keys.TAB).perform();5051/​/​Moving logically through form controls5253WebElement secondFormControl = driver.findElement(By.id("second-form-control"));5455secondFormControl.click();5657actions.sendKeys(Keys.TAB).perform();5859WebElement thirdFormControl = driver.findElement(By.id("third-form-control"));6061thirdFormControl.click();6263actions.sendKeys(Keys.TAB).perform();6465/​/​Moving logically through links and interactive objects6667WebElement firstLink = driver.findElement(By.linkText("First link"));6869firstLink.click();7071actions.sendKeys(Keys.TAB).perform();7273WebElement secondLink = driver.findElement(By.linkText("Second link"));7475secondLink.click();7677actions.sendKeys(Keys.TAB).perform();7879/​/​Closing the browser8081driver.quit();8283}8485}

Language: Python

Framework: Selenium 4

copy
1from selenium import webdriver2from selenium.webdriver.common.keys import Keys34# Launching the local driver5driver = webdriver.Chrome()6# To connect to remote client with desired capabilities, uncomment below code and specify the remote webdriver URL7# driver = webdriver.Remote('http:/​/​<REMOTE_WEBDRIVER_URL>:4444/​wd/​hub', desired_capabilities=<DESIRED_CAPABILITIES>)89# Navigating to the application URL 10driver.get("https:/​/​example.com")1112# Finding the first form control and clicking on it13first_form_control = driver.find_element_by_xpath('/​/​input[@type="text"]')14first_form_control.click()1516# Pressing the Tab Key to navigate to next element17first_form_control.send_keys(Keys.TAB)1819# Finding the first link and clicking on it20first_link = driver.find_element_by_xpath('/​/​a[contains(@href,"https:/​/​")]')21first_link.click()2223# Pressing the Tab Key to navigate to next element24first_link.send_keys(Keys.TAB)2526# Finding the second form control and clicking on it27second_form_control = driver.find_element_by_xpath('/​/​input[@type="password"]')28second_form_control.click()2930# Pressing the Tab Key to navigate to next element31second_form_control.send_keys(Keys.TAB)3233# Finding the third form control and clicking on it34third_form_control = driver.find_element_by_xpath('/​/​input[@type="checkbox"]')35third_form_control.click()3637# Pressing the Tab Key to navigate to next element38third_form_control.send_keys(Keys.TAB)3940# Finding the fourth form control and clicking on it41fourth_form_control = driver.find_element_by_xpath('/​/​input[@type="radio"]')42fourth_form_control.click()4344# Pressing the Tab Key to navigate to next element45fourth_form_control.send_keys(Keys.TAB)4647# Adding comment to specify assumptions48# Assuming all form controls, links, and interactive objects are properly tagged and ordered for accessibility testing.

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