Data integrity checks 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 ProfilePage: def __init__(self, driver): self.driver = driver self.first_name_input = (By.ID, 'firstName') self.last_name_input = (By.ID, 'lastName') self.email_input = (By.ID, 'email') self.save_button = (By.ID, 'saveProfile') self.first_name_display = (By.ID, 'displayFirstName') self.last_name_display = (By.ID, 'displayLastName') self.email_display = (By.ID, 'displayEmail') def enter_first_name(self, first_name): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.first_name_input)).clear() self.driver.find_element(*self.first_name_input).send_keys(first_name) def enter_last_name(self, last_name): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.last_name_input)).clear() self.driver.find_element(*self.last_name_input).send_keys(last_name) def enter_email(self, email): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).clear() self.driver.find_element(*self.email_input).send_keys(email) def click_save(self): WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.save_button)).click() def get_displayed_first_name(self): return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.first_name_display)).text def get_displayed_last_name(self): return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.last_name_display)).text def get_displayed_email(self): return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_display)).text class TestDataIntegrity(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/profile/edit') self.profile_page = ProfilePage(self.driver) def test_data_integrity_after_save(self): # Enter user data self.profile_page.enter_first_name('John') self.profile_page.enter_last_name('Doe') self.profile_page.enter_email('john.doe@example.com') # Save the profile self.profile_page.click_save() # Navigate to profile view page self.driver.get('https://example.com/profile/view') # Verify data integrity self.assertEqual(self.profile_page.get_displayed_first_name(), 'John', 'First Name does not match') self.assertEqual(self.profile_page.get_displayed_last_name(), 'Doe', 'Last Name does not match') self.assertEqual(self.profile_page.get_displayed_email(), 'john.doe@example.com', 'Email does not match') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python and the unittest framework.
The ProfilePage class follows the Page Object Model. It stores locators and methods to interact with the profile page fields and buttons. This keeps the test code clean and easy to maintain.
Explicit waits ensure elements are visible or clickable before interacting, avoiding timing issues.
The test test_data_integrity_after_save enters the user data, clicks save, then navigates to the profile view page to verify the displayed data matches exactly what was entered.
Assertions include messages to help understand failures.
The setUp and tearDown methods handle browser start and close for each test run.
Now add data-driven testing with 3 different sets of user profile inputs to verify data integrity for each.
Practice
data integrity checks in software testing?Solution
Step 1: Understand the meaning of data integrity
Data integrity means data is accurate, consistent, and unchanged during operations.Step 2: Identify the goal of data integrity checks
These checks confirm that data stays correct and unaltered through software processes.Final Answer:
To ensure data remains accurate and unchanged during processing -> Option AQuick Check:
Data integrity = data accuracy and consistency [OK]
- Confusing data integrity with performance improvements
- Thinking data integrity changes data formats
- Assuming data integrity adds new features
Solution
Step 1: Recall the correct syntax for equality assertion
The common syntax is assertEqual(expected, actual) to compare values.Step 2: Check each option for syntax correctness
assertEqual(expected, actual) matches the correct order and syntax; others have wrong order or wrong method name.Final Answer:
assertEqual(expected, actual) -> Option AQuick Check:
assertEqual(expected, actual) is correct [OK]
- Swapping expected and actual parameters
- Using wrong assertion method names
- Passing a boolean expression instead of two values
expected_data = {'id': 101, 'name': 'Alice'}
actual_data = {'id': 101, 'name': 'Alice'}
assert expected_data == actual_data, 'Data mismatch!'What will be the test result?
Solution
Step 1: Compare expected and actual data dictionaries
Both dictionaries have the same keys and values, so they are equal.Step 2: Understand assertion behavior
Since the condition is true, the assertion passes without raising an error.Final Answer:
Test passes with no error -> Option CQuick Check:
Equal data means assertion passes [OK]
- Assuming assertion prints message on success
- Confusing assertion syntax with function calls
- Thinking assertion fails when data matches
expected = {'user': 'bob', 'age': 30}
actual = {'user': 'bob', 'age': '30'}
assert expected == actual, 'Mismatch found!'Solution
Step 1: Compare dictionary keys and values
Keys are the same, but 'age' values differ: integer 30 vs string '30'.Step 2: Understand assertion behavior with type differences
Since values differ in type, the assertion fails and raises 'Mismatch found!'.Final Answer:
The age values have different data types causing assertion failure -> Option BQuick Check:
Different types cause assertion failure [OK]
- Ignoring type differences in values
- Assuming assertion syntax is wrong
- Thinking keys differ when they don't
users = [
{'id': 1, 'name': 'Anna'},
{'id': 2, 'name': 'Ben'},
{'id': 1, 'name': 'Anna'},
{'id': 3, 'name': 'Cara'}
]Which data integrity check best detects duplicate user IDs?
Solution
Step 1: Understand the problem of duplicate IDs
Duplicates mean some IDs appear more than once in the list.Step 2: Use set to find unique IDs and compare lengths
Convert IDs to a set (unique values). If set length is less than list length, duplicates exist.Final Answer:
Check if length of user list equals length of set of user IDs -> Option DQuick Check:
Set length < list length means duplicates [OK]
- Checking names instead of IDs for duplicates
- Sorting without checking duplicates
- Only verifying data types, not duplicates
Ready to prove it?
Pass the test and this pattern moves unknown
