When the focus changes, the page should not cause a change in page content, spawn a new browser window, submit a form, cause further change in focus, or cause any other change that disorients the user.
Language: Java
Framework: Selenium 4
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.chrome.ChromeOptions;67public class AccessibilityTesting {8 public static void main(String[] args) {9 //Assuming the website URL10 String url = "https://example.com";11 //Assuming the absolute path of the Chrome driver12 String driverPath = "/path/to/chromedriver.exe";13 14 //Creating a ChromeOptions object with desired capabilities15 ChromeOptions options = new ChromeOptions();16 options.setCapability("enableVNC", true);17 18 //Creating a new instance of the ChromeDriver with the ChromeOptions object19 WebDriver driver = new ChromeDriver(options);20 21 //Maximizing the browser window22 driver.manage().window().maximize();23 24 //Navigating to the website URL25 driver.get(url);26 27 //Assuming the ID of the element to be focused28 String elementID = "my-element";29 //Assuming the text of the element to be focused30 String elementText = "Some text";31 32 //Locating the element to be focused and storing it in a WebElement33 WebElement element = driver.findElement(By.id(elementID));34 35 //Printing the text of the element before focusing on it36 System.out.println("Text before focus change: " + element.getText());37 38 //Focusing on the element39 element.click();40 41 //Printing the text of the element after focusing on it42 System.out.println("Text after focus change: " + element.getText());43 44 //Assuming the expected behavior of the page content during focus change45 String expectedText = elementText;46 47 //Checking if the page content has changed during focus change48 if(expectedText.equals(element.getText())) {49 System.out.println("Test Passed: Stable page content during focus change");50 } 51 else {52 System.out.println("Test Failed: Page content changed during focus change");53 }54 55 //Closing the browser window56 driver.quit();57 }58}
Language: Python
Framework: Selenium 4
1#Assumptions: The Selenium 4 package is already installed and the Python environment is set up to use it. 2#The website has an input field that can be focused on. 3#The test will be run locally.45from selenium import webdriver6from selenium.webdriver.common.keys import Keys78#Local driver initialization9driver = webdriver.Chrome()1011#Remote client with desired capabilities connection12#Assuming remote server IP address = 123.456.789.0 and port number = 444413#browser_name = "chrome", platform = "linux"14#desired_capabilities = {"browserName": browser_name, "platform": platform}15#driver = webdriver.Remote(command_executor='http://123.456.789.0:4444/wd/hub', desired_capabilities=desired_capabilities)1617#Assuming website URL = "https://www.example.com"18driver.get("https://www.example.com")1920#Find an input field on the website21input_field = driver.find_element_by_id("inputField")2223#Check if the input field exists and is enabled24if input_field and input_field.is_enabled():2526 #Focus on the input field27 input_field.send_keys(Keys.TAB)2829 #Check if page content is stable30 if driver.execute_script("return document.readyState") == "complete":31 #print a success message32 print("Stable page content during focus change test passed.")33 else:34 #print error message if page content is not stable35 print("Error: Page content is not stable during focus change.")3637else:38 #print error message if input field cannot be found or is disabled39 print("Error: Input field not found or disabled.")
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