Information filled out by users should remain intact when there is an error message on the page submitted. The user should be able to submit the form again by correcting the errors.
Language: Java
Framework: Selenium 4
1//Assuming that the webpage is a form-based application and user input is required2//Assuming that validation errors will be displayed on the same page on form submission34import org.openqa.selenium.WebDriver;5import org.openqa.selenium.chrome.ChromeDriver;6import org.openqa.selenium.By;78public class ErrorPersistenceTest {9 public static void main(String[] args) {1011 //Assuming that the driver path is configured12 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");1314 //Local driver instance15 WebDriver driver = new ChromeDriver();1617 //Assuming the webpage URL18 String webpage = "https://example.com/form";1920 //Assuming user input fields with IDs 'name', 'email', and 'phone'21 driver.get(webpage);22 driver.findElement(By.id("name")).sendKeys("John Doe");23 driver.findElement(By.id("email")).sendKeys("johndoe@example.com");24 driver.findElement(By.id("phone")).sendKeys("1234567890");2526 //Assuming form submission results in error validation on the same page27 driver.findElement(By.id("submit")).click();2829 //Checking if error message is displayed30 if(driver.findElements(By.id("validation-errors")).size() != 0) {31 //If error message is displayed, checking if user input fields remain intact32 String name = driver.findElement(By.id("name")).getAttribute("value");33 String email = driver.findElement(By.id("email")).getAttribute("value");34 String phone = driver.findElement(By.id("phone")).getAttribute("value");3536 //Correcting user input fields37 name = "Jane Doe";38 email = "janedoe@example.com";39 phone = "0987654321";4041 //Assuming button ID for resubmitting form after correction as 'resubmit'42 driver.findElement(By.id("resubmit")).click();4344 //Checking if form submission is successful45 if(driver.findElements(By.id("success-message")).size() != 0) {46 System.out.println("Test case passed - Error message persistence check is successful");47 }48 else {49 System.out.println("Test case failed - Error message persistence check is unsuccessful");50 }51 }52 else {53 System.out.println("Test case failed - Error message not displayed");54 }5556 //Assuming desired capabilities for remote client57 //Adding commented code to connect to remote client with desired capabilities58 /*59 ChromeOptions options = new ChromeOptions();60 options.setCapability("platform", "WINDOWS");61 options.setCapability("version", "10");62 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);63 */64 }65}
Language: Python
Framework: Selenium 4
1# Assumptions: 2# - The form is located at http://example.com/form3# - The error message element has the class name 'error-message'4# - The form submit button has the id 'submit-button'56from selenium import webdriver7from selenium.webdriver.common.keys import Keys8from selenium.webdriver.common.by import By9from selenium.webdriver.support.ui import WebDriverWait10from selenium.webdriver.support import expected_conditions as EC1112# Local driver setup13driver = webdriver.Chrome()1415# Remote client with desired capabilities16# options = webdriver.ChromeOptions()17# options.add_argument('--disable-extensions')18# options.add_argument('--headless')19# options.add_argument('--disable-gpu')20# capabilities = {'browserName': 'chrome', 'chromeOptions': {'args': ['--disable-extensions']}}21# driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities=capabilities)2223# Navigate to the form page24driver.get("http://example.com/form")2526# Fill out form with valid data27driver.find_element(By.NAME, "name").send_keys("John Smith")28driver.find_element(By.NAME, "email").send_keys("john.smith@example.com")29driver.find_element(By.NAME, "subject").send_keys("Test Subject")30driver.find_element(By.NAME, "message").send_keys("Test Message")3132# Submit the form with invalid data33driver.find_element(By.ID, "submit-button").click()3435# Wait for error message to appear36WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "error-message")))3738# Verify that form information is still intact39assert driver.find_element(By.NAME, "name").get_attribute("value") == "John Smith"40assert driver.find_element(By.NAME, "email").get_attribute("value") == "john.smith@example.com"41assert driver.find_element(By.NAME, "subject").get_attribute("value") == "Test Subject"42assert driver.find_element(By.NAME, "message").get_attribute("value") == "Test Message"4344# Correct the errors and resubmit the form45driver.find_element(By.NAME, "email").send_keys(Keys.CONTROL + "a")46driver.find_element(By.NAME, "email").send_keys("invalid-email") # invalid email will trigger error message47driver.find_element(By.ID, "submit-button").click()4849# Wait for confirmation message to appear after successful submission50WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.ID, "confirmation-message"), "Thank you for your submission."))5152# Close the driver53driver.quit()
Language: Javascript
Framework: Cypress
1//Assumptions:2//1. The webpage has a form with input fields and validation on submission.3//2. After submitting the form with invalid input and receiving an error message, the input fields are still visible with the previous values submitted by the user.4//3. The form can be resubmitted by editing the input fields and correcting the errors.56//Local Driver7describe('General webpage functionality', () => {8 it('Check error message persistence', () => {9 //Connect to local driver10 cy.visit('example.com')1112 //Fill out form with invalid input13 cy.get('#name').type('John')14 cy.get('#email').type('johndoecom')15 cy.get('#submit-button').click()1617 //Assert error message is displayed18 cy.get('.error-message').should('be.visible')1920 //Assert input fields have previous values submitted by the user21 cy.get('#name').should('have.value', 'John')22 cy.get('#email').should('have.value', 'johndoecom')2324 //Resubmit the form by correcting the errors25 cy.get('#email').clear().type('johndoe@example.com')26 cy.get('#submit-button').click()2728 //Assert success message is displayed29 cy.get('.success-message').should('be.visible')30 })31})3233//Remote Client with Desired Capabilities34describe('General webpage functionality', () => {35 it('Check error message persistence', () => {36 //Connect to remote client with desired capabilities37 cy.visit('example.com', {38 browser: 'chrome',39 platform: 'Windows 10',40 version: 'latest'41 })4243 //Fill out form with invalid input44 cy.get('#name').type('John')45 cy.get('#email').type('johndoecom')46 cy.get('#submit-button').click()4748 //Assert error message is displayed49 cy.get('.error-message').should('be.visible')5051 //Assert input fields have previous values submitted by the user52 cy.get('#name').should('have.value', 'John')53 cy.get('#email').should('have.value', 'johndoecom')5455 //Resubmit the form by correcting the errors56 cy.get('#email').clear().type('johndoe@example.com')57 cy.get('#submit-button').click()5859 //Assert success message is displayed60 cy.get('.success-message').should('be.visible')61 })62})
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