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 SystemTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.wait = WebDriverWait(self.driver, 10)
def test_system_functionality(self):
driver = self.driver
wait = self.wait
# Launch application
driver.get('https://example.com/login')
# Login
wait.until(EC.visibility_of_element_located((By.ID, 'username'))).send_keys('user1')
driver.find_element(By.ID, 'password').send_keys('Password123!')
driver.find_element(By.ID, 'login-button').click()
# Verify dashboard loaded
wait.until(EC.url_contains('/dashboard'))
self.assertIn('/dashboard', driver.current_url)
# Create new record
wait.until(EC.element_to_be_clickable((By.ID, 'new-record-button'))).click()
wait.until(EC.visibility_of_element_located((By.ID, 'record-name'))).send_keys('Test Record')
driver.find_element(By.ID, 'record-description').send_keys('System test entry')
driver.find_element(By.ID, 'save-record-button').click()
# Verify new record in list
record_locator = (By.XPATH, "//table//td[text()='Test Record']")
wait.until(EC.visibility_of_element_located(record_locator))
self.assertTrue(driver.find_element(*record_locator).is_displayed())
# Edit record
edit_button_locator = (By.XPATH, "//table//td[text()='Test Record']/following-sibling::td/button[@class='edit']")
driver.find_element(*edit_button_locator).click()
name_field = wait.until(EC.visibility_of_element_located((By.ID, 'record-name')))
name_field.clear()
name_field.send_keys('Updated Record')
driver.find_element(By.ID, 'save-record-button').click()
# Verify updated record name
updated_record_locator = (By.XPATH, "//table//td[text()='Updated Record']")
wait.until(EC.visibility_of_element_located(updated_record_locator))
self.assertTrue(driver.find_element(*updated_record_locator).is_displayed())
# Delete record
delete_button_locator = (By.XPATH, "//table//td[text()='Updated Record']/following-sibling::td/button[@class='delete']")
driver.find_element(*delete_button_locator).click()
# Confirm deletion alert
alert = wait.until(EC.alert_is_present())
alert.accept()
# Verify record removed
wait.until(EC.invisibility_of_element_located(updated_record_locator))
elements = driver.find_elements(*updated_record_locator)
self.assertEqual(len(elements), 0)
# Logout
driver.find_element(By.ID, 'logout-button').click()
# Verify redirected to login
wait.until(EC.url_contains('/login'))
self.assertIn('/login', driver.current_url)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()This test script uses Selenium WebDriver with Python's unittest framework to automate the system test.
setUp: Opens the browser and prepares explicit waits.
test_system_functionality: Automates the manual test steps precisely:
- Launches the app and logs in with given credentials.
- Waits for dashboard URL to confirm login success.
- Creates a new record and verifies it appears in the list.
- Edits the record's name and verifies the update.
- Deletes the record and confirms it is removed.
- Logs out and verifies redirection to login page.
Explicit waits ensure elements are ready before actions, avoiding flaky tests.
Locators use IDs and XPath with stable attributes for reliability.
tearDown: Closes the browser after test completion.
This structure follows best practices for maintainability and clarity.