0
0
Testing Fundamentalstesting~15 mins

Acceptance testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify user can successfully register and login
Preconditions (2)
Step 1: Enter 'newuser@example.com' in the email field
Step 2: Enter 'Password123!' in the password field
Step 3: Click the 'Register' button
Step 4: Verify registration success message is displayed
Step 5: Navigate to the login page
Step 6: Enter 'newuser@example.com' in the email field
Step 7: Enter 'Password123!' in the password field
Step 8: Click the 'Login' button
✅ Expected Result: User is redirected to the dashboard page with a welcome message
Automation Requirements - Selenium with Python
Assertions Needed:
Verify registration success message is visible
Verify current URL is dashboard URL after login
Verify welcome message is displayed on dashboard
Best Practices:
Use explicit waits to wait for elements
Use Page Object Model to organize locators and actions
Use clear and descriptive assertion messages
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 RegistrationPage:
    def __init__(self, driver):
        self.driver = driver
        self.email_input = (By.ID, 'email')
        self.password_input = (By.ID, 'password')
        self.register_button = (By.ID, 'register-btn')
        self.success_message = (By.ID, 'register-success')

    def register(self, email, password):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).send_keys(email)
        self.driver.find_element(*self.password_input).send_keys(password)
        self.driver.find_element(*self.register_button).click()

    def is_registration_successful(self):
        return WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.success_message))

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.email_input = (By.ID, 'email')
        self.password_input = (By.ID, 'password')
        self.login_button = (By.ID, 'login-btn')

    def login(self, email, password):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).send_keys(email)
        self.driver.find_element(*self.password_input).send_keys(password)
        self.driver.find_element(*self.login_button).click()

class DashboardPage:
    def __init__(self, driver):
        self.driver = driver
        self.welcome_message = (By.ID, 'welcome-msg')

    def is_welcome_message_displayed(self):
        return WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.welcome_message))


def test_user_registration_and_login():
    driver = webdriver.Chrome()
    driver.get('https://example.com/register')

    registration_page = RegistrationPage(driver)
    registration_page.register('newuser@example.com', 'Password123!')

    assert registration_page.is_registration_successful(), 'Registration success message not displayed'

    driver.get('https://example.com/login')

    login_page = LoginPage(driver)
    login_page.login('newuser@example.com', 'Password123!')

    WebDriverWait(driver, 10).until(EC.url_contains('/dashboard'))
    assert '/dashboard' in driver.current_url, 'Did not navigate to dashboard after login'

    dashboard_page = DashboardPage(driver)
    assert dashboard_page.is_welcome_message_displayed(), 'Welcome message not displayed on dashboard'

    driver.quit()

This test script automates the acceptance test case for user registration and login.

We use the Page Object Model to keep locators and actions organized for each page: RegistrationPage, LoginPage, and DashboardPage.

Explicit waits ensure the test waits for elements to appear before interacting or asserting, avoiding flaky tests.

Assertions check that the registration success message appears, the URL changes to the dashboard after login, and the welcome message is visible on the dashboard.

Finally, the browser is closed with driver.quit() to clean up.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Hardcoding URLs and locators without using variables or page objects
Not verifying important outcomes like success messages or URL changes
Bonus Challenge

Now add data-driven testing with 3 different user email and password combinations for registration and login.

Show Hint