0
0
Selenium Pythontesting~8 mins

Switching between windows in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Switching between windows
Folder Structure
selenium_project/
├── tests/
│   ├── test_switch_windows.py
│   └── __init__.py
├── pages/
│   ├── base_page.py
│   ├── main_page.py
│   └── popup_page.py
├── utils/
│   ├── driver_factory.py
│   └── window_manager.py
├── config/
│   ├── config.yaml
│   └── __init__.py
├── reports/
│   └── test_report.html
├── conftest.py
└── requirements.txt
Test Framework Layers
  • Driver Layer: utils/driver_factory.py creates and manages WebDriver instances.
  • Page Objects: pages/ contains classes representing pages, e.g., MainPage and PopupPage, encapsulating element locators and actions.
  • Window Manager Utility: utils/window_manager.py handles switching between browser windows or tabs safely.
  • Tests: tests/test_switch_windows.py contains test cases that use page objects and utilities to verify window switching behavior.
  • Configuration: config/config.yaml stores environment settings like browser type and URLs.
  • Test Runner Setup: conftest.py manages fixtures for setup and teardown of tests.
  • Reports: reports/ stores test execution reports.
Configuration Patterns
  • Environment Settings: Use config/config.yaml to define URLs, browser types (e.g., Chrome, Firefox), and timeouts.
  • Credentials: Store sensitive data securely outside the repo or use environment variables accessed in conftest.py.
  • Browser Selection: Parameterize browser choice in driver_factory.py to allow running tests on different browsers.
  • Window Handles: Use utility functions in window_manager.py to switch windows by handle or title, avoiding hard-coded values.
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable HTML reports saved in reports/.
  • Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run on every code push.
  • Configure CI to run tests in headless mode for faster execution.
  • Fail tests if window switching does not work as expected, ensuring reliability.
Best Practices
  1. Use Page Object Model: Encapsulate window-related actions in page classes to keep tests clean.
  2. Explicit Waits: Wait for new windows or tabs to appear before switching to avoid flaky tests.
  3. Window Handle Management: Always store and restore original window handles to maintain test flow.
  4. Reusable Utilities: Create helper functions for switching windows by title or index to avoid repeating code.
  5. Clear Test Isolation: Close any extra windows after tests to keep environment clean.
Self Check

Where in this folder structure would you add a new utility function to switch to a window by its title?

Key Result
Organize Selenium Python tests with clear layers: driver setup, page objects, utilities for window switching, tests, and config for flexible, maintainable automation.