0
0
Selenium Pythontesting~8 mins

Page factory pattern in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Page factory pattern
Folder Structure
project_root/
├── tests/
│   ├── test_login.py
│   └── test_search.py
├── pages/
│   ├── base_page.py
│   ├── login_page.py
│   └── search_page.py
├── utils/
│   ├── driver_factory.py
│   └── config_reader.py
├── resources/
│   └── config.yaml
└── requirements.txt
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., driver_factory.py).
  • Page Objects: Each page class uses the Page Factory pattern to initialize web elements lazily and cleanly (e.g., login_page.py).
  • Tests: Test scripts that use page objects to perform actions and assertions (e.g., test_login.py).
  • Utilities: Helpers for configuration reading, waits, and common functions.
  • Resources: External files like configuration data (e.g., config.yaml).
Configuration Patterns
  • Use a config.yaml file to store environment URLs, browser types, and credentials.
  • config_reader.py reads this file and provides config data to tests and driver setup.
  • Driver factory uses config to launch the correct browser (Chrome, Firefox, etc.).
  • Separate test data from code for easy updates and environment switching.
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html or allure-pytest for readable test reports.
  • Reports show which tests passed or failed with screenshots on failure.
  • Integrate tests in CI/CD pipelines (GitHub Actions, Jenkins) to run on every code push.
  • Fail fast on critical errors and notify team via email or chat tools.
Best Practices for Page Factory Pattern
  1. Use lazy element initialization: Elements are located only when needed, improving speed and reliability.
  2. Keep page classes focused: Each page object should represent one page or component with clear methods.
  3. Encapsulate element locators: Store locators as private variables to avoid duplication and ease maintenance.
  4. Use explicit waits: Wait for elements to be ready before interacting to avoid flaky tests.
  5. Separate test logic from page logic: Tests call page methods, not raw Selenium commands.
Self Check

Where in this folder structure would you add a new page object for the "Profile" page using the Page Factory pattern?

Key Result
Use the Page Factory pattern in page objects to initialize web elements cleanly and improve test maintainability.