CRUD operation verification 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 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.
Now add data-driven testing with 3 different user inputs for create, update, and delete operations