Accessibility testing : Preserve focus during scripting

In general, avoid using scripts to remove focus from an element until the user moves focus manually.

Language: Java

Framework: Selenium 4

copy
1/​/​Assuming that the website has a login page with a username field, a password field and a login button.23import org.openqa.selenium.By;45import org.openqa.selenium.WebDriver;67import org.openqa.selenium.WebElement;89import org.openqa.selenium.chrome.ChromeDriver;1011public class AccessibilityTesting {1213public static void main(String[] args) {1415/​/​ Set the path of chromedriver.exe file here:1617System.setProperty("webdriver.chrome.driver", "C:/​Path/​To/​chromedriver.exe");1819WebDriver driver = new ChromeDriver();2021driver.manage().window().maximize();2223/​/​Assuming that the website URL is "https:/​/​www.example.com"2425driver.get("https:/​/​www.example.com/​login");2627/​/​Find the username field element2829WebElement username = driver.findElement(By.id("username"));3031/​/​Set the value in the username field3233username.sendKeys("testuser");3435/​/​Find the password field element3637WebElement password = driver.findElement(By.id("password"));3839/​/​Set the value in the password field4041password.sendKeys("testpassword");4243/​/​Find the login button element4445WebElement loginButton = driver.findElement(By.id("login-button"));4647/​/​Click on the login button4849loginButton.click();5051/​/​Assuming that there is a dashboard page after successful login with user name displayed on top:5253/​/​Find the user name element5455WebElement usernameDisplay = driver.findElement(By.id("username-display"));5657/​/​Assuming that the script tries to remove focus from the user name element automatically:5859/​/​Check if the user name element has focus before running the script6061if (driver.switchTo().activeElement() == usernameDisplay) {6263/​/​Run the script to remove focus from the user name element6465/​/​Assuming that the script contains code such as: usernameDisplay.blur();6667/​/​Check if the user has moved focus manually6869if (driver.switchTo().activeElement() != usernameDisplay) {7071/​/​Test Passes7273System.out.println("Test Passed: Focus preserved during scripting");7475}7677else {7879/​/​Test Fails8081System.out.println("Test Failed: Focus not preserved during scripting");8283}8485}8687/​/​Close the browser8889driver.quit();9091}9293/​/​Assuming that we can add the following code to connect to a remote client with desired capabilities:9495/​/​Assuming that the remote client's IP address is "192.168.0.10" and the port number is "4444"9697/​/​import org.openqa.selenium.remote.DesiredCapabilities;9899/​/​import org.openqa.selenium.remote.RemoteWebDriver;100101/​/​import java.net.URL;102103/​/​DesiredCapabilities capabilities = DesiredCapabilities.chrome();104105/​/​WebDriver driver = new RemoteWebDriver(new URL("http:/​/​192.168.0.10:4444/​wd/​hub"), capabilities);

Language: Python

Framework: Selenium 4

copy
1#Assumptions:2#1. The web application being tested is written in HTML, CSS, and JavaScript.3#2. The development team has not implemented any functionality in the application that would automatically move the focus of the user away from a field if they have not manually selected another field.4#3. The automated test will be run locally on the machine.56#Import required libraries7from selenium import webdriver8from selenium.webdriver.common.keys import Keys9from selenium.webdriver.support.ui import WebDriverWait10from selenium.webdriver.support import expected_conditions as EC11from selenium.webdriver.common.by import By1213#Create a new instance of the Chrome driver14driver = webdriver.Chrome()1516#To connect to remote client with desired capabilities, uncomment the following code and update the URL with appropriate values17#remote_client = "http:/​/​remote-ip-address:port/​wd/​hub"18#desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()19#driver = webdriver.Remote(command_executor=remote_client, desired_capabilities=desired_capabilities)2021#Navigate to the application URL22driver.get("https:/​/​example.com")2324#Assuming an input field exists on the page, locate it and send keys to it25input_field = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "input-field-id")))26input_field.send_keys("Test Input")2728#Assuming there is a button on the page that triggers a script, locate it and click on it29script_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "script-button-id")))30script_button.click()3132#Assuming the focus is expected to remain on the input field, check if it is still active33focused_element = driver.switch_to.active_element3435#Assert whether or not the input field still has focus36assert input_field == focused_element3738#Close the browser window39driver.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.

Accelerate Your Automation Test Cycles With LambdaTest

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.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing