0
0
Testing Fundamentalstesting~15 mins

Data validation rules in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Validate user registration form data
Preconditions (1)
Step 1: Enter 'John' in the First Name field
Step 2: Enter 'Doe' in the Last Name field
Step 3: Enter 'john.doe@example.com' in the Email field
Step 4: Enter 'Password123!' in the Password field
Step 5: Enter 'Password123!' in the Confirm Password field
Step 6: Click the Submit button
✅ Expected Result: Form submits successfully and confirmation message 'Registration successful' is displayed
Automation Requirements - Selenium with Python
Assertions Needed:
Verify that the confirmation message 'Registration successful' is displayed after submission
Verify that no validation error messages are shown
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use meaningful locators such as By.ID or By.NAME
Use Page Object Model to separate page structure from test logic
Automated Solution
Testing Fundamentals
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class RegistrationPage:
    def __init__(self, driver):
        self.driver = driver
        self.first_name = driver.find_element(By.ID, 'firstName')
        self.last_name = driver.find_element(By.ID, 'lastName')
        self.email = driver.find_element(By.ID, 'email')
        self.password = driver.find_element(By.ID, 'password')
        self.confirm_password = driver.find_element(By.ID, 'confirmPassword')
        self.submit_button = driver.find_element(By.ID, 'submitBtn')

    def fill_form(self, first, last, email, pwd, confirm_pwd):
        self.first_name.clear()
        self.first_name.send_keys(first)
        self.last_name.clear()
        self.last_name.send_keys(last)
        self.email.clear()
        self.email.send_keys(email)
        self.password.clear()
        self.password.send_keys(pwd)
        self.confirm_password.clear()
        self.confirm_password.send_keys(confirm_pwd)

    def submit(self):
        self.submit_button.click()

class TestRegistration(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/registration')
        self.wait = WebDriverWait(self.driver, 10)
        self.page = RegistrationPage(self.driver)

    def test_valid_registration(self):
        self.page.fill_form('John', 'Doe', 'john.doe@example.com', 'Password123!', 'Password123!')
        self.page.submit()

        confirmation = self.wait.until(
            EC.visibility_of_element_located((By.ID, 'confirmationMessage'))
        )
        self.assertEqual(confirmation.text, 'Registration successful')

        # Verify no validation errors
        errors = self.driver.find_elements(By.CLASS_NAME, 'validation-error')
        self.assertEqual(len(errors), 0)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

The RegistrationPage class uses the Page Object Model to keep element locators and actions together. This makes the test easier to maintain.

In test_valid_registration, we fill the form with valid data and submit it.

We use WebDriverWait with visibility_of_element_located to wait for the confirmation message to appear, ensuring the test does not fail due to timing issues.

Assertions check that the confirmation message text is exactly 'Registration successful' and that no validation error messages are present.

The tearDown method closes the browser after the test.

Common Mistakes - 3 Pitfalls
Using hardcoded sleeps like time.sleep() instead of explicit waits
Using brittle locators like absolute XPath
Mixing test logic and page structure in the test method
Bonus Challenge

Now add data-driven testing with 3 different sets of valid user data to verify the registration form works for multiple inputs.

Show Hint