Avoid using whitespace characters for layout purposes.
Language: Java
Framework: Selenium 4
1//Assuming the web page to test is loaded in a local driver using Chrome Browser23import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;78public class AccessibilityTesting {9 public static void main(String[] args) {10 System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");1112 //Create a new instance of ChromeDriver13 WebDriver driver = new ChromeDriver();1415 //Navigate to the website16 driver.get("https://www.example.com");1718 //Find all elements with whitespace characters19 List<WebElement> whitespaceElements = driver.findElements(By.xpath("//*[contains(text(), ' ')]"));2021 //Assert that there are no whitespace characters used for layout22 assert(whitespaceElements.size() == 0);2324 //Close the driver25 driver.quit();26 }27 28 //Assuming connection to remote client with desired capabilities is done through the following code29 //Replace the "remoteURL" and "capabilities" with the actual values30 31 //import org.openqa.selenium.remote.DesiredCapabilities;32 //import org.openqa.selenium.remote.RemoteWebDriver;33 //import java.net.URL;34 35 //public class AccessibilityTesting {36 // public static void main(String[] args) throws Throwable{37 // DesiredCapabilities capabilities = DesiredCapabilities.chrome();38 // WebDriver driver = new RemoteWebDriver(new URL("remoteURL"), capabilities);39 // driver.get("https://www.example.com");40 // //Rest of the code remains the same41 // driver.quit();42 // }43 //}44}
Language: Python
Framework: Selenium 4
1# Assume that the application being tested is a web application and we are using Selenium 4 with the Python programming language23# Import required libraries4from selenium.webdriver import Chrome5from selenium.webdriver.common.by import By6from selenium.webdriver.support.ui import WebDriverWait7from selenium.webdriver.support import expected_conditions as EC89# Create Chrome driver object10driver = Chrome()1112# Set maximum time limit for each test step13wait = WebDriverWait(driver, 10)1415# Navigate to the application URL16driver.get("http://example.com")1718# Verify whitespace characters are not used for layout19assert not driver.find_elements(By.CSS_SELECTOR, "body *:empty")2021# Close the browser window after the test is complete22driver.quit()2324# Uncomment the below code to connect to a remote client with desired capabilities25# from selenium import webdriver26# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities27# 28# # Define the desired capabilities29# desired_capabilities = DesiredCapabilities.CHROME.copy()30# desired_capabilities["version"] = "latest"31# desired_capabilities["platform"] = "WINDOWS"32# 33# # Create a new instance of the Chrome driver with the desired capabilities34# driver = webdriver.Remote(35# command_executor="http://localhost:4444/wd/hub",36# desired_capabilities=desired_capabilities37# )
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.
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.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing