Accessibility testing : Accessible content on hover and focus

For content that appears on hover and focus: the content should be dismissible with the escape key; the content itself can be hovered over; and the content remains available unless it is dismissed, it is no longer relevant, or the user removes hover and focus.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming the website is already opened2import org.openqa.selenium.By;3import org.openqa.selenium.Keys;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;78public class AccessibilityTest {9 public static void main(String[] args) {10 System.setProperty("webdriver.chrome.driver", "chromedriver.exe");11 WebDriver driver = new ChromeDriver();12 13 /​/​Navigate to the website14 driver.navigate().to("https:/​/​www.example.com");15 16 /​/​Assuming the content appears on hover and focus17 WebElement content = driver.findElement(By.xpath("/​/​div[@class='content']"));18 19 /​/​Hover over the content20 Actions actions = new Actions(driver);21 actions.moveToElement(content).perform();22 23 /​/​Verify that the content is available24 if(content.isDisplayed()) {25 System.out.println("Content is available");26 }27 28 /​/​Assuming the content can be dismissed with the escape key29 actions.sendKeys(Keys.ESCAPE).perform();30 if(!content.isDisplayed()) {31 System.out.println("Content is dismissed with the escape key");32 }33 34 /​/​Assuming the content remains available unless it is dismissed, it is no longer relevant, or the user removes hover and focus35 actions.moveAwayFromElement(content).perform();36 if(content.isDisplayed()) {37 System.out.println("Content remains available");38 }39 40 /​/​Assuming remote capabilities41 /​*42 DesiredCapabilities caps = new DesiredCapabilities();43 caps.setCapability("browserName", "chrome");44 caps.setCapability("platformName", "Windows 10");45 caps.setCapability("browserVersion", "latest");46 47 RemoteWebDriver remoteDriver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), caps);48 remoteDriver.navigate().to("https:/​/​www.example.com");49 */​50 51 /​/​Close the browser52 driver.close();53 }54}

Language: Python

Framework: Selenium 4

copy
1# Assumptions:2# - The website under test has content that appears on hover and focus.3# - The content can be dismissed with the escape key.4# - The content disappears when no longer relevant or when the user removes hover and focus.56# Import required libraries7import time8from selenium import webdriver9from selenium.webdriver.common.keys import Keys10from selenium.webdriver.common.action_chains import ActionChains1112# Set up driver13driver = webdriver.Chrome('path/​to/​chromedriver')1415# Navigate to the website16driver.get('https:/​/​www.example.com')1718# Find element that appears on hover and focus19element = driver.find_element_by_id('element-id')2021# Hover over the element22hover = ActionChains(driver).move_to_element(element)23hover.perform()2425# Verify that content appears26assert driver.find_element_by_id('content-id').is_displayed()2728# Verify that content can be dismissed with the escape key29escape = Keys.ESCAPE30element.send_keys(escape)31time.sleep(1) # Wait for content to disappear32assert not driver.find_element_by_id('content-id').is_displayed()3334# Verify that content remains available unless it is dismissed, it is no longer relevant,35# or the user removes hover and focus36hover.perform()37assert driver.find_element_by_id('content-id').is_displayed()3839# Clean up40driver.quit()4142# To use remote client with desired capabilities, replace the driver setup code43# with the following:44# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities45# capabilities = DesiredCapabilities.CHROME46# capabilities['version'] = 'latest'47# capabilities['platform'] = 'WINDOWS'48# driver = webdriver.Remote(command_executor='http:/​/​localhost:4444/​wd/​hub',49# desired_capabilities=capabilities)

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