0
0
Selenium Pythontesting~8 mins

New window and tab creation in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - New window and tab creation
Folder Structure
selenium_project/
├── src/
│   ├── pages/
│   │   ├── base_page.py
│   │   ├── home_page.py
│   │   └── new_window_page.py
│   ├── tests/
│   │   ├── test_new_window.py
│   │   └── test_home.py
│   ├── utils/
│   │   ├── driver_factory.py
│   │   └── window_manager.py
│   └── config/
│       ├── config.yaml
│       └── credentials.yaml
├── requirements.txt
└── pytest.ini
Test Framework Layers
  • Driver Layer: driver_factory.py creates and manages WebDriver instances with options for different browsers.
  • Page Objects: Classes in pages/ represent web pages. For new window/tab tests, new_window_page.py handles window switching methods.
  • Tests: Test scripts in tests/ use page objects and utilities to perform actions and assertions, e.g., test_new_window.py tests new window/tab creation and switching.
  • Utilities: Helper functions like window_manager.py provide reusable methods to switch windows or tabs safely.
  • Configuration: config/ holds environment settings and credentials, loaded dynamically for flexible test runs.
Configuration Patterns
  • Environment Settings: Use config.yaml to define URLs, browser types, and timeouts for different environments (dev, staging, prod).
  • Browser Selection: Pass browser choice via command line or config file; driver_factory.py initializes the correct WebDriver.
  • Credentials: Store sensitive data in credentials.yaml and load securely in tests.
  • Window/Tab Handling: Utilities handle window handles and switching logic to avoid flaky tests.
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html for clear HTML reports showing pass/fail and screenshots on failure.
  • Integrate with CI tools (GitHub Actions, Jenkins) to run tests on each commit or pull request.
  • Configure reports to be archived or sent via email for team visibility.
  • Include logs of window/tab switching steps to help debug failures related to new windows or tabs.
Best Practices
  1. Use Page Object Model: Encapsulate window/tab actions in page classes to keep tests clean and maintainable.
  2. Explicit Waits: Always wait for new windows or tabs to appear before switching to avoid timing issues.
  3. Window Handle Management: Store and switch window handles carefully to return to the original window after tests.
  4. Isolate Tests: Each test should open and close windows/tabs independently to avoid side effects.
  5. Clear Naming: Name window handles or tabs logically in utilities for easier debugging and readability.
Self Check

Where in this folder structure would you add a new utility function to safely switch back to the original browser tab after opening a new tab?

Key Result
Organize Selenium Python tests with clear layers: driver setup, page objects for window/tab actions, utilities for window management, and config for environments.