0
0
Selenium Pythontesting~8 mins

Parallel execution in CI in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Parallel execution in CI
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   ├── test_checkout.py
│   └── test_search.py
├── pages/
│   ├── base_page.py
│   ├── login_page.py
│   └── checkout_page.py
├── utils/
│   ├── driver_factory.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── conftest.py
├── pytest.ini
└── requirements.txt
    
Test Framework Layers
  • Driver Layer: utils/driver_factory.py creates and manages browser drivers with options for parallel runs.
  • Page Objects: pages/ contains classes representing web pages with methods to interact with UI elements.
  • Tests: tests/ holds test scripts using pytest and Selenium WebDriver.
  • Utilities: utils/ has helper functions and driver setup for reuse.
  • Configuration: config/ stores environment settings, credentials, and browser options.
  • Fixtures & Hooks: conftest.py defines pytest fixtures for setup/teardown and driver initialization supporting parallel execution.
Configuration Patterns

Use config.yaml to define environments (dev, staging, prod), browsers (chrome, firefox), and parallel settings.

Example pytest.ini to enable pytest-xdist for parallel runs:

[pytest]
addopts = -n auto --dist=loadscope
    

conftest.py reads config and creates isolated WebDriver instances per test worker.

Test Reporting and CI/CD Integration
  • Use pytest-html or Allure for clear HTML reports showing parallel test results.
  • Integrate with CI tools like GitHub Actions, Jenkins, or GitLab CI to run tests in parallel on multiple agents or containers.
  • Configure CI pipeline to install dependencies, run pytest with parallel flags, and publish reports.
  • Example GitHub Actions step to run tests in parallel:
- name: Run Selenium tests
  run: |
    pip install -r requirements.txt
    pytest -n auto --html=report.html
    
Best Practices
  1. Isolate Tests: Ensure tests do not share state or data to avoid flaky results during parallel runs.
  2. Use Fixtures Wisely: Use pytest fixtures with scope='function' to create fresh browser instances per test.
  3. Explicit Waits: Use explicit waits to handle dynamic page elements reliably in parallel execution.
  4. Configurable Parallelism: Allow parallel thread count to be set via config or CLI for flexible CI integration.
  5. Clean Up: Properly close browser instances after each test to free resources.
Self Check

Where in this folder structure would you add a new fixture to initialize a browser driver that supports parallel execution?

Key Result
Use pytest with pytest-xdist and isolated WebDriver instances to run Selenium tests in parallel within CI pipelines.