0
0
Selenium Pythontesting~8 mins

Expected conditions in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Expected conditions
Folder Structure
selenium_project/
├── tests/
│   ├── test_login.py
│   ├── test_search.py
│   └── __init__.py
├── pages/
│   ├── base_page.py
│   ├── login_page.py
│   └── search_page.py
├── utils/
│   ├── wait_utils.py
│   └── __init__.py
├── config/
│   ├── config.yaml
│   └── __init__.py
├── conftest.py
└── requirements.txt
  
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (in conftest.py using pytest fixtures).
  • Page Objects: Classes representing web pages with locators and methods (pages/ folder).
  • Expected Conditions Utilities: Custom or Selenium built-in waits using expected_conditions in utils/wait_utils.py.
  • Tests: Test cases using pytest in tests/ folder, calling page objects and wait utilities.
  • Configuration: Environment settings like URLs, browser types, credentials in config/config.yaml.
Configuration Patterns

Use a YAML file (config/config.yaml) to store environment variables such as:

  • Base URL
  • Browser type (chrome, firefox)
  • User credentials

Load config in conftest.py and pass to tests and page objects.

Example snippet from config.yaml:

base_url: "https://example.com"
browser: "chrome"
credentials:
  username: "testuser"
  password: "password123"
  
Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate HTML reports.
  • Reports show which tests passed or failed, with screenshots on failure.
  • Integrate tests in CI/CD pipelines (GitHub Actions, Jenkins) to run on each commit.
  • Failing tests block deployment until fixed.
Best Practices for Expected Conditions Framework
  • Use explicit waits: Always wait for elements or conditions before interacting to avoid flaky tests.
  • Centralize wait logic: Put expected conditions in utility functions to reuse and maintain easily.
  • Prefer Selenium built-in expected_conditions: Use selenium.webdriver.support.expected_conditions for common waits.
  • Handle timeouts gracefully: Set reasonable wait times and catch exceptions to log failures clearly.
  • Keep tests readable: Use descriptive method names and comments for wait conditions.
Self Check

Where would you add a new expected condition utility function that waits for an element to be clickable?

Key Result
Use explicit waits with expected conditions centralized in utility modules to make Selenium tests reliable and maintainable.