When a navigation menu is presented on multiple pages, the links should appear in the same order on each page.
Language: Java
Framework: Selenium 4
1//Assumptions: 2//1. Navigation menu is present on all pages of the application3//2. Navigation menu is in the same location (i.e. header, footer) on all pages4//3. Navigation menu contains the same links on all pages56import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.chrome.ChromeDriver;1011public class AccessibilityTesting {1213 public static void main(String[] args) {14 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");15 WebDriver driver = new ChromeDriver();16 17 //Connecting to Remote Client18 /*19 DesiredCapabilities capabilities = new DesiredCapabilities();20 capabilities.setBrowserName("chrome");21 capabilities.setVersion("77.0");22 capabilities.setCapability("platform", "Windows 10");23 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);24 */25 26 driver.get("https://www.example.com/home");27 28 WebElement navigationMenu = driver.findElement(By.id("navigationMenu"));29 List<WebElement> links = navigationMenu.findElements(By.tagName("a"));30 ArrayList<String> linkTexts = new ArrayList<String>();31 for (WebElement link : links){32 linkTexts.add(link.getText());33 }34 35 driver.get("https://www.example.com/about");36 37 WebElement navigationMenu2 = driver.findElement(By.id("navigationMenu"));38 List<WebElement> links2 = navigationMenu2.findElements(By.tagName("a"));39 ArrayList<String> linkTexts2 = new ArrayList<String>();40 for (WebElement link : links2){41 linkTexts2.add(link.getText());42 }43 44 if (linkTexts.equals(linkTexts2)){45 System.out.println("Consistent link order in navigation menus passed");46 } else {47 System.out.println("Consistent link order in navigation menus failed");48 }49 50 driver.quit();51 }5253}
Language: Python
Framework: Selenium 4
1from selenium.webdriver import Chrome2from selenium.webdriver.common.by import By3from selenium.webdriver.support.ui import WebDriverWait4from selenium.webdriver.support import expected_conditions as EC56#Assumptions:7#1. Navigation menu is a common component across all pages of the application.8#2. Navigation menu is identified by the same locator on all pages of the application.9#3. Navigation menu contains only clickable links and no other elements.10#4. Links in the navigation menu are identified by the same locator on all pages of the application.11#5. Navigation menu is loaded before the links in the menu are accessed.1213#Create a new instance of Chrome driver14driver = Chrome()1516#Navigate to the first page where the navigation menu is present17driver.get('https://www.example.com/page1')1819#Wait for the navigation menu to be visible20wait = WebDriverWait(driver, 10)21nav_menu = wait.until(EC.visibility_of_element_located((By.ID, 'nav_menu')))2223#Find all links in the navigation menu24nav_links = nav_menu.find_elements(By.CSS_SELECTOR, 'a')2526#Save the order of the links in the navigation menu as a list27nav_link_order = [link.text for link in nav_links]2829#Loop through all pages of the application where the navigation menu is present and compare the order of the links in the navigation menu with the saved order30pages = ['https://www.example.com/page2', 'https://www.example.com/page3']31for page in pages:32 driver.get(page)33 wait.until(EC.visibility_of_element_located((By.ID, 'nav_menu')))34 current_links = nav_menu.find_elements(By.CSS_SELECTOR, 'a')35 current_order = [link.text for link in current_links]36 assert nav_link_order == current_order, f'Link order in navigation menu is not consistent on page: {page}'3738#To connect to a remote client with desired capabilities, use the following commented code instead of the code above39'''40from selenium.webdriver.common.desired_capabilities import DesiredCapabilities4142#Create a DesiredCapabilities object with desired capabilities such as browser name and version, platform, etc.43desired_caps = DesiredCapabilities.CHROME.copy()44desired_caps['browserVersion'] = '91.0'45desired_caps['platformName'] = 'Windows 10'4647#Create a remote WebDriver instance with the specified capabilities48driver = webdriver.Remote(command_executor='http://0.0.0.0:4444/wd/hub',49 desired_capabilities=desired_caps)5051#Rest of the code remains the same52'''
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