0
0
Selenium Pythontesting~8 mins

pytest with Selenium setup in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - pytest with Selenium setup
Folder Structure
project-root/
├── tests/
│   ├── test_login.py
│   ├── test_search.py
│   └── __init__.py
├── pages/
│   ├── login_page.py
│   ├── search_page.py
│   └── __init__.py
├── utils/
│   ├── selenium_helpers.py
│   └── __init__.py
├── config/
│   ├── config.yaml
│   └── __init__.py
├── conftest.py
├── requirements.txt
└── pytest.ini
  
Test Framework Layers
  • Driver Layer: Managed in conftest.py using pytest fixtures to initialize and quit Selenium WebDriver.
  • Page Objects: Classes in pages/ folder representing web pages with methods to interact with UI elements.
  • Tests: Test scripts in tests/ folder using pytest functions that call page object methods and include assertions.
  • Utilities: Helper functions and Selenium wrappers in utils/ to support common actions like waits or screenshots.
  • Configuration: Environment settings, URLs, credentials stored in config/config.yaml and accessed by tests and fixtures.
Configuration Patterns

Use a config.yaml file to store environment-specific data like base URLs, browser types, and credentials.

Example config.yaml:

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

Load this config in conftest.py and pass values to fixtures. Use pytest command line options to select environment or browser.

Test Reporting and CI/CD Integration
  • Use pytest built-in reports with --junitxml=report.xml for XML reports.
  • Integrate pytest-html plugin to generate readable HTML reports.
  • Configure CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests on push or pull requests.
  • Publish reports as build artifacts or send notifications on test failures.
Best Practices
  • Use pytest fixtures in conftest.py to manage WebDriver lifecycle cleanly.
  • Implement Page Object Model to separate test logic from UI details.
  • Keep test data and environment configs outside code in YAML or JSON files.
  • Use explicit waits in Selenium helpers to avoid flaky tests.
  • Write small, independent tests that can run in parallel.
Self Check

Where in this folder structure would you add a new page object for the Login page?

Key Result
Organize pytest with Selenium using clear folder layers: config, pages, tests, utils, and driver management in conftest.py.