0
0
Testing Fundamentalstesting~15 mins

Test data management in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify user registration with valid test data
Preconditions (2)
Step 1: Open the user registration page
Step 2: Enter 'testuser1@example.com' in the email field
Step 3: Enter 'TestUser123!' in the password field
Step 4: Enter 'Test User' in the full name field
Step 5: Click the 'Register' button
✅ Expected Result: User is successfully registered and redirected to the welcome page with a confirmation message
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the URL is the welcome page URL after registration
Verify the confirmation message is displayed on the page
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use Page Object Model to separate page locators and actions
Use parameterized test data to allow easy changes
Avoid hardcoding test data inside the test script
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.email_input = (By.ID, 'email')
        self.password_input = (By.ID, 'password')
        self.fullname_input = (By.ID, 'fullname')
        self.register_button = (By.ID, 'register-btn')
        self.confirmation_message = (By.ID, 'confirmation-msg')

    def enter_email(self, email):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).send_keys(email)

    def enter_password(self, password):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.password_input)).send_keys(password)

    def enter_fullname(self, fullname):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.fullname_input)).send_keys(fullname)

    def click_register(self):
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.register_button)).click()

    def get_confirmation_message(self):
        return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.confirmation_message)).text

class TestUserRegistration(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/register')
        self.page = RegistrationPage(self.driver)

    def test_register_with_valid_data(self):
        test_data = {
            'email': 'testuser1@example.com',
            'password': 'TestUser123!',
            'fullname': 'Test User'
        }
        self.page.enter_email(test_data['email'])
        self.page.enter_password(test_data['password'])
        self.page.enter_fullname(test_data['fullname'])
        self.page.click_register()

        WebDriverWait(self.driver, 10).until(EC.url_contains('/welcome'))
        current_url = self.driver.current_url
        self.assertIn('/welcome', current_url, 'URL after registration should contain /welcome')

        confirmation_text = self.page.get_confirmation_message()
        self.assertEqual(confirmation_text, 'Registration successful!', 'Confirmation message should be displayed')

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

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

This script uses Selenium with Python to automate the user registration test case.

The RegistrationPage class follows the Page Object Model pattern. It stores locators and methods to interact with the registration page elements. This keeps the test code clean and easy to maintain.

The TestUserRegistration class contains the test method test_register_with_valid_data. It uses explicit waits to ensure elements are ready before interacting. The test inputs the prepared test data, clicks register, then verifies the URL and confirmation message.

Explicit waits avoid timing issues. Assertions check that the registration succeeded by URL and message. The test data is stored in a dictionary to avoid hardcoding inside the methods.

Finally, tearDown closes the browser after the test.

Common Mistakes - 3 Pitfalls
Hardcoding test data directly inside the test steps
Using time.sleep() instead of explicit waits
Locating elements inside test methods repeatedly
Bonus Challenge

Now add data-driven testing with 3 different sets of user registration data

Show Hint