Avoid relying exclusively on pointer-driven events, such as onmouseover, to provide functionality when scripting. Generally, such functionality will also require scripting for keyboard operability.
Language: Java
Framework: Selenium 4
1//Assuming the website under test is written in HTML23//Importing Selenium libraries4import org.openqa.selenium.*;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.chrome.ChromeOptions;78public class AccessibilityTesting {910 public static void main(String[] args) {11 12 //Configuring the ChromeOptions with desired capabilities for headless testing13 ChromeOptions options = new ChromeOptions();14 options.setCapability("browserName", "chrome");15 options.setCapability("enableVNC", true);16 options.setCapability("headless", true);17 18 //Creating a new Chrome driver with local driver executable path19 System.setProperty("webdriver.chrome.driver", "/path/to/local/driver/chromedriver"); //Assuming the driver executable is in the mentioned path20 WebDriver driver = new ChromeDriver(options);21 22 //Connecting to a remote client with desired capabilities in case of remote testing23 /*ChromeOptions options = new ChromeOptions();24 options.setCapability("browserName", "chrome");25 options.setCapability("enableVNC", true);26 options.setCapability("headless", true);27 28 //Assuming the remote client URL is 'http://10.20.30.40:4444/wd/hub' and access is granted29 WebDriver driver = new RemoteWebDriver(new URL("http://10.20.30.40:4444/wd/hub"), options);*/3031 //Assuming the website URL is 'https://www.example.com'32 driver.get("https://www.example.com");3334 //Locating all elements with 'onmouseover' attribute35 List<WebElement> mouseOverElements = driver.findElements(By.cssSelector("*[onmouseover]"));3637 //Iterating through all elements and checking if they have alternate keyboard access38 for (WebElement element : mouseOverElements) {39 if(!element.getAttribute("onkeypress").isEmpty()){40 //If 'onkeypress' attribute is present, then keyboard access is also available41 System.out.println("Element with 'onmouseover' attribute also supports keyboard operability");42 }else{43 //If 'onkeypress' attribute is not present, then keyboard access is not available44 System.out.println("Element with 'onmouseover' attribute does not support keyboard operability");45 }46 }4748 //Closing the driver session49 driver.quit();50 }51}
Language: Python
Framework: Selenium 4
1# Assuming the web application has a login page and user credentials are already inputted.2# Also assuming the web application has functionality that is triggered by onmouseover event.3# This test case aims to verify that this functionality can be triggered by keyboard operability as well.45# Import the necessary modules for Selenium 4 with Python6from selenium.webdriver import Firefox7from selenium.webdriver.common.keys import Keys8from selenium.webdriver.common.action_chains import ActionChains9from selenium.webdriver.firefox.options import Options1011# Set the options for the Firefox browser12options = Options()13options.headless = True # Set to True to avoid opening a browser window1415# Create a Firefox browser instance using the local driver16driver = Firefox(options=options)1718# Add commented code to connect to a remote client with desired capabilities19# Assuming that a remote client with the desired capabilities is already set up20# from selenium import webdriver21# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities22# capabilities = DesiredCapabilities.FIREFOX.copy()23# capabilities['platform'] = 'WINDOWS'24# capabilities['version'] = '10'25# driver = webdriver.Remote(26# command_executor='http://localhost:4444/wd/hub',27# desired_capabilities=capabilities28# )2930# Navigate to the web application login page31driver.get("https://www.example.com/login")3233# Input the user credentials34username = driver.find_element_by_name("username")35password = driver.find_element_by_name("password")36username.send_keys("example_username")37password.send_keys("example_password")38password.send_keys(Keys.RETURN)3940# Wait for the page to load and settle41driver.implicitly_wait(10)4243# Find the element that triggers the functionality and click on it using the mouse pointer44mouse_pointer = driver.find_element_by_id("mouse_pointer_element")45ActionChains(driver).move_to_element(mouse_pointer).click().perform()4647# Wait for the functionality to be triggered and settle48driver.implicitly_wait(10)4950# Verify that the functionality can also be triggered using keyboard operability51# Assuming that the functionality can be triggered using the Enter key52keyboard_operability = driver.find_element_by_id("keyboard_operability_element")53keyboard_operability.send_keys(Keys.RETURN)
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