0
0
Selenium Pythontesting~8 mins

Base page class in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Base page class
Folder Structure
selenium-python-project/
├── pages/
│   ├── base_page.py       # Base page class with common methods
│   ├── login_page.py      # Login page object
│   └── dashboard_page.py  # Dashboard page object
├── tests/
│   ├── test_login.py      # Tests for login functionality
│   └── test_dashboard.py  # Tests for dashboard
├── utils/
│   └── helpers.py         # Utility functions
├── config/
│   └── config.yaml        # Environment and browser settings
├── conftest.py            # Pytest fixtures for setup/teardown
└── requirements.txt       # Dependencies
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown using Selenium WebDriver.
  • Page Objects Layer: Contains page classes representing web pages. BasePage class holds common methods like click, input text, wait, and is inherited by specific page classes.
  • Tests Layer: Contains test scripts using pytest that call page object methods to perform actions and assertions.
  • Utilities Layer: Helper functions for common tasks like reading config, logging, or waiting.
  • Configuration Layer: Holds environment variables, browser options, and credentials in config files.
Configuration Patterns

Use a config.yaml file to store environment URLs, browser types, and user credentials. Load this config in conftest.py to provide fixtures that initialize WebDriver with desired browser and navigate to the correct URL.

Example config.yaml snippet:

environments:
  dev:
    url: "https://dev.example.com"
  prod:
    url: "https://example.com"
browser: chrome
credentials:
  username: "testuser"
  password: "password123"
    

In conftest.py, read this config and create a fixture that sets up the driver accordingly.

Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html or allure-pytest to generate readable HTML or Allure reports after test runs.
  • Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code push.
  • Configure reports to be archived or published as build artifacts for easy access.
  • Use pytest markers to categorize tests (smoke, regression) for selective runs.
Best Practices for Base Page Class Framework
  1. Single Responsibility: BasePage should only have common reusable methods like click, input, wait, and navigation helpers.
  2. Inheritance: Specific page classes inherit from BasePage to avoid code duplication and keep tests clean.
  3. Explicit Waits: Use Selenium explicit waits in BasePage methods to handle dynamic page elements reliably.
  4. Locator Encapsulation: Keep locators private in page classes and expose only meaningful actions to tests.
  5. Readable Methods: Name methods clearly to describe user actions, improving test readability.
Self Check

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

Key Result
Use a BasePage class to hold common Selenium actions, inherited by specific page classes for clean, reusable test code.