0
0
Selenium Pythontesting~8 mins

Handling multiple pages in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Handling multiple pages
Folder Structure
tests/
├── test_login.py
├── test_shopping_cart.py

pages/
├── base_page.py
├── login_page.py
├── home_page.py
├── product_page.py

utils/
├── driver_factory.py
├── wait_utils.py

config/
├── config.yaml

reports/
├── test_report.html

Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., driver_factory.py creates WebDriver instances).
  • Page Objects: Each page (Login, Home, Product) has a class with methods to interact with page elements. This helps handle multiple pages by switching between page objects.
  • Tests: Test scripts use page objects to perform actions and assertions across multiple pages.
  • Utilities: Helper functions like explicit waits (wait_utils.py) to handle page load and element visibility.
  • Configuration: Stores environment settings like URLs, browser types, and credentials in config.yaml.
Configuration Patterns

Use a config.yaml file to store environment details:

environment:
  base_url: "https://example.com"
  browser: "chrome"
  implicit_wait: 10
  credentials:
    username: "testuser"
    password: "password123"

Load this config in driver_factory.py and test setup to initialize the browser and navigate to the correct URL.

Use environment variables or command-line arguments to switch between test environments (e.g., staging, production).

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate readable HTML reports saved in the reports/ folder.
  • Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on every code push.
  • Reports show which tests passed or failed, including tests that navigate multiple pages.
  • Logs and screenshots on failure help debug multi-page navigation issues.
Best Practices
  1. Use Page Object Model: Create a separate class for each page to keep code organized and reusable.
  2. Explicit Waits: Use waits to handle page loads and dynamic elements when switching pages.
  3. Clear Navigation Methods: Define methods in page objects that represent user actions leading to other pages (e.g., login_page.login() returns HomePage object).
  4. Keep Tests Focused: Each test should focus on a user flow across pages, using page objects to switch context cleanly.
  5. Centralize Config: Manage URLs, credentials, and browser settings in one place for easy updates.
Self Check

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

Key Result
Use Page Object Model with clear navigation methods to handle multiple pages in Selenium Python tests.