0
0
Selenium Pythontesting~8 mins

Retry mechanism for flaky tests in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Retry mechanism for flaky tests
Folder Structure
selenium-python-project/
├── src/
│   ├── pages/
│   │   └── login_page.py
│   ├── tests/
│   │   └── test_login.py
│   ├── utils/
│   │   └── retry.py
│   └── config/
│       └── config.yaml
├── conftest.py
├── requirements.txt
└── pytest.ini
    
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown using Selenium WebDriver in conftest.py.
  • Page Objects: Encapsulate page elements and actions, e.g., login_page.py.
  • Tests: Test cases using PyTest in tests/ folder.
  • Utilities: Helper functions like retry decorators in utils/retry.py to handle flaky tests.
  • Configuration: Environment and test settings in config/config.yaml.
Configuration Patterns

Use config.yaml to store environment URLs, browser types, and credentials. Load these settings in conftest.py to initialize tests dynamically.

Example pytest.ini to enable retry plugin and set retry count:

[pytest]
addopts = --maxfail=3 --reruns 2 --reruns-delay 1
    

Alternatively, implement a custom retry decorator in utils/retry.py to retry flaky tests explicitly.

Test Reporting and CI/CD Integration
  • Use PyTest's built-in reporting with --junitxml=report.xml for CI tools.
  • Integrate with CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on each commit.
  • Configure retries in CI to reduce false failures from flaky tests.
  • Use HTML reports (e.g., pytest-html) for easy visualization of test results.
Best Practices for Retry Mechanism
  1. Limit retries: Avoid infinite retries; set a max retry count to prevent long test runs.
  2. Use explicit waits: Combine retries with Selenium waits to handle dynamic page loads.
  3. Isolate flaky tests: Mark flaky tests clearly to focus on fixing root causes later.
  4. Keep retries transparent: Report retry attempts in test reports for clear visibility.
  5. Use decorators or plugins: Implement retries cleanly using PyTest plugins or custom decorators.
Self Check

Where in this folder structure would you add a new retry decorator to handle flaky tests?

Key Result
Use a retry utility layer with decorators or PyTest plugins to handle flaky tests cleanly and transparently.