logologo
  • Dashboard
  • Features
  • AI Tools
  • FAQs
  • Jobs
  • Modus
logologo

We source, screen & deliver pre-vetted developers—so you only interview high-signal candidates matched to your criteria.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Certifications
  • Topics
  • Collections
  • Articles
  • Services

AI Tools

  • AI Interviewer
  • Xperto AI
  • Pre-Vetted Top Developers

Procodebase © 2025. All rights reserved.

Q: Write a script to automate a UI test for form validation?

author
Generated by
ProCodebase AI

30/10/2024

Selenium

Automating UI tests for form validation can significantly enhance the reliability of your application. In this tutorial, we’ll be using Selenium, a powerful tool for web automation, to validate a web form. Let’s get started!

Prerequisites

Before we proceed, make sure you have the following:

  1. Python installed - Download from python.org.
  2. Selenium package - Install it via pip:
    pip install selenium
  3. Web driver - For example, if you are using Chrome, you'll need the ChromeDriver. Download it from chromedriver.chromium.org and ensure it's in your system PATH.

Setting Up Your Testing Environment

Assuming you have a sample web application with a form that includes:

  • A text field for the email address.
  • A password field.
  • A submit button.

Here’s how your basic form might look in HTML:

<form id="registrationForm"> <input type="text" id="email" placeholder="Email" required/> <input type="password" id="password" placeholder="Password" required/> <button type="submit">Register</button> </form>

Writing the Automated Test Script

Now, let’s write a Python script that uses Selenium to test this form validation.

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import time # Set up the Chrome driver driver = webdriver.Chrome() # Open the webpage with form driver.get("http://example.com/registration") # Replace with your form URL # Locate the email and password fields email_field = driver.find_element(By.ID, "email") password_field = driver.find_element(By.ID, "password") submit_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']") # Function to test form validation with various scenarios def test_form_validation(): # Test empty fields submit_button.click() time.sleep(1) # Wait for validation feedback assert "This field is required." in driver.page_source # Modify with your error message # Test invalid email format email_field.send_keys("invalid-email") password_field.send_keys("validPassword123") submit_button.click() time.sleep(1) # Wait for validation feedback assert "Please enter a valid email address." in driver.page_source # Modify as needed # Test valid submission email_field.clear() password_field.clear() email_field.send_keys("test@example.com") password_field.send_keys("validPassword123") submit_button.click() time.sleep(1) # Wait for feedback assert "Registration successful!" in driver.page_source # Change as required # Execute the tests test_form_validation() # Close the driver driver.quit()

Explanation of the Script

  1. Import Modules: We import the necessary modules from Selenium and time for handling delays.
  2. Setting Up the Driver: We initialize a Chrome web driver that will simulate user actions in the browser.
  3. Opening the Page: The browser navigates to the page containing the form.
  4. Element Locators: We locate the input fields and submit button using their IDs or CSS selectors.
  5. Test Function:
    • Empty Fields Validation: Click the submit button without entering anything and assert that the required field error message is displayed.
    • Invalid Email Format: Enter an invalid email, a valid password, and check for the specific error message.
    • Successful Submission: Enter valid data to check for a success message.
  6. Executing the Tests: We call the function to run all tests.
  7. Clean Up: Finally, we close the web driver to end the session.

By using this script, you ensure that your form validation works as expected, catching errors in user input before data submission. Remember to adjust the assertions and messages based on your actual application’s behavior. Happy testing!

Popular Tags

Seleniumautomation testingUI testing

Share now!

Related Questions

  • How would you test a UI for accessibility

    30/10/2024 | UI Manual Testing

  • Write a script to automate a UI test for form validation

    30/10/2024 | UI Manual Testing

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design