0
0
Selenium Pythontesting~8 mins

Custom expected conditions in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Custom expected conditions
Folder Structure
selenium_project/
├── tests/
│   ├── test_login.py
│   └── test_dashboard.py
├── pages/
│   ├── login_page.py
│   └── dashboard_page.py
├── expected_conditions/
│   └── custom_conditions.py
├── utils/
│   ├── browser_factory.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── conftest.py
└── requirements.txt
  
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown (e.g., browser_factory.py).
  • Page Objects: Encapsulate page elements and actions (e.g., login_page.py).
  • Custom Expected Conditions: Contains reusable wait conditions beyond built-in ones (e.g., custom_conditions.py).
  • Tests: Test cases using page objects and expected conditions (e.g., test_login.py).
  • Utilities: Helper functions and common utilities (e.g., helpers.py).
  • Configuration: Environment settings and credentials (e.g., config.yaml).
Configuration Patterns
  • Environment Files: Use YAML files (config/config.yaml) to store URLs, timeouts, and browser preferences.
  • Credentials: Store sensitive data separately in credentials.yaml and load securely.
  • Browser Selection: Parameterize browser choice in conftest.py or browser_factory.py for flexibility.
  • Timeouts: Define explicit wait times in config to use in custom expected conditions.
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html or allure-pytest for clear, readable reports.
  • Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run on every code push.
  • Configure reports to be saved as artifacts for easy access after test runs.
  • Include screenshots on failure using hooks in conftest.py to help debug custom expected condition failures.
Framework Design Principles
  1. Single Responsibility: Keep custom expected conditions focused on one clear wait condition.
  2. Reusability: Write custom conditions as reusable functions or classes to avoid duplication.
  3. Explicit Waits: Use custom expected conditions with Selenium's WebDriverWait for reliable synchronization.
  4. Readable Naming: Name custom conditions clearly to describe what they wait for, improving test readability.
  5. Parameterization: Allow custom conditions to accept parameters for flexibility (e.g., element locator, text to wait for).
Self Check

Where would you add a new custom expected condition that waits for an element's CSS class to change?

Key Result
Organize custom expected conditions in a dedicated folder to create reusable, clear, and reliable wait conditions integrated with page objects and tests.