0
0
Selenium Pythontesting~15 mins

Why advanced patterns solve real challenges in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Automate login and verify user dashboard using Page Object Model
Preconditions (2)
Step 1: Open the browser and navigate to https://example.com/login
Step 2: Enter 'testuser@example.com' in the email input field with id 'email'
Step 3: Enter 'TestPass123!' in the password input field with id 'password'
Step 4: Click the login button with id 'loginBtn'
Step 5: Wait until the dashboard page loads and URL contains '/dashboard'
Step 6: Verify that the welcome message with id 'welcomeMsg' contains the text 'Welcome, Test User'
✅ Expected Result: User is successfully logged in and redirected to the dashboard page showing the welcome message
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL contains '/dashboard'
Verify welcome message text is 'Welcome, Test User'
Best Practices:
Use Page Object Model to separate page structure and test logic
Use explicit waits to wait for elements or conditions
Use meaningful locators like IDs
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 open(self):
        self.driver.get('https://example.com/login')

    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, 'welcomeMsg')

    def wait_for_dashboard(self):
        WebDriverWait(self.driver, 10).until(
            EC.url_contains('/dashboard')
        )

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


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

        login_page.open()
        login_page.login('testuser@example.com', 'TestPass123!')

        dashboard_page.wait_for_dashboard()

        assert '/dashboard' in driver.current_url, 'URL does not contain /dashboard'

        welcome_text = dashboard_page.get_welcome_text()
        assert welcome_text == 'Welcome, Test User', f'Unexpected welcome message: {welcome_text}'

    finally:
        driver.quit()

if __name__ == '__main__':
    test_login()

This test uses the Page Object Model (POM) pattern to organize code. The LoginPage class handles all actions on the login page, like opening the page and entering credentials. The DashboardPage class handles actions on the dashboard page, like waiting for the page to load and getting the welcome message.

Explicit waits are used to wait for elements to appear or for the URL to change. This avoids timing issues where the test tries to interact with elements before they are ready.

Assertions check that the URL contains '/dashboard' and that the welcome message text is exactly as expected. This confirms the login succeeded and the user sees the correct page.

Using POM keeps the test code clean and easy to maintain. If the page changes, only the page classes need updating, not the test logic. This pattern solves real challenges like code duplication, brittle tests, and hard-to-read scripts.

Common Mistakes - 3 Pitfalls
Using hardcoded sleeps instead of explicit waits
Locating elements inside the test function instead of page objects
Using brittle locators like absolute XPath
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid) to verify login success and failure scenarios.

Show Hint