0
0
Testing Fundamentalstesting~15 mins

Data integrity checks in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify data integrity after saving user profile information
Preconditions (2)
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: Click the Save button
Step 5: Navigate to the profile view page
✅ Expected Result: The profile view page displays First Name as 'John', Last Name as 'Doe', and Email as 'john.doe@example.com' exactly as entered
Automation Requirements - Selenium with Python
Assertions Needed:
Verify that the First Name displayed matches the input 'John'
Verify that the Last Name displayed matches the input 'Doe'
Verify that the Email displayed matches the input 'john.doe@example.com'
Best Practices:
Use explicit waits to wait for elements to be visible before interacting
Use clear and maintainable locators like IDs or data-test attributes
Separate test logic and page interactions using the Page Object Model
Include meaningful assertion messages for easier debugging
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 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.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators that depend on page structure
Mixing test logic and page interaction code
Not verifying the displayed data after saving
Bonus Challenge

Now add data-driven testing with 3 different sets of user profile inputs to verify data integrity for each.

Show Hint