Use semantic markup to designate headings, lists, figures, emphasized text, etc.
Language: Java
Framework: Selenium 4
1//Assuming the web application to be tested is already open.2//Assuming the developer has already utilized semantic markup to specify headings, lists, figures, emphasized text, etc.34import org.openqa.selenium.WebDriver;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.remote.DesiredCapabilities;7import org.openqa.selenium.remote.RemoteWebDriver;8import java.net.URL;910public class AccessibilityTesting {11 public static void main(String[] args) throws Exception {12 //System Property for Chrome Driver 13 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");14 15 //Use local driver16 WebDriver driver = new ChromeDriver();17 18 //We can connect to a Remote WebDriver with Desired Capabilities by specifying the URL and Capabilities 19 /* DesiredCapabilities capabilities = DesiredCapabilities.chrome();20 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities); 21 */22 23 //Maximize the window24 driver.manage().window().maximize();25 26 //Navigate to the webpage27 driver.get("https://example.com");28 29 //Code to check semantic markup for headings, lists, figures, etc.30 //Use the findElements method to find all elements with semantic markup31 List<WebElement> headElements = driver.findElements(By.tagName("h1"));32 List<WebElement> listElements = driver.findElements(By.tagName("ul"));33 List<WebElement> figureElements = driver.findElements(By.tagName("figure"));34 List<WebElement> emElements = driver.findElements(By.tagName("em"));35 36 //Assert the presence of semantic markup37 Assert.assertEquals(headElements.size(), 1); //There should be only one H1 element marking a main heading38 Assert.assertFalse(listElements.isEmpty()); //There should be unordered lists marked by UL elements39 Assert.assertFalse(figureElements.isEmpty()); //There should be images marked by Figure elements40 Assert.assertFalse(emElements.isEmpty()); //There should be emphasized text marked by EM elements41 42 //Close the browser43 driver.close();44 }45}
Language: Python
Framework: Selenium 4
1Python Code:23# Assumptions: The application under test is a web application.4# We are using local web driver for running the test.5# We are testing the semantic markup of headings, lists, figures and emphasized text on a specific page.67from selenium import webdriver8from selenium.webdriver.common.by import By9from selenium.webdriver.support.ui import WebDriverWait10from selenium.webdriver.support import expected_conditions as EC1112# Set the ChromeDriver path13chromedriver_path = "/path/to/chromedriver"1415# Initialize Chrome driver16driver = webdriver.Chrome(chromedriver_path)1718# Launch the web application URL19driver.get("https://www.example.com")2021# Wait for the page to load completely22WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))2324# Check if H1 tag has semantic markup25h1_elem = driver.find_element(By.TAG_NAME, "h1")26assert h1_elem.get_attribute("role") == "heading"2728# Check if unordered list has semantic markup29ulist_elem = driver.find_element(By.TAG_NAME, "ul")30assert ulist_elem.get_attribute("role") == "list"3132# Check if figure has semantic markup33figure_elem = driver.find_element(By.TAG_NAME, "figure")34assert figure_elem.get_attribute("role") == "figure"3536# Check if emphasized text has semantic markup37em_elem = driver.find_element(By.TAG_NAME, "em")38assert em_elem.get_attribute("role") == "emphasis"3940# Uncomment the following code to run the test on a remote client with desired capabilities41'''42from selenium.webdriver.common.desired_capabilities import DesiredCapabilities4344# Set the desired capabilities45desired_caps = DesiredCapabilities.CHROME.copy()46desired_caps['platform'] = "WINDOWS"47desired_caps['version'] = "10"4849# Create a remote webdriver instance50driver = webdriver.Remote(command_executor='http://<remote_host>:4444/wd/hub/', desired_capabilities=desired_caps)51'''5253# Close the browser window54driver.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.
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