Test data management in Testing Fundamentals - Build an Automation Script
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.
Now add data-driven testing with 3 different sets of user registration data
Practice
Solution
Step 1: Understand the role of test data management
Test data management is about handling the data used during testing to ensure tests run smoothly and reliably.Step 2: Differentiate from other testing activities
Writing test cases, fixing bugs, and designing UI are separate tasks from managing test data.Final Answer:
To organize and maintain data needed for testing -> Option CQuick Check:
Test data management = Organize test data [OK]
- Confusing test data management with writing test cases
- Thinking it involves fixing bugs
- Mixing it up with UI design
Solution
Step 1: Identify the CSV format rules
CSV files use commas to separate values, and text fields are often enclosed in quotes.Step 2: Check each option for correct CSV syntax
"username","password","email"\nuser1,pass123,user1@example.com uses commas and quotes correctly. Options A, B, and D use spaces, semicolons, or pipes, which are not standard CSV separators.Final Answer:
"username","password","email"\nuser1,pass123,user1@example.com -> Option AQuick Check:
CSV uses commas and quotes [OK]
- Using semicolons or pipes instead of commas
- Not enclosing text fields in quotes when needed
- Using spaces as separators
test_data = [
{'id': 1, 'name': 'Alice'},
{'id': 2, 'name': 'Bob'},
{'id': 3, 'name': 'Charlie'}
]
names = [user['name'] for user in test_data if user['id'] > 1]
print(names)Solution
Step 1: Understand the list comprehension condition
The code selects names where 'id' is greater than 1, so it excludes the entry with id 1 (Alice).Step 2: Identify which names meet the condition
Users with id 2 and 3 are Bob and Charlie, so the list will be ['Bob', 'Charlie'].Final Answer:
['Bob', 'Charlie'] -> Option BQuick Check:
Filter id > 1 gives Bob and Charlie [OK]
- Including all names without filtering
- Confusing id comparison operators
- Misreading list comprehension syntax
INSERT INTO users (id, username, email) VALUES (1, 'testuser', testuser@example.com);What is the error and how to fix it?
Solution
Step 1: Identify syntax error in SQL query
The email value is a string and must be enclosed in single quotes in SQL.Step 2: Correct the email value by adding quotes
Change testuser@example.com to 'testuser@example.com' to fix the syntax error.Final Answer:
The email value is missing quotes; fix by adding quotes around 'testuser@example.com' -> Option AQuick Check:
String values need quotes in SQL [OK]
- Forgetting quotes around string values
- Changing table names unnecessarily
- Misusing SQL keywords
Solution
Step 1: Understand the need for unique test data
Unique emails prevent conflicts and errors when tests run multiple times.Step 2: Evaluate approaches for uniqueness
Generating emails with timestamps ensures uniqueness automatically, avoiding manual errors or conflicts.Final Answer:
Use a script that generates a new unique email each time by appending a timestamp -> Option DQuick Check:
Automate unique data generation to avoid conflicts [OK]
- Reusing fixed emails causing test failures
- Resetting database is slow and error-prone
- Ignoring uniqueness leads to conflicts
Ready to prove it?
Pass the test and this pattern moves unknown
