0
0
Selenium Pythontesting~8 mins

Page class structure in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Page class structure
Folder Structure
project_root/
├── pages/
│   ├── __init__.py
│   ├── base_page.py
│   ├── login_page.py
│   └── dashboard_page.py
├── tests/
│   ├── __init__.py
│   ├── test_login.py
│   └── test_dashboard.py
├── utils/
│   ├── __init__.py
│   └── helpers.py
├── config/
│   ├── __init__.py
│   └── config.py
├── conftest.py
└── requirements.txt
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown, usually in conftest.py using fixtures.
  • Page Objects: Classes in pages/ folder representing web pages. Each class has locators and methods to interact with page elements.
  • Tests: Test scripts in tests/ folder that use page objects to perform actions and assertions.
  • Utilities: Helper functions or common utilities in utils/ to support tests and page objects.
  • Configuration: Settings like URLs, credentials, and environment variables in config/ folder.
Configuration Patterns

Use a config.py file to store environment-specific data such as base URLs, browser types, and credentials. Use environment variables or command line options to switch environments.

# config/config.py
class Config:
    BASE_URL = "https://example.com"
    BROWSER = "chrome"
    USERNAME = "testuser"
    PASSWORD = "password123"

# conftest.py example snippet
import pytest
from selenium import webdriver
from config.config import Config

@pytest.fixture
def driver():
    if Config.BROWSER == "chrome":
        driver = webdriver.Chrome()
    elif Config.BROWSER == "firefox":
        driver = webdriver.Firefox()
    else:
        raise ValueError("Unsupported browser")
    driver.maximize_window()
    yield driver
    driver.quit()
    
Test Reporting and CI/CD Integration

Use PyTest built-in reporting with options like --junitxml=report.xml for XML reports.

Integrate with CI/CD tools (GitHub Actions, Jenkins) to run tests automatically on code push.

# Example GitHub Actions snippet
name: Run Selenium Tests

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Run tests
        run: |
          pytest --junitxml=report.xml
      - name: Upload test report
        uses: actions/upload-artifact@v3
        with:
          name: test-report
          path: report.xml
    
Best Practices for Page Class Structure
  • Single Responsibility: Each page class should represent one page and contain only methods related to that page.
  • Encapsulation: Keep locators private and expose only meaningful actions as methods.
  • Reusability: Use a base page class for common methods like navigation, waits, or element interactions.
  • Explicit Waits: Use Selenium explicit waits inside page methods to handle dynamic page elements.
  • Readable Method Names: Name methods clearly to describe user actions, e.g., login(username, password).
Self Check

Where in this folder structure would you add a new page object class for a "Profile" page?

Key Result
Organize Selenium tests with page classes in a dedicated folder, using a base page for shared methods and clear separation of tests, config, and utilities.