0
0
Selenium Pythontesting~8 mins

Default content switching in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Default content switching
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_default_content_switching.py
│   └── __init__.py
├── pages/
│   ├── base_page.py
│   ├── iframe_page.py
│   └── __init__.py
├── utils/
│   ├── driver_factory.py
│   └── __init__.py
├── config/
│   ├── config.yaml
│   └── __init__.py
├── reports/
│   └── (test reports here)
├── requirements.txt
└── pytest.ini
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (utils/driver_factory.py).
  • Page Objects: Encapsulate page elements and actions, including iframe handling (pages/base_page.py, pages/iframe_page.py).
  • Tests: Contains test cases that use page objects to perform actions and assertions (tests/test_default_content_switching.py).
  • Utilities: Helper functions and WebDriver management (utils/driver_factory.py).
  • Configuration: Environment settings, URLs, browser options stored in config/config.yaml.
Configuration Patterns
  • Environment Settings: Use config/config.yaml to store URLs and environment-specific data.
  • Browser Options: Define browser type and options in driver_factory.py to allow easy switching.
  • Credentials: Store sensitive data securely outside the repo or use environment variables (not shown here for simplicity).
  • Pytest.ini: Configure pytest options and markers for test runs.
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html or pytest-allure to generate readable HTML reports saved in reports/.
  • Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Configure test failure screenshots and logs to help diagnose iframe switching issues.
Best Practices for Default Content Switching
  • Always switch back to default content: After working inside an iframe, call driver.switch_to.default_content() to avoid stale element errors.
  • Use explicit waits: Wait for iframe presence before switching to avoid timing issues.
  • Encapsulate iframe logic in page objects: Keep iframe switching inside page methods to keep tests clean.
  • Keep tests independent: Each test should start from a known state, including default content.
  • Use clear naming: Name iframe elements and switching methods clearly for readability.
Self Check

Where in this framework structure would you add a new method to switch to a specific iframe on the page?

Key Result
Organize Selenium Python tests with clear layers and always switch back to default content after iframe interactions.