0
0
Selenium Pythontesting~15 mins

Markers for categorization in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality with valid credentials using markers for categorization
Preconditions (2)
Step 1: Open the browser and navigate to https://example.com/login
Step 2: Enter 'validuser@example.com' in the email input field with id 'email'
Step 3: Enter 'ValidPass123!' in the password input field with id 'password'
Step 4: Click the login button with id 'loginBtn'
Step 5: Wait until the URL changes to https://example.com/dashboard
Step 6: Verify that the dashboard heading with tag h1 and text 'Welcome to your dashboard' is visible
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with the welcome heading displayed
Automation Requirements - pytest with selenium
Assertions Needed:
Assert current URL is https://example.com/dashboard
Assert dashboard heading text is 'Welcome to your dashboard'
Best Practices:
Use pytest markers to categorize the test as 'smoke' and 'login'
Use explicit waits to wait for page elements and URL changes
Use Page Object Model to separate page interactions
Use clear and descriptive assertion messages
Automated Solution
Selenium Python
import pytest
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):
        self.driver.find_element(*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.heading = (By.TAG_NAME, 'h1')

    def get_heading_text(self):
        return self.driver.find_element(*self.heading).text

@pytest.mark.smoke
@pytest.mark.login
def test_login_success():
    driver = webdriver.Chrome()
    wait = WebDriverWait(driver, 10)
    login_page = LoginPage(driver)
    dashboard_page = DashboardPage(driver)

    try:
        login_page.open()
        login_page.login('validuser@example.com', 'ValidPass123!')

        wait.until(EC.url_to_be('https://example.com/dashboard'))
        assert driver.current_url == 'https://example.com/dashboard', 'URL after login should be dashboard URL'

        wait.until(EC.visibility_of_element_located(dashboard_page.heading))
        heading_text = dashboard_page.get_heading_text()
        assert heading_text == 'Welcome to your dashboard', f'Expected heading text but got "{heading_text}"'
    finally:
        driver.quit()

This test uses pytest markers @pytest.mark.smoke and @pytest.mark.login to categorize it as a smoke test and login test. This helps to run specific groups of tests easily.

The Page Object Model separates page details from the test logic. LoginPage handles login page actions, and DashboardPage handles dashboard page elements.

We use explicit waits with WebDriverWait and expected_conditions to wait for the URL to change and the dashboard heading to appear. This avoids flaky tests caused by timing issues.

Assertions check the URL and heading text with clear messages to help understand failures.

The finally block ensures the browser closes even if the test fails.

Common Mistakes - 4 Pitfalls
Not using explicit waits and relying on time.sleep
Hardcoding locators inside the test function instead of using Page Object Model
Not using pytest markers for categorization
Not closing the browser after test execution
Bonus Challenge

Now add data-driven testing with 3 different sets of valid login credentials using pytest parametrize.

Show Hint