Accessibility testing : Avoid pixel-based height and spacing

Avoid using pixels for defining the height and spacing (e.g., height, line height, etc.) of text boxes.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming we are testing on a web application2/​/​Assuming we are using Chrome browser3/​/​Assuming we have downloaded Selenium and ChromeDriver and added it to our project45import org.openqa.selenium.WebDriver;6import org.openqa.selenium.chrome.ChromeDriver;7import org.openqa.selenium.chrome.ChromeOptions;8import org.openqa.selenium.By;9import org.openqa.selenium.WebElement;1011public class AccessibilityTest {1213 public static void main(String[] args) {14 /​/​set Chrome driver path15 System.setProperty("webdriver.chrome.driver","path/​to/​chromedriver");1617 /​/​initialize ChromeOptions and disable using pixel-based height and spacing18 ChromeOptions options = new ChromeOptions();19 options.addArguments("disable-features=CSSTranslate");20 21 /​/​initialize ChromeDriver with ChromeOptions22 WebDriver driver = new ChromeDriver(options);2324 /​/​navigate to the website25 driver.get("https:/​/​www.example.com");2627 /​/​get the height and spacing of text boxes and verify they are not in pixels28 WebElement textbox = driver.findElement(By.xpath("/​/​input[@type='text']"));29 String height = textbox.getCssValue("height");30 String lineHeight = textbox.getCssValue("line-height");31 assert !height.contains("px") : "Height is in pixels!";32 assert !lineHeight.contains("px") : "Line height is in pixels!";3334 /​/​close the browser35 driver.quit();36 }37 38 /​/​Assuming we are connecting to remote client with desired capabilities39 /​*40 DesiredCapabilities cap = DesiredCapabilities.chrome();41 ChromeOptions options = new ChromeOptions();42 options.addArguments("--disable-gpu");43 cap.setCapability(ChromeOptions.CAPABILITY, options);44 WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), cap);45 */​46 47}

Language: Python

Framework: Selenium 4

copy
1from selenium import webdriver23# Assumptions:4# - The web application is accessible via a URL5# - The web application has text boxes with attributes height and line height6# - The text boxes are located on a web page that can be accessed using a web driver7# - The web driver is able to navigate to the text boxes on the web page89# Local driver initialization10driver = webdriver.Chrome()1112# Remote client initialization with desired capabilities:13'''14from selenium.webdriver.common.desired_capabilities import DesiredCapabilities15capabilities = DesiredCapabilities.CHROME.copy()16capabilities['platform'] = "WINDOWS"17capabilities['version'] = "10"18driver = webdriver.Remote(19 command_executor='http:/​/​127.0.0.1:4444/​wd/​hub',20 desired_capabilities=capabilities)21'''2223# Navigate to the web application24driver.get("https:/​/​example.com")2526# Find all text boxes on the web page and check their height and line height attributes27text_boxes = driver.find_elements_by_xpath("/​/​input[@type='text']")28for text_box in text_boxes:29 height = text_box.value_of_css_property("height")30 line_height = text_box.value_of_css_property("line-height")31 if "px" in height or "px" in line_height:32 print(f"Pixel-based height/​spacing detected for textbox with element ID: {text_box.get_attribute('id')}") 33 else:34 print(f"Height/​spacing of textbox with element ID: {text_box.get_attribute('id')} is not pixel-based") 35 36# Close the web driver37driver.quit()

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