Organize pages using properly nested HTML headings.
Language: Java
Framework: Selenium 4
1//Assuming the web application is accessible via the URL assigned to "websiteURL"23import org.openqa.selenium.WebDriver;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.By;78public class AccessibilityTesting {9 public static void main(String[] args) {10 WebDriver driver = new ChromeDriver();11 driver.get(websiteURL);12 13 //Assuming there is a webpage we want to test and its code is assigned to "webpageCode"14 String webpageCode = driver.getPageSource();15 16 //Assuming there are heading tags(h1, h2, h3,....) on the webpage and we want to test only h1 headings17 int headcount = 0;18 int h1_count = 0;19 for (int i=0; i<webpageCode.length()-2; i++) {20 String substring = webpageCode.substring(i, i+2);21 if (substring.equals("<h")) {22 headcount++;23 String heading = webpageCode.substring(i+1, i+3);24 if (heading.equals("h1")) {25 h1_count++;26 }27 }28 }2930 if (headcount == h1_count) {31 System.out.println("Properly nested HTML headings testcase for h1 headings: PASS");32 } else {33 System.out.println("Properly nested HTML headings testcase for h1 headings: FAIL");34 }35 }36}37//To connect to remote client with desired capabilities, add the following comments as code:38//Assuming remote client's IP address is assigned to "clientIP"39//Assuming remote client's port number is assigned to "clientPort"40//Assuming desired capabilities are assigned to "capabilities"41//To connect to remote client, add the following code:42//import org.openqa.selenium.remote.DesiredCapabilities;43//import org.openqa.selenium.remote.RemoteWebDriver;44//...45//DesiredCapabilities capabilities = DesiredCapabilities.chrome();46//WebDriver driver = new RemoteWebDriver(new URL("http://" + clientIP + ":" + clientPort + "/wd/hub"), capabilities);
Language: Python
Framework: Selenium 4
1# Assumptions: 2# - The web page has headings from h1 to h6 elements3# - The headings are properly nested, with h1 being the top level heading and h6 being the lowest4# - The Selenium WebDriver is installed and properly configured56from selenium import webdriver7from selenium.webdriver.common.desired_capabilities import DesiredCapabilities89# For local driver10driver = webdriver.Firefox()11# For remote client with desired capabilities12# capabilities = DesiredCapabilities.FIREFOX.copy()13# capabilities['platform'] = 'WINDOWS'14# capabilities['version'] = '10'15# driver = webdriver.Remote(16# command_executor='http://REMOTE_SERVER:4444/wd/hub',17# desired_capabilities=capabilities18# )1920# Navigate to the web page to be tested21driver.get('https://example.com')2223# Find all the headings on the page24headings = driver.find_elements_by_xpath('//h1 | //h2 | //h3 | //h4 | //h5 | //h6')2526# Check that each heading is properly nested27for i in range(len(headings) - 1):28 current_heading_level = int(headings[i].tag_name[1])29 next_heading_level = int(headings[i+1].tag_name[1])30 if next_heading_level > current_heading_level:31 print("Error: Heading ", i+1, " is improperly nested.")32 # Assertion error can also be raised here if required 3334# Close the browser window35driver.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