0
0
Selenium Pythontesting~15 mins

Page class structure in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Automate login page using Page Object Model
Preconditions (2)
Step 1: Open the browser and navigate to the login page URL
Step 2: Enter 'testuser@example.com' in the email input field
Step 3: Enter 'TestPass123' in the password input field
Step 4: Click the login button
Step 5: Wait for the dashboard page to load
✅ Expected Result: User is successfully logged in and dashboard page is displayed with URL containing '/dashboard'
Automation Requirements - selenium
Assertions Needed:
Verify current URL contains '/dashboard' after login
Verify login button is clickable before clicking
Verify email and password fields are present before entering text
Best Practices:
Use Page Object Model to separate page structure and test logic
Use explicit waits to wait for elements to be interactable
Use meaningful locators like By.ID or By.NAME instead of brittle XPath
Keep test code clean and readable
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.presence_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.presence_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('TestPass123')
    login_page.click_login()

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

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

    driver.quit()

This code uses the Page Object Model to organize the login page interactions.

The LoginPage class stores locators and methods to interact with the email, password, and login button elements.

Each method uses explicit waits to ensure elements are present or clickable before interacting, which avoids timing issues.

The test function test_login opens the browser, navigates to the login page, uses the page object methods to enter credentials and click login, then waits for the dashboard URL to appear.

Finally, it asserts the URL contains '/dashboard' to confirm successful login and closes the browser.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using hardcoded sleeps instead of explicit waits', 'why_bad': 'Hardcoded sleeps slow tests and can cause flaky failures if the page loads faster or slower than expected.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait only as long as needed."}
Locating elements directly in test code instead of using a page class
Using brittle XPath locators that break easily
Bonus Challenge

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

Show Hint