0
0
Selenium Pythontesting~8 mins

Implicit waits in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Implicit waits
Folder Structure
selenium_project/
├── tests/
│   ├── test_login.py
│   └── test_search.py
├── pages/
│   ├── base_page.py
│   ├── login_page.py
│   └── search_page.py
├── utils/
│   ├── driver_factory.py
│   └── wait_utils.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
└── requirements.txt
Test Framework Layers
  • Driver Layer: driver_factory.py creates and manages the Selenium WebDriver instance with implicit waits configured.
  • Page Objects: Classes in pages/ represent web pages. They use the driver with implicit waits to find elements.
  • Tests: Scripts in tests/ contain test cases that use page objects to perform actions and assertions.
  • Utilities: Helper functions like wait utilities (explicit waits if needed) in wait_utils.py complement implicit waits.
  • Configuration: YAML files in config/ store environment settings, browser options, and credentials.
Configuration Patterns

Use config.yaml to define environment URLs and browser types. For example:

environment: "staging"
browser: "chrome"
implicit_wait_seconds: 10

In driver_factory.py, read these settings and set the implicit wait like this:

from selenium import webdriver
import yaml

with open('config/config.yaml') as f:
    config = yaml.safe_load(f)

browser = config['browser']
implicit_wait = config['implicit_wait_seconds']

def create_driver():
    if browser == 'chrome':
        driver = webdriver.Chrome()
    elif browser == 'firefox':
        driver = webdriver.Firefox()
    else:
        raise ValueError('Unsupported browser')
    driver.implicitly_wait(implicit_wait)  # Set implicit wait here
    return driver

This way, you can change wait times or browsers without changing code.

Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable HTML reports after test runs.
  • Configure CI/CD pipelines (GitHub Actions, Jenkins) to run tests automatically on code push.
  • Reports show which tests passed or failed, helping quickly spot issues related to timing or element loading.
  • Implicit waits reduce flaky failures caused by slow page loads, improving report stability.
Best Practices for Implicit Waits Framework Design
  1. Set implicit wait once: Configure implicit wait when creating the driver to avoid repetition and confusion.
  2. Keep implicit wait moderate: Use a reasonable time (e.g., 5-10 seconds) to balance test speed and reliability.
  3. Avoid mixing waits carelessly: Use implicit waits mainly; if explicit waits are needed, use them carefully to prevent conflicts.
  4. Centralize driver setup: Manage driver creation and implicit wait settings in one place (driver_factory.py) for easy maintenance.
  5. Use configuration files: Store wait times and browser choices in config files for flexibility without code changes.
Self Check

Where in this folder structure would you add a new page object class for a "Profile" page?

Key Result
Configure implicit waits centrally in the driver setup to handle element loading delays smoothly.