0
0
Selenium Pythontesting~15 mins

Why POM organizes test code in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Verify 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 'Test@1234' in the password input field
Step 4: Click the login button
Step 5: Verify that the user is redirected to the dashboard page
✅ Expected Result: User successfully logs in and dashboard page is displayed
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL is dashboard URL after login
Verify login button is clickable before clicking
Verify email and password fields accept input
Best Practices:
Use Page Object Model to separate page locators and actions
Use explicit waits to wait for elements
Keep test logic separate from page interaction code
Automated Solution
Selenium Python
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 enter_email(self, email):
        email_field = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.email_input))
        email_field.clear()
        email_field.send_keys(email)

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

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


def test_login():
    driver = webdriver.Chrome()
    driver.get('https://example.com/login')

    login_page = LoginPage(driver)
    login_page.enter_email('testuser@example.com')
    login_page.enter_password('Test@1234')
    login_page.click_login()

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

    assert '/dashboard' in driver.current_url, 'Dashboard URL not loaded after login'

    driver.quit()

This code uses the Page Object Model (POM) to organize test code.

The LoginPage class holds all locators and actions for the login page. This keeps selectors and interaction logic in one place.

The test function test_login uses this class to perform steps clearly and simply.

Explicit waits ensure elements are ready before interacting, avoiding flaky tests.

Assertions check that the login succeeded by verifying the URL.

This separation makes the test easier to read, maintain, and update if the page changes.

Common Mistakes - 3 Pitfalls
Putting locators and test steps together in the test function
Not using explicit waits before interacting with elements
Hardcoding URLs and credentials inside test functions
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials

Show Hint