Accessibility testing : Avoid triggering functionality on down-events

Avoid triggering functionality on down-events, such as onmousedown. Use events such as onclick instead.

Language: Java

Framework: Selenium 4

copy
1/​/​ Assumptions:2/​/​ 1. The web application is currently running on localhost3/​/​ 2. The web application has a button with an ID of 'button'45import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.chrome.ChromeDriver;910public class AccessibilityTesting {11 12 private static WebDriver driver;13 14 public static void main(String[] args) {15 /​/​ Set the system property for the Chrome driver16 System.setProperty("webdriver.chrome.driver", "path/​to/​chrome/​driver");17 18 /​/​ Create a new instance of the Chrome driver19 driver = new ChromeDriver();20 21 /​/​ Navigate to the web application22 driver.get("http:/​/​localhost:8080/​myapp");23 24 /​/​ Find the button element and click on it using the 'onclick' event25 WebElement button = driver.findElement(By.id("button"));26 button.click();27 28 /​/​ Check if any functionality was triggered on the 'onmousedown' event29 String elementAttributes = button.getAttribute("outerHTML");30 if (elementAttributes.contains("onmousedown")) {31 System.out.println("Functionality triggered on 'onmousedown' event.");32 } else {33 System.out.println("No functionality triggered on 'onmousedown' event.");34 }35 36 /​/​ Close the browser window37 driver.quit();38 39 /​/​ Remote client connection can be achieved by adding the following lines of code after creating the driver instance:4041 /​/​ DesiredCapabilities desiredCapabilities = new DesiredCapabilities();42 /​/​ desiredCapabilities.setBrowserName(BrowserType.CHROME);43 /​/​ WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), desiredCapabilities);44 }45}

Language: Python

Framework: Selenium 4

copy
1#Assumptions:2#1. Application is a web-based application.3#2. The element to be tested has onmousedown event attached to it.4#3. The behaviour of the element is expected to change on onmousedown event.56#Code to test accessibility of a web-based application using Selenium 4 Python framework:78from selenium import webdriver9from selenium.webdriver.common.keys import Keys101112#Code to use local driver13driver = webdriver.Chrome() #Assuming Chrome is installed141516#Code to connect to remote client with desired capabilities - Uncomment the below code and add remote web driver URL and desired capabilities as needed1718# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities19# caps = DesiredCapabilities.CHROME.copy()20# caps['platform'] = "WINDOWS"21# caps['version'] = "10"22# driver = webdriver.Remote(desired_capabilities=caps,23# command_executor="http:/​/​<REMOTE_WEB_DRIVER_URL>:4444/​wd/​hub")242526#Navigate to the webpage containing the element to be tested27driver.get("https:/​/​www.example.com")2829#Find the element on the webpage using its CSS selector30elem = driver.find_element_by_css_selector("<CSS_SELECTOR_OF_THE_ELEMENT>")3132#Get the value of onmousedown attribute attached to the element33onmousedown_val = elem.get_attribute("onmousedown")3435#Check if onmousedown attribute is present and not empty36if onmousedown_val and len(onmousedown_val)>0:37 #If onmousedown attribute is present, update the attribute with onclick attribute value38 elem.execute_script("arguments[0].setAttribute('onclick',arguments[0].getAttribute('onmousedown'));",39 elem)40 elem.execute_script("arguments[0].removeAttribute('onmousedown');",41 elem)42 43#Click on the element to test the updated attribute value44elem.click()4546#Close the driver instance47driver.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