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 RegistrationPage:
def __init__(self, driver):
self.driver = driver
self.first_name = driver.find_element(By.ID, 'firstName')
self.last_name = driver.find_element(By.ID, 'lastName')
self.email = driver.find_element(By.ID, 'email')
self.password = driver.find_element(By.ID, 'password')
self.confirm_password = driver.find_element(By.ID, 'confirmPassword')
self.submit_button = driver.find_element(By.ID, 'submitBtn')
def fill_form(self, first, last, email, pwd, confirm_pwd):
self.first_name.clear()
self.first_name.send_keys(first)
self.last_name.clear()
self.last_name.send_keys(last)
self.email.clear()
self.email.send_keys(email)
self.password.clear()
self.password.send_keys(pwd)
self.confirm_password.clear()
self.confirm_password.send_keys(confirm_pwd)
def submit(self):
self.submit_button.click()
class TestRegistration(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get('https://example.com/registration')
self.wait = WebDriverWait(self.driver, 10)
self.page = RegistrationPage(self.driver)
def test_valid_registration(self):
self.page.fill_form('John', 'Doe', 'john.doe@example.com', 'Password123!', 'Password123!')
self.page.submit()
confirmation = self.wait.until(
EC.visibility_of_element_located((By.ID, 'confirmationMessage'))
)
self.assertEqual(confirmation.text, 'Registration successful')
# Verify no validation errors
errors = self.driver.find_elements(By.CLASS_NAME, 'validation-error')
self.assertEqual(len(errors), 0)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()The RegistrationPage class uses the Page Object Model to keep element locators and actions together. This makes the test easier to maintain.
In test_valid_registration, we fill the form with valid data and submit it.
We use WebDriverWait with visibility_of_element_located to wait for the confirmation message to appear, ensuring the test does not fail due to timing issues.
Assertions check that the confirmation message text is exactly 'Registration successful' and that no validation error messages are present.
The tearDown method closes the browser after the test.