0
0
Testing Fundamentalstesting~15 mins

Page Object Model concept in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Automate login functionality using Page Object Model
Preconditions (2)
Step 1: Open the login page URL
Step 2: Enter 'testuser@example.com' in the email input field
Step 3: Enter 'Password123' in the password input field
Step 4: Click the login button
Step 5: Verify that the user is redirected to the dashboard page
Step 6: Verify that the dashboard page contains a welcome message
✅ Expected Result: User successfully logs in and sees the dashboard page with a welcome message
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL is dashboard URL after login
Verify presence of welcome message element on dashboard
Best Practices:
Use Page Object Model to separate page locators and actions
Use explicit waits to wait for elements before interacting
Keep test logic separate from page object classes
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 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, 'loginBtn')

    def open(self, url):
        self.driver.get(url)

    def enter_email(self, email):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.email_input)
        ).send_keys(email)

    def enter_password(self, password):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.password_input)
        ).send_keys(password)

    def click_login(self):
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.login_button)
        ).click()

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

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


def test_login():
    driver = webdriver.Chrome()
    login_page = LoginPage(driver)
    dashboard_page = DashboardPage(driver)

    try:
        login_page.open('https://example.com/login')
        login_page.enter_email('testuser@example.com')
        login_page.enter_password('Password123')
        login_page.click_login()

        WebDriverWait(driver, 10).until(
            EC.url_contains('/dashboard')
        )

        assert '/dashboard' in driver.current_url, 'URL after login is incorrect'
        assert dashboard_page.is_welcome_message_displayed(), 'Welcome message not found on dashboard'

    finally:
        driver.quit()

if __name__ == '__main__':
    test_login()

This code uses the Page Object Model to organize the test.

The LoginPage class holds locators and actions for the login page. It has methods to open the page, enter email, enter password, and click login. Each method waits explicitly for the element to be ready before interacting.

The DashboardPage class holds the locator and method to check if the welcome message is visible on the dashboard.

The test_login function creates a browser driver, uses the page objects to perform the login steps, waits for the dashboard URL, and asserts the URL and welcome message presence.

Explicit waits ensure the test does not fail due to slow loading. Separating page details from test logic makes the code easier to maintain and read.

Common Mistakes - 3 Pitfalls
Putting locators and test steps directly in the test function
Using fixed sleep delays instead of explicit waits
Not verifying the page URL after login
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid).

Show Hint