Accessibility testing : Avoid custom widgets when HTML elements suffice

Avoid creating custom widgets when HTML elements already exist. For example, use a and button tags appropriately.

Language: Java

Framework: Selenium 4

copy
1/​/​Assumptions:2/​/​1. The page layout is a web application3/​/​2. The application is running on the latest version of Selenium 44/​/​3. The page we are testing has custom widgets that we need to avoid if HTML elements are enough56import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.chrome.ChromeDriver;910import java.util.List;1112public class AccessibilityTest {13 public static void main(String[] args) {14 System.setProperty("webdriver.chrome.driver", "path/​to/​chromedriver.exe");15 WebDriver driver = new ChromeDriver();1617 /​/​Remote client connection with desired capabilities18 /​/​DesiredCapabilities capabilities = new DesiredCapabilities();19 /​/​capabilities.setBrowserName("chrome");20 /​/​capabilities.setVersion("93.0.4577.63");21 /​/​WebDriver remoteDriver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);2223 /​/​Navigate to the webpage24 driver.get("http:/​/​www.example.com");2526 /​/​Find all elements with custom widgets27 List<WebElement> customWidgets = driver.findElements(By.xpath("/​/​div[contains(@class,'custom-widget')]"));2829 /​/​Loop through all custom widgets to find HTML alternatives30 for (WebElement customWidget : customWidgets) {31 String tagName = customWidget.getTagName();3233 /​/​If custom widget is not a valid HTML tag, print a warning34 if (!(tagName.equalsIgnoreCase("a") || tagName.equalsIgnoreCase("button"))) {35 System.out.println("WARNING: Custom widget found that could be replaced by a standard HTML tag: " + tagName);36 }37 }3839 driver.quit();40 }41}

Language: Python

Framework: Selenium 4

copy
1#Assumptions:2#1. The website has relevant HTML elements, like a and button tags3#2. The local Selenium WebDriver is setup and the website is accessible4#3. Custom widgets are identified by the tester56#Import required libraries7from selenium import webdriver8from selenium.webdriver.common.keys import Keys910#Initialize and open driver11driver = webdriver.Chrome()12driver.get("https:/​/​website.com")1314#Locate custom widgets and verify their existence15custom_widgets = driver.find_elements_by_css_selector(".custom-widget")16if custom_widgets:17 print("Custom widgets found: ", custom_widgets)18else:19 print("No custom widgets found on the page")2021#Locate HTML elements, like a and button tags22html_elements = driver.find_elements_by_css_selector("a, button")23if html_elements:24 print("HTML elements found: ", html_elements)25else:26 print("No HTML elements found on the page")2728#Verify if custom widgets can be replaced with HTML elements29for widget in custom_widgets:30 for element in html_elements:31 if element.get_attribute("class") == widget.get_attribute("class"):32 print("Custom widget can be replaced with HTML element: ", element)33 #Add code to replace custom widget with HTML element34 break3536#Close driver37driver.close()3839#Commented code to connect to remote client with desired capabilities40"""41from selenium.webdriver.common.desired_capabilities import DesiredCapabilities4243#Set desired capabilities44desired_capabilities = DesiredCapabilities.CHROME.copy()45desired_capabilities['version'] = '84.0'46desired_capabilities['platform'] = 'WINDOWS'4748#Connect to remote client49driver = webdriver.Remote(50 command_executor='http:/​/​192.168.1.100:4444/​wd/​hub',51 desired_capabilities=desired_capabilities)52"""

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