Accessibility testing : Keyboard operability without timing requirements

All functionality should be available to a keyboard without requiring specific timing of keystrokes, unless the functionality cannot be provided by a keyboard alone.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming we are testing a web application and using Chrome Driver2import java.util.concurrent.TimeUnit;3import org.junit.*;4import org.openqa.selenium.*;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.remote.CapabilityType;7import org.openqa.selenium.remote.DesiredCapabilities;89public class KeyboardAccessibilityTest {10 11 private WebDriver driver;1213 @Before14 public void setup() {15 /​/​Add commented code for setting desired capabilities for remote client16 /​/​DesiredCapabilities capabilities = DesiredCapabilities.chrome();17 /​/​capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");18 /​/​capabilities.setCapability(CapabilityType.VERSION, "Latest");19 /​/​capabilities.setCapability(CapabilityType.PLATFORM_NAME, Platform.LINUX);20 /​/​driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);2122 /​/​Assuming we are using local driver23 System.setProperty("webdriver.chrome.driver", "path/​to/​chromedriver.exe");/​/​Path to the ChromeDriver application24 driver = new ChromeDriver(); 25 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);26 driver.manage().window().maximize();27 driver.get("https:/​/​www.example.com");/​/​Add URL of the application being tested 28 }2930 @Test31 public void testKeyboardOperability() throws InterruptedException {32 /​/​Assuming we are testing the functionality of a form using keyboard33 WebElement inputField = driver.findElement(By.id("input_field"));34 inputField.sendKeys("Test Input");/​/​Enter text in the input field using keyboard35 inputField.sendKeys(Keys.TAB);/​/​Use TAB key to navigate to the next field36 WebElement submitButton = driver.findElement(By.id("submit_button"));37 submitButton.sendKeys(Keys.ENTER);/​/​Use ENTER key to submit the form3839 /​/​Add assertion to check if the form was submitted successfully40 Assert.assertTrue("Form not submitted successfully", driver.getCurrentUrl().contains("success"));41 }4243 @After44 public void tearDown() {45 driver.quit();46 }47}48

Language: Python

Framework: Selenium 4

copy
1from selenium.webdriver.common.keys import Keys2from selenium.webdriver.common.action_chains import ActionChains3from selenium.webdriver.common.by import By4from selenium.webdriver.support.ui import WebDriverWait5from selenium.webdriver.support import expected_conditions as EC6from selenium.webdriver import DesiredCapabilities7from selenium.webdriver.remote.remote_connection import RemoteConnection89#Assumptions: 10#1. The web application has a login page and an accessiblity testing page.11#2. The web application supports keyboard operability feature.12#3. The page has a 'tab' functionality.13#4. The functionality specific to mouse-click operations only will support mouse keys.1415#Local Driver16driver = webdriver.Chrome()17driver.maximize_window()1819#Remote Client with Desired Capabilities20#capabilities = DesiredCapabilities.CHROME.copy()21#capabilities['platform'] = "WINDOWS"22#capabilities['version'] = "10"23#driver = webdriver.Remote(24# command_executor='http:/​/​127.0.0.1:4444/​wd/​hub',25# desired_capabilities=capabilities)2627#Navigate to accessibility testing page28driver.get("https:/​/​www.example.com/​accessibility-testing")2930# Wait for page to load31wait = WebDriverWait(driver, 10)32page_loaded = wait.until(EC.visibility_of_element_located((By.XPATH, "/​/​h1[contains(text(), 'Accessibility Testing')]")))3334# Keyboard operability without timing requirements35# Assumption: All the functionality is available to a keyboard36# Press 'tab' key to navigate to the next element37action_chains = ActionChains(driver)3839for i in range(6):40 action_chains.send_keys(Keys.TAB)41 42# Press 'Enter' key to perform the required action43action_chains.send_keys(Keys.ENTER).perform()4445# Assumption: If the functionality can't be provided by a keyboard alone, then it will need to support mouse keys.46# Therefore, we need to check if the current element is clickable.47if driver.find_element_by_xpath("/​/​button[contains(text(), 'Click me')]").is_enabled():48 driver.find_element_by_xpath("/​/​button[contains(text(), 'Click me')]").click()49else:50 raise Exception("The functionality cannot be provided by a keyboard alone.") 5152#Close the browser window53driver.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