Bird
Raised Fist0
Testing Fundamentalstesting~15 mins

CRUD operation verification in Testing Fundamentals - Build an Automation Script

Choose your learning style10 modes available
Verify CRUD operations on the user management system
Preconditions (2)
Step 1: Navigate to the user management page
Step 2: Create a new user with name 'Test User', email 'testuser@example.com', and role 'Editor'
Step 3: Verify the new user appears in the user list
Step 4: Update the user's role to 'Admin'
Step 5: Verify the user's role is updated in the user list
Step 6: Delete the user 'Test User'
Step 7: Verify the user no longer appears in the user list
✅ Expected Result: User is successfully created, updated, and deleted with the changes reflected correctly in the user list after each operation
Automation Requirements - Selenium with Python
Assertions Needed:
Verify user creation by checking user appears in list
Verify user update by checking updated role in list
Verify user deletion by checking user no longer in list
Best Practices:
Use explicit waits to handle dynamic page elements
Use Page Object Model to organize locators and actions
Use meaningful and stable locators (id, name, or data-test attributes)
Clean up test data after test execution
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

class UserManagementPage:
    def __init__(self, driver):
        self.driver = driver
        self.wait = WebDriverWait(driver, 10)

    def navigate(self):
        self.driver.get('https://example.com/user-management')

    def create_user(self, name, email, role):
        self.wait.until(EC.element_to_be_clickable((By.ID, 'create-user-btn'))).click()
        self.wait.until(EC.visibility_of_element_located((By.ID, 'user-name'))).send_keys(name)
        self.driver.find_element(By.ID, 'user-email').send_keys(email)
        role_dropdown = self.driver.find_element(By.ID, 'user-role')
        for option in role_dropdown.find_elements(By.TAG_NAME, 'option'):
            if option.text == role:
                option.click()
                break
        self.driver.find_element(By.ID, 'save-user-btn').click()

    def find_user_row(self, name):
        self.wait.until(EC.visibility_of_element_located((By.ID, 'user-table')))
        rows = self.driver.find_elements(By.CSS_SELECTOR, '#user-table tbody tr')
        for row in rows:
            if row.find_element(By.CSS_SELECTOR, 'td.name').text == name:
                return row
        return None

    def update_user_role(self, name, new_role):
        row = self.find_user_row(name)
        if row:
            row.find_element(By.CSS_SELECTOR, 'button.edit-btn').click()
            role_dropdown = self.wait.until(EC.visibility_of_element_located((By.ID, 'user-role')))
            for option in role_dropdown.find_elements(By.TAG_NAME, 'option'):
                if option.text == new_role:
                    option.click()
                    break
            self.driver.find_element(By.ID, 'save-user-btn').click()

    def delete_user(self, name):
        row = self.find_user_row(name)
        if row:
            row.find_element(By.CSS_SELECTOR, 'button.delete-btn').click()
            alert = self.wait.until(EC.alert_is_present())
            alert.accept()

from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import unittest

class TestUserManagementCRUD(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        options = Options()
        options.add_argument('--headless')
        cls.driver = webdriver.Chrome(service=Service(), options=options)
        cls.page = UserManagementPage(cls.driver)
        cls.page.navigate()

    def test_crud_operations(self):
        name = 'Test User'
        email = 'testuser@example.com'
        initial_role = 'Editor'
        updated_role = 'Admin'

        # Create user
        self.page.create_user(name, email, initial_role)
        row = self.page.find_user_row(name)
        self.assertIsNotNone(row, 'User should be created and visible in list')
        self.assertEqual(row.find_element(By.CSS_SELECTOR, 'td.role').text, initial_role, 'User role should be Editor')

        # Update user role
        self.page.update_user_role(name, updated_role)
        row = self.page.find_user_row(name)
        self.assertIsNotNone(row, 'User should still be visible after update')
        self.assertEqual(row.find_element(By.CSS_SELECTOR, 'td.role').text, updated_role, 'User role should be updated to Admin')

        # Delete user
        self.page.delete_user(name)
        row = self.page.find_user_row(name)
        self.assertIsNone(row, 'User should be deleted and not visible in list')

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

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

The code uses Selenium with Python to automate the CRUD operations on a user management page.

UserManagementPage class encapsulates page actions and locators using the Page Object Model. This keeps the test code clean and maintainable.

Explicit waits ensure the script waits for elements to be ready before interacting, avoiding flaky tests.

The test class TestUserManagementCRUD runs the full flow: create a user, verify creation, update the user's role, verify update, delete the user, and verify deletion.

Assertions check that the user appears or disappears in the list as expected after each operation.

Headless browser mode is used for faster test runs without opening a browser window.

Finally, the driver quits after all tests to clean up resources.

Common Mistakes - 4 Pitfalls
Using hardcoded sleeps instead of explicit waits
Using brittle XPath locators that break easily
Mixing test logic and page interaction code
Not cleaning up test data after test runs
Bonus Challenge

Now add data-driven testing with 3 different user inputs for create, update, and delete operations

Show Hint

Practice

(1/5)
1. Which CRUD operation is responsible for adding new data to a system?
easy
A. Delete
B. Read
C. Update
D. Create

Solution

  1. Step 1: Understand CRUD operations

    CRUD stands for Create, Read, Update, and Delete, each representing a basic data operation.
  2. Step 2: Identify the operation for adding data

    The 'Create' operation is used to add new data entries into the system.
  3. Final Answer:

    Create -> Option D
  4. Quick Check:

    Adding new data = Create [OK]
Hint: Create means add new data to the system [OK]
Common Mistakes:
  • Confusing 'Create' with 'Update'
  • Thinking 'Read' adds data
  • Mixing 'Delete' with adding data
2. Which of the following is the correct syntax to verify a Delete operation in a test case?
easy
A. assert data.exists() == True
B. assert data.is_deleted() == True
C. assert data.count() > 0
D. assert data.is_present() == True

Solution

  1. Step 1: Understand Delete verification

    To verify deletion, the test must confirm the data no longer exists or is marked deleted.
  2. Step 2: Choose assertion that confirms deletion

    assert data.is_deleted() == True correctly checks that the data was deleted.
  3. Final Answer:

    assert data.is_deleted() == True -> Option B
  4. Quick Check:

    Delete check uses is_deleted() == True [OK]
Hint: Check deletion with is_deleted() == True [OK]
Common Mistakes:
  • Using exists() == True after deletion
  • Checking count() > 0 which means data exists
  • Confusing is_present() with deletion status
3. Consider this test code snippet verifying an Update operation:
original_value = record.get('name')
record.update({'name': 'Alice'})
updated_value = record.get('name')
assert updated_value == 'Alice'

What will be the result of this test if the update works correctly?
medium
A. Test passes because updated_value matches 'Alice'
B. Test fails because original_value is not checked
C. Test fails due to syntax error in update method
D. Test passes but updated_value remains original_value

Solution

  1. Step 1: Analyze the update operation

    The code updates the 'name' field to 'Alice' and retrieves it again.
  2. Step 2: Check the assertion logic

    The assertion checks if updated_value equals 'Alice', which should be true if update works.
  3. Final Answer:

    Test passes because updated_value matches 'Alice' -> Option A
  4. Quick Check:

    Updated value == 'Alice' means test passes [OK]
Hint: Assert updated value equals new data after update [OK]
Common Mistakes:
  • Assuming original_value must be checked in this test
  • Thinking update method has syntax error
  • Believing updated_value stays unchanged after update
4. A tester wrote this code to verify a Create operation:
new_record = db.create({'id': 1, 'value': 100})
assert new_record.id == 1
assert new_record.value == 100
assert db.find(1) == None

What is the error in this test?
medium
A. The assertion db.find(1) == None is incorrect after creation
B. The new_record object does not have id attribute
C. The create method should not return a record
D. The value 100 is not valid for creation

Solution

  1. Step 1: Understand Create operation verification

    After creating a record, it should be found in the database.
  2. Step 2: Analyze the assertion db.find(1) == None

    This assertion expects no record found, which contradicts the creation.
  3. Final Answer:

    The assertion db.find(1) == None is incorrect after creation -> Option A
  4. Quick Check:

    Created record must be found, so find(1) != None [OK]
Hint: After create, find should return the record, not None [OK]
Common Mistakes:
  • Expecting find to return None after creation
  • Assuming create returns nothing
  • Confusing attribute existence on new_record
5. You want to verify all CRUD operations on a user profile. Which sequence of test steps is best to ensure data integrity?
hard
A. Create user, Update user, Delete user, Read user
B. Read user, Create user, Update user, Delete user
C. Create user, Read user, Update user, Delete user
D. Delete user, Create user, Read user, Update user

Solution

  1. Step 1: Understand logical CRUD test order

    First, create data to have something to work with, then read to verify creation.
  2. Step 2: Follow with update and delete

    Update tests changes, and delete confirms removal, maintaining data integrity.
  3. Final Answer:

    Create user, Read user, Update user, Delete user -> Option C
  4. Quick Check:

    CRUD order: Create, Read, Update, Delete [OK]
Hint: Test CRUD in order: Create, Read, Update, Delete [OK]
Common Mistakes:
  • Deleting before creating data
  • Reading before data exists
  • Updating before creation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard