Accessibility testing : Keyboard shortcuts with user customization

If a keyboard shortcut uses only letter (including upper- and lower-case letters), punctuation, number, or symbol characters, then the user must be able to disable the shortcut, remap the shortcut, or limit the shortcut to only when a particular interactive element has focus.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the web application has implemented accessibility features2/​/​Assuming keyboard shortcuts are present in the application3/​/​Assuming user customization is available in the web application45import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.chrome.ChromeDriver;9import org.openqa.selenium.interactions.Actions;1011public class KeyboardShortcutsAccessibilityTest {12 public static void main(String[] args) {13 System.setProperty("webdriver.chrome.driver", "path/​to/​chromedriver");14 WebDriver driver = new ChromeDriver();15 driver.get("https:/​/​www.example.com"); /​/​Assuming web application URL1617 /​/​Assuming user is logged in to the application18 WebElement element = driver.findElement(By.id("username"));19 element.sendKeys("username");20 element = driver.findElement(By.id("password"));21 element.sendKeys("password");22 element.submit();2324 /​/​Assuming user navigated to accessibility settings page25 element = driver.findElement(By.xpath("/​/​a[contains(text(), 'Accessibility')]"));26 element.click();2728 /​/​Assuming there is a section for keyboard shortcuts29 element = driver.findElement(By.xpath("/​/​a[contains(text(), 'Keyboard Shortcuts')]"));30 element.click();3132 /​/​Assuming there is a table with all the available keyboard shortcuts33 WebElement shortcutsTable = driver.findElement(By.id("shortcuts-table"));3435 /​/​Assuming the first shortcut is customizable36 WebElement shortcutRow = shortcutsTable.findElement(By.xpath("/​/​tr[contains(@class, 'customizable')]"));37 WebElement shortcutKeyElement = shortcutRow.findElement(By.xpath("/​/​td[@class='shortcut-key']"));38 String shortcutKey = shortcutKeyElement.getText();3940 /​/​Assuming the user clicks on the 'Edit Shortcut' button for the first shortcut41 WebElement editShortcutButton = shortcutRow.findElement(By.xpath("/​/​button[contains(text(), 'Edit Shortcut')]"));42 editShortcutButton.click();4344 /​/​Assuming the user wants to remap the first shortcut and chooses a new key 'A'45 WebElement remapShortcutInput = shortcutRow.findElement(By.xpath("/​/​input[@class='remap-shortcut-input']"));46 remapShortcutInput.click();47 remapShortcutInput.sendKeys("A");4849 /​/​Assuming the user wants to disable the first shortcut50 WebElement disableShortcutCheckbox = shortcutRow.findElement(By.xpath("/​/​input[@class='disable-shortcut-checkbox']"));51 disableShortcutCheckbox.click();5253 /​/​Assuming the user wants the first shortcut to only work when a specific interactive element has focus54 WebElement limitShortcutSelect = shortcutRow.findElement(By.xpath("/​/​select[@class='limit-shortcut-select']"));55 Actions actions = new Actions(driver);56 actions.moveToElement(limitShortcutSelect).click().build().perform();57 WebElement limitOption = shortcutRow.findElement(By.xpath("/​/​option[contains(text(), 'Button')]"));58 limitOption.click();5960 /​/​Assuming the user saves the changes61 WebElement saveButton = shortcutRow.findElement(By.xpath("/​/​button[contains(text(), 'Save Changes')]"));62 saveButton.click();6364 /​/​Assuming the changes are applied and the first shortcut now works only when a button has focus and can be triggered by 'A' key65 66 /​/​Code to connect to a remote client with desired capabilities67 /​/​Assuming the remote client is running on a different machine68 /​/​Assuming the remote client is also using Selenium 4 and Java69 /​/​Assuming desired capabilities are set up as follows70 71 /​/​desiredCapabilities.setBrowserName("chrome");72 /​/​desiredCapabilities.setPlatform(Platform.MAC);73 /​/​desiredCapabilities.setCapability("version", "90.0");74 /​/​desiredCapabilities.setCapability("enableVNC", true);75 76 /​/​Assuming the remote client URL is "https:/​/​remoteserver.example.com:4444/​wd/​hub"77 78 /​/​WebDriver driver = new RemoteWebDriver(new URL("https:/​/​remoteserver.example.com:4444/​wd/​hub"), desiredCapabilities);7980 driver.quit();81 }82}

Language: Python

Framework: Selenium 4

copy
1# Assumptions:2# 1. The application being tested has keyboard shortcuts with letter, punctuation, number, or symbol characters.3# 2. The application allows users to customize the keyboard shortcuts.4# 3. The keyboard shortcuts are disabled by default when the user starts using the application.5# 4. The application has a function to limit the shortcut to only when a particular interactive element has focus.67# Test case code:8from selenium import webdriver9from selenium.webdriver.common.keys import Keys1011# Using local driver12driver = webdriver.Chrome()1314# Connecting to remote client with desired capabilities15# remote_url = 'http:/​/​localhost:4444/​wd/​hub'16# caps = DesiredCapabilities.CHROME17# driver = webdriver.Remote(command_executor=remote_url, desired_capabilities=caps)1819# Launch the application being tested20driver.get('https:/​/​example.com')2122# Disable keyboard shortcut23disable_shortcut_key = Keys.CONTROL + Keys.SHIFT + 'D'24disable_shortcut_value = None25driver.execute_script(f'window.onkeydown = function(event) {{if (event.key == "{disable_shortcut_key}") {{return {disable_shortcut_value}}}}')2627# Remap keyboard shortcut28remap_shortcut_key = 'F2'29remap_shortcut_value = 'some_function()'30driver.execute_script(f'window.onkeydown = function(event) {{if (event.key == "{remap_shortcut_key}") {{event.preventDefault(); {remap_shortcut_value}}}}')3132# Limit keyboard shortcut to particular interactive element33limit_shortcut_element_id = 'some_element_id'34limit_shortcut_key = 'F3'35limit_shortcut_value = 'some_function()'36driver.execute_script(f'document.getElementById("{limit_shortcut_element_id}").onkeydown = function(event) {{if (event.key == "{limit_shortcut_key}") {{event.preventDefault(); {limit_shortcut_value}}}}}')3738# Close the application being tested39driver.close()

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