Accessibility testing : Relative units for text and text containers

Define texts and text containers in relative units (percents, ems, rems) rather than in pixels.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming a web application with a login page23import org.openqa.selenium.WebDriver;4import org.openqa.selenium.chrome.ChromeDriver;56public class AccessibilityTesting {7 8 public static void main(String[] args) {9 10 /​/​setting up local driver for Chrome browser11 System.setProperty("webdriver.chrome.driver", "path/​to/​chromedriver");12 WebDriver driver = new ChromeDriver();13 14 /​/​navigating to the login page of the web application15 driver.navigate().to("https:/​/​example.com/​login");16 17 /​/​Assuming the font size to be 16 pixels, setting it as 100% for text and text containers18 /​/​For ems, it will be 1em (which is equivalent to the font size) and for rems, it will be 1rem19 20 /​/​checking relative units for text size21 22 /​/​getting the font size for a text element23 int fontSize = Integer.parseInt(driver.findElement(By.xpath("/​/​p")).getCssValue("font-size").replaceAll("\\D+",""));24 25 /​/​converting the font size from pixels to percent26 double fontSizePercent = ((double) fontSize/​16) * 100;27 28 /​/​asserting that the font size is set in percent29 assert (fontSizePercent == 100.0): "Font size is not set in relative units";30 31 32 /​/​checking relative units for text container size33 34 /​/​getting the width of a text container element35 int containerWidth = Integer.parseInt(driver.findElement(By.className("container")).getCssValue("width").replaceAll("\\D+",""));36 37 /​/​converting the width from pixels to percent38 double containerWidthPercent = ((double) containerWidth/​1920) * 100;39 40 /​/​asserting that the container width is set in percent41 assert (containerWidthPercent == 50.0): "Container width is not set in relative units";42 43 /​/​closing the driver connection44 driver.quit();45 46 /​*uncomment the below code to connect to remote client with desired capabilities47 48 /​/​Desired capabilities for Chrome browser49 DesiredCapabilities capabilities = DesiredCapabilities.chrome();50 51 /​/​Setting up Chrome options52 ChromeOptions options = new ChromeOptions();53 options.addArguments("--start-maximized");54 55 /​/​Setting the desired capabilities56 capabilities.setCapability(ChromeOptions.CAPABILITY, options);57 58 /​/​Connecting to remote client with desired capabilities59 WebDriver remoteDriver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);60 61 /​/​navigating to the login page of the web application62 remoteDriver.navigate().to("http:/​/​example.com/​login");63 64 /​/​rest of the code for testing goes here65 66 /​/​closing the remote driver connection67 remoteDriver.quit();68 */​69 }70 71}

Language: Python

Framework: Selenium 4

copy
1from selenium import webdriver23#Assuming the web application we are testing is built with HTML and CSS4#Assuming the text container's class name is 'textContainer' and the text's class name is 'text'56#local driver setup7driver = webdriver.Chrome()89#remote client setup with desired capabilities (to be implemented if needed)10#desired_capabilities = {'browserName': 'chrome', 'version': '91.0', 'platform': 'ANY'}11#driver = webdriver.Remote(command_executor='http:/​/​127.0.0.1:4444/​wd/​hub', desired_capabilities=desired_capabilities)1213#Navigate to the application URL14driver.get("http:/​/​www.example.com")1516#Finding the text container element and checking its font-size unit17text_container = driver.find_element_by_class_name("textContainer")18font_size_value = text_container.value_of_css_property("font-size")1920#Asserting that the font-size unit is not in pixels21assert "px" not in font_size_value, "Text container's font-size should not be in pixels"2223#Finding the text element and checking its font-size unit24text = driver.find_element_by_class_name("text")25font_size_value = text.value_of_css_property("font-size")2627#Asserting that the font-size unit is not in pixels28assert "px" not in font_size_value, "Text's font-size should not be in pixels"2930#Close the driver31driver.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