If an input error is detected and if suggestions for correction are known, provide suggestions for fixing the submission.
Language: Java
Framework: Selenium 4
1//Assuming that the web application has a form with input fields and a submit button2//Assuming that the accessibility issues have been resolved and the form labels and input fields have proper accessibility tags for screen readers34//Code to create a new instance of the chrome driver and connect to a remote client with desired capabilities5WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.chrome());67//Navigate to the webpage with the input form8driver.get("https://www.example.com/input-form");910//Identify the input fields and submit button using their name or id attributes11WebElement firstNameInput = driver.findElement(By.name("firstName"));12WebElement lastNameInput = driver.findElement(By.name("lastName"));13WebElement emailInput = driver.findElement(By.name("email"));14WebElement submitButton = driver.findElement(By.id("submit-button"));1516//Enter incorrect values into the input fields and click on the submit button17firstNameInput.sendKeys("123");18lastNameInput.sendKeys("Smith");19emailInput.sendKeys("test@example");2021submitButton.click();2223//Check if an error message is displayed on the page24WebElement errorMessage = driver.findElement(By.id("error-message"));25if(errorMessage.getText().equals("Please enter a valid first name and email address.")){26 //Get the suggestions for correcting the input errors27 List<WebElement> suggestions = driver.findElements(By.className("suggestion"));28 29 //Loop through the suggestions and print them to the console30 for(WebElement suggestion : suggestions){31 System.out.println(suggestion.getText());32 }33}else{34 //If the error message is not displayed, fail the test and print an error message35 System.out.println("Error message not displayed.");36 Assert.fail("Error message not displayed.");37}3839//Close the browser window40driver.quit();
Language: Python
Framework: Selenium 4
1from selenium.webdriver import Chrome, ChromeOptions2from selenium.webdriver.common.desired_capabilities import DesiredCapabilities34# Assuming the web application is a form with input fields5# and a submit button6def test_input_error_correction():7 # Assume the web application URL8 url = "http://www.example.com/form"910 # Local Driver11 driver = Chrome()1213 # Remote client with desired capabilities14 #caps = DesiredCapabilities.CHROME.copy()15 # Replace with desired capabilities16 #driver = Chrome(command_executor='http://localhost:4444/wd/hub', desired_capabilities=caps)1718 # Navigate to URL19 driver.get(url)2021 # Fill in the input fields22 input_field_1 = driver.find_element_by_id("input1")23 input_field_2 = driver.find_element_by_id("input2")2425 # Assume the input error26 input_error = "Both fields cannot be left empty"2728 # Submit the form29 submit_button = driver.find_element_by_id("submit")30 submit_button.click()3132 # Verify if error message appears33 error_message = driver.find_element_by_id("error")34 assert error_message.text == input_error3536 # Assume the suggestions for correction37 suggestions = ["Please enter a value for input field 1", 38 "Please enter a value for input field 2"]3940 # Provide suggestions for correction41 if suggestions:42 suggestions_text = "\n".join(suggestions)43 suggestions_element = driver.find_element_by_id("suggestions")44 suggestions_element.text = suggestions_text4546 # Close the browser47 driver.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