When components have the same functionality across several web pages, the components are labeled consistently on each page.
Language: Java
Framework: Selenium 4
1WebDriver.23//Assuming website application is developed using React.js45//Importing required packages6import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.chrome.ChromeDriver;1011public class AccessibilityTesting {12 public static void main(String[] args) {13 14 //Assuming website URL15 String websiteUrl = "https://example.com";16 17 //Setting up local driver for Chrome browser18 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");19 WebDriver driver = new ChromeDriver();20 21 //Connecting to remote client using desired capabilities22 //DesiredCapabilities capabilities = DesiredCapabilities.safari();23 //WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);24 25 //Assuming repeated component locator26 By repeatedComponentLocator = By.cssSelector(".component-class");27 28 try {29 //Loading website30 driver.get(websiteUrl);31 32 //Assuming multiple web pages having repeated component33 String[] pageUrls = {"https://example.com/page1", "https://example.com/page2", "https://example.com/page3"};34 35 //Iterating through all page URLs and checking for consistent labeling36 for(String pageUrl : pageUrls) {37 driver.get(pageUrl);38 WebElement repeatedComponent = driver.findElement(repeatedComponentLocator);39 String componentLabel = repeatedComponent.getText();40 for(int i=0; i<pageUrls.length; i++) {41 if(i != pageUrls.indexOf(pageUrl)) {42 driver.get(pageUrls[i]);43 WebElement componentOnOtherPage = driver.findElement(repeatedComponentLocator);44 String otherLabel = componentOnOtherPage.getText();45 if(!componentLabel.equals(otherLabel)) {46 System.out.println("Test case failed: Inconsistent labeling for repeated component");47 return;48 }49 }50 }51 }52 System.out.println("Test case passed: Consistent labeling for repeated components");53 }54 catch(Exception e) {55 System.out.println("Exception occurred: " + e.getMessage());56 }57 finally {58 //Closing the driver59 driver.quit();60 }61 }62}
Language: Python
Framework: Selenium 4
1from selenium.webdriver import Chrome2from selenium.webdriver.chrome.service import Service3from selenium.webdriver.common.by import By4from selenium.webdriver.support.ui import WebDriverWait5from selenium.webdriver.support import expected_conditions as EC67#Assumption: The website has a navigational header which contains the repeated components8#Assumption: The individual components have a unique identifier such as a class or ID910#Set up local driver11service = Service('/path/to/chromedriver')12driver = Chrome(service=service)1314#Connect to remote client with desired capabilities (commented out)15#desired_cap = {'browserName': 'chrome', 'version': '91.0'}16#driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',17# desired_capabilities=desired_cap)1819#Navigate to the first web page with component20driver.get('http://website.com/page1')2122#Find the repeated component and get its label23component_label = WebDriverWait(driver, 10).until(24 EC.presence_of_element_located((By.ID, 'component1'))).text2526#Navigate to subsequent web pages and check for consistent labeling27for i in range(2,5):28 driver.get('http://website.com/page' + str(i))2930 #Find the repeated component and get its label31 new_component_label = WebDriverWait(driver, 10).until(32 EC.presence_of_element_located((By.ID, 'component1'))).text3334 #Assert that the label is consistent with the first web page35 assert component_label == new_component_label, "Component label is not consistent"3637#Close the driver38driver.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