0
0
Selenium Pythontesting~8 mins

Action methods in page class in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Action methods in page class
Folder Structure
project_root/
├── src/
│   └── pages/
│       ├── base_page.py
│       ├── login_page.py
│       └── dashboard_page.py
├── tests/
│   ├── test_login.py
│   └── test_dashboard.py
├── utils/
│   ├── driver_factory.py
│   └── helpers.py
├── config/
│   └── config.yaml
└── requirements.txt
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., driver_factory.py).
  • Page Objects Layer: Contains page classes with locators and action methods that perform user interactions (e.g., login_page.py).
  • Test Layer: Contains test scripts that call page object methods to perform tests (e.g., test_login.py).
  • Utilities Layer: Helper functions and reusable utilities (e.g., waits, data handling).
  • Configuration Layer: Holds environment settings, credentials, and browser options.
Configuration Patterns

Use a config.yaml file to store environment URLs, browser types, and user credentials. Load this config in your driver_factory.py and tests to keep settings centralized and easy to change.

Example snippet from config.yaml:

environment:
  url: "https://example.com"
  browser: "chrome"
credentials:
  username: "testuser"
  password: "password123"
    
Test Reporting and CI/CD Integration

Use pytest with plugins like pytest-html to generate readable HTML reports after test runs.

Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code changes and publish reports.

Best Practices for Action Methods in Page Classes
  • Encapsulate Actions: Each method should perform a clear user action (e.g., login(username, password)), hiding low-level details.
  • Use Descriptive Method Names: Names like click_login_button() or enter_username() make tests easy to read.
  • Return Page Objects: After actions that navigate, return the new page object to allow fluent test code.
  • Keep Locators Private: Define locators as private variables inside the page class to avoid external access.
  • Use Explicit Waits: Inside action methods, wait for elements to be ready before interacting to avoid flaky tests.
Self Check

Where in this framework structure would you add a new action method for clicking the "Submit" button on the login page?

Key Result
Action methods in page classes encapsulate user interactions, improving test readability and maintainability.