Use semantic, descriptive labels for inputs. Visually position labels in a consistent way that makes associating labels with form controls easy. Do not rely on placeholder text in lieu of an HTML label.
Language: Java
Framework: Selenium 4
1//Assumptions: The web application is developed using HTML, CSS, and Javascript.2//The form input elements have 'id' and 'name' attributes.3//The labels for each form input element have 'for' attribute with the value of the input element's 'id' attribute.4//The web page uses Bootstrap framework, which provides CSS classes for styling forms.56import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.chrome.ChromeDriver;10import org.openqa.selenium.remote.DesiredCapabilities;11import org.openqa.selenium.remote.RemoteWebDriver;1213public class AccessibilityTesting {14 public static void main(String[] args) {15 //Local WebDriver instance16 WebDriver driver = new ChromeDriver();1718 //Connect to Remote Client with Desired Capabilities19 /*20 DesiredCapabilities capabilities = new DesiredCapabilities();21 capabilities.setCapability("browserName", "chrome");22 capabilities.setCapability("version", "91.0");23 capabilities.setCapability("enableVNC", true);24 WebDriver driver = new RemoteWebDriver(25 URI.create("https://your-remote-client.com/wd/hub").toURL(),26 capabilities27 );28 */2930 //Navigate to a web page with form inputs31 driver.navigate().to("https://yourwebapp.com/form");3233 //Get all input elements on the form34 List<WebElement> inputElements = driver.findElements(By.tagName("input"));3536 for (WebElement input : inputElements) {37 //Verify each input element has a label38 String labelForAttribute = input.getAttribute("for");39 Assert.assertNotNull("Input " + input.getAttribute("id") + " does not have label", labelForAttribute);4041 //Verify the label is positioned correctly and uses descriptive text42 WebElement labelElement = driver.findElement(By.id(labelForAttribute));43 String labelPositionClass = labelElement.getAttribute("class");44 Assert.assertTrue("Label " + labelForAttribute + " is not positioned correctly", labelPositionClass.contains("form-label"));45 Assert.assertNotEquals("Label " + labelForAttribute + " does not have descriptive text", labelElement.getText(), "");4647 //Verify placeholder text is not used in place of label48 String inputPlaceholderText = input.getAttribute("placeholder");49 Assert.assertTrue("Input " + input.getAttribute("id") + " uses placeholder text instead of label", inputPlaceholderText == null || inputPlaceholderText == "");50 }5152 //Close the WebDriver instance53 driver.quit();54 }55}
Language: Python
Framework: Selenium 4
1from selenium import webdriver2from selenium.webdriver.common.keys import Keys3from selenium.webdriver.chrome.options import Options45# Assumptions: We are testing a web application with a form that requires user input. The form has input fields such as name, email, phone number, etc.67# Set the options to run the test on a remote client with desired capabilities8chrome_options = Options()9chrome_options.add_argument("--headless")10chrome_options.add_argument("--disable-dev-shm-usage")11chrome_options.add_argument("--no-sandbox")12chrome_options.add_argument("--disable-gpu")13chrome_options.add_argument("--remote-debugging-port=9222")1415# Start the local driver16driver = webdriver.Chrome(options=chrome_options)1718# Navigate to the web application url19driver.get("http://www.example.com")2021# Find all form input fields and their labels22input_fields = driver.find_elements_by_xpath("//input[@type='text']|//input[@type='email']|//input[@type='tel']")23for field in input_fields:24 label = field.find_element_by_xpath("./preceding-sibling::label")25 # Compare the label text to the expected semantic and descriptive label26 if label.text == "Full Name":27 # Enter the name into the input field28 field.send_keys("John Doe")29 elif label.text == "Email Address":30 # Enter the email into the input field31 field.send_keys("johndoe@example.com")32 elif label.text == "Phone Number":33 # Enter the phone number into the input field34 field.send_keys("1234567890")3536# Submit the form37submit_button = driver.find_element_by_xpath("//button[@type='submit']")38submit_button.click()3940# Close the browser session41driver.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