0
0
Selenium Pythontesting~8 mins

Explicit waits (WebDriverWait) in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Explicit waits (WebDriverWait)
Folder Structure
selenium-python-project/
├── src/
│   ├── pages/
│   │   ├── __init__.py
│   │   └── login_page.py
│   ├── tests/
│   │   ├── __init__.py
│   │   └── test_login.py
│   ├── utils/
│   │   ├── __init__.py
│   │   └── wait_helpers.py
│   └── config/
│       ├── __init__.py
│       └── settings.py
├── requirements.txt
└── pytest.ini
    
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., ChromeDriver).
  • Page Objects: Classes representing web pages with locators and methods using explicit waits for elements.
  • Tests: Test cases that use page objects to perform actions and assertions.
  • Utilities: Helper functions for explicit waits (e.g., wait until element is clickable).
  • Config: Settings for environment URLs, browser options, and timeouts.
Configuration Patterns

Use a settings.py file to store environment URLs, default wait times, and browser options.

# settings.py
BASE_URL = "https://example.com"
DEFAULT_WAIT_TIME = 10  # seconds
BROWSER = "chrome"
    

Use environment variables or command-line options to switch environments or browsers.

Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html for readable HTML reports.
  • Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on code changes.
  • Capture screenshots on test failures to help debug waits and element visibility issues.
Best Practices
  1. Use explicit waits instead of implicit waits: Explicit waits wait for specific conditions, reducing flaky tests.
  2. Centralize wait helpers: Put explicit wait functions in utilities to reuse and maintain easily.
  3. Wait for meaningful conditions: Wait for elements to be clickable, visible, or present before interacting.
  4. Set reasonable timeout values: Avoid too short or too long waits to balance speed and reliability.
  5. Use Page Object Model: Encapsulate waits inside page methods to keep tests clean and readable.
Self Check

Where in this folder structure would you add a new explicit wait helper function to wait for an element to be visible?

Key Result
Use explicit waits in utility helpers and page objects to improve test reliability and readability.