Ensure that on each page, headings, landmark labels, and form labels are unique unless the structure provides adequate differentiation between them.
Language: Java
Framework: Selenium 4
1//Assumptions:2//1. The webpage has headings, landmarks and form controls.3//2. The webpage can be accessed locally or via a remote client with desired capabilities.4//3. Selenium 4 and Java are used as the automation testing framework and language, respectively.56//Code to use local driver:7import org.openqa.selenium.By;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10import org.openqa.selenium.chrome.ChromeDriver;1112public class UniqueLabelsTest {13 public static void main(String[] args) {14 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");15 WebDriver driver = new ChromeDriver();16 String url = "https://example.com";17 driver.get(url);18 19 //Verify unique labels for headings20 WebElement[] headings = driver.findElements(By.tagName("h1"), By.tagName("h2"), By.tagName("h3"), By.tagName("h4"), By.tagName("h5"), By.tagName("h6"));21 Set<String> headingsTextSet = new HashSet<String>();22 for(WebElement heading : headings) {23 String headingText = heading.getText().trim();24 if(!headingsTextSet.add(headingText)) {25 System.out.println("Error: Non-unique heading label found - " + headingText);26 }27 }2829 //Verify unique labels for landmarks30 WebElement[] landmarks = driver.findElements(By.tagName("nav"), By.tagName("main"), By.tagName("footer"), By.tagName("aside"));31 Set<String> landmarksTextSet = new HashSet<String>();32 for(WebElement landmark : landmarks) {33 String landmarkText = landmark.getAttribute("aria-label").trim();34 if(!landmarksTextSet.add(landmarkText)) {35 System.out.println("Error: Non-unique landmark label found - " + landmarkText);36 }37 }3839 //Verify unique labels for form controls40 WebElement[] formControls = driver.findElements(By.tagName("label"));41 Set<String> formControlsTextSet = new HashSet<String>();42 for(WebElement formControl : formControls) {43 String formControlText = formControl.getText().trim();44 if(!formControlsTextSet.add(formControlText)) {45 System.out.println("Error: Non-unique form control label found - " + formControlText);46 }47 }48 49 driver.quit();50 }51}5253//Code to connect to remote client with desired capabilities:54import org.openqa.selenium.WebDriver;55import org.openqa.selenium.remote.DesiredCapabilities;56import org.openqa.selenium.remote.RemoteWebDriver;57import java.net.URL;5859public class UniqueLabelsTest {60 public static void main(String[] args) throws Exception {61 DesiredCapabilities capabilities = new DesiredCapabilities();62 capabilities.setBrowserName("chrome");63 capabilities.setVersion("87.0");64 capabilities.setCapability("enableVNC", true); //enableVNC is just an example of a desired capability; you can add more desired capabilities as required.65 WebDriver driver = new RemoteWebDriver(new URL("http://remoteclienturl:port/wd/hub"), capabilities);66 String url = "https://example.com";67 driver.get(url);68 69 //Test case implementation same as before70 71 driver.quit();72 }73}
Language: Python
Framework: Selenium 4
1from selenium.webdriver import Chrome2from selenium.webdriver.chrome.service import Service3from selenium.webdriver.common.keys import Keys4from selenium.webdriver.chrome.options import Options5import time67#Assuming the web page has headings, landmarks and form controls89#Local driver10service = Service('/path/to/chromedriver')11options = Options()12options.add_argument('start-maximized')13driver = Chrome(service=service, options=options)1415#Remote driver with desired capabilities16#uncomment the following code if required17'''18desired_cap = {19 'browserName': 'Chrome',20 'loggingPrefs': {21 'browser': 'ALL',22 'driver': 'ALL',23 'performance': 'ALL',24 },25}26driver = webdriver.Remote(27 command_executor='http://localhost:4444/wd/hub',28 desired_capabilities=desired_cap)2930'''3132#Navigate to web page33driver.get("https://mywebpage.com")3435#Get headings, landmarks and form controls36headings = driver.find_elements_by_xpath('//h1 | //h2 | //h3 | //h4 | //h5 | //h6')37landmarks = driver.find_elements_by_xpath('//*[@role="main"] | //*[@role="navigation"] | //*[@role="search"] | //*[@role="banner"] | //*[@role="contentinfo"] | //*[@role="complementary"] | //*[@role="form"]')38form_labels = driver.find_elements_by_xpath('//label')3940#Check for unique labels41unique_headings = set(headings)42if len(headings) != len(unique_headings):43 print("Headings on the page are not unique")44 45unique_landmarks = set(landmarks)46if len(landmarks) != len(unique_landmarks):47 print("Landmarks on the page are not unique")4849unique_form_labels = set(form_labels)50if len(form_labels) != len(unique_form_labels):51 print("Form labels on the page are not unique")52 53#Close the browser54driver.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