Avoid implementing access keys. When access keys and other keyboard shortcuts are implemented, they must not interfere with existing browser and screen reader provided shortcuts.
Language: Java
Framework: Selenium 4
1Java with Selenium 4 Framework:23// Assumptions: 4// 1) The web application being tested already has keyboard shortcuts enabled for screen readers. 5// This means that the keyboard shortcuts are not to be overridden and they should work as expected. 6// 2) The web application being tested does not use any existing keyboard shortcuts. 7// 3) We are testing that the application does not implement any new keyboard shortcuts or access keys that would interfere with the browser and screen reader provided shortcuts. 8// 4) We are using the local driver but can be easily connected to a remote client with the desired capabilities.910// Importing the necessary packages11import org.openqa.selenium.WebDriver;12import org.openqa.selenium.WebElement;13import org.openqa.selenium.chrome.ChromeDriver;14import org.openqa.selenium.Keys;1516// Defining the WebDriver object17WebDriver driver = new ChromeDriver();1819// Test case: Avoid access keys20// Description: Avoid implementing access keys that interfere with existing browser and screen reader provided shortcuts21public void testAccessibility() {2223 // Navigate to the web page being tested24 driver.navigate().to("https://www.example.com");2526 // Get all the elements on the page27 List<WebElement> elements = driver.findElements(By.cssSelector("*"));2829 // Loop through all the elements and check if they have any access keys defined30 for(WebElement element : elements) {31 if(element.getAttribute("accesskey") != null) {32 // Access key found, fail the test33 System.out.println("Access keys are not allowed in the application");34 break;35 }36 }3738 // Close the driver39 driver.close();40} 4142// Note: To connect to a remote client with desired capabilities, refer to the Selenium documentation on how to set up the remote driver with the desired capabilities.
Language: Python
Framework: Selenium 4
1from selenium import webdriver2from selenium.webdriver.common.keys import Keys34#Assuming a web application is being tested5#Assuming we are on the login page6#Assuming we have a login button78#Set up local driver9driver = webdriver.Chrome()1011#Navigate to the login page12driver.get("https://www.example.com/login")1314#Avoid access keys15#First, check if any access keys are implemented on the page16#Assuming the access keys are labeled with "accesskey" attribute, we search for this attribute in the HTML code of the page1718elements_with_access_keys = driver.find_elements_by_xpath("//*[@accesskey]")19if len(elements_with_access_keys) > 0:20 #If there are access keys, print an error message and fail the test21 print("Error: Access keys are implemented on the page")22 assert False2324#If there are no access keys, pass the test and continue testing the login process 25else:26 #Assuming there are two input fields, one for username and another for password27 username = driver.find_element_by_id("username")28 password = driver.find_element_by_id("password")2930 #Login button31 login_button = driver.find_element_by_id("login_button")3233 #Enter credentials34 username.send_keys("user12345")35 password.send_keys("password123")3637 #Click login button38 login_button.click()3940 #Assert that the user is logged in41 assert "Dashboard" in driver.page_source4243#To connect to a remote client with desired capabilities, we can add the following commented code4445'''46#Set up remote driver with desired capabilities47desired_capabilities = {48 "browserName": "chrome",49 "browserVersion": "95.0",50 "platformName": "Windows 10",51 "sauce:options": {52 "username": "your_user_name",53 "accessKey": "your_access_key"54 }55}56remote_driver = webdriver.Remote(57 command_executor="https://ondemand.saucelabs.com:443/wd/hub",58 desired_capabilities=desired_capabilities59)60'''
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing