0
0
Selenium Pythontesting~8 mins

Handling pop-up windows in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Handling pop-up windows
Folder Structure
my_selenium_project/
├── src/
│   ├── pages/
│   │   ├── base_page.py
│   │   ├── main_page.py
│   │   └── popup_page.py
│   ├── tests/
│   │   ├── test_main_page.py
│   │   └── test_popup_handling.py
│   ├── utils/
│   │   ├── driver_factory.py
│   │   └── wait_utils.py
│   └── config/
│       ├── config.yaml
│       └── credentials.yaml
├── reports/
│   └── test_report.html
├── requirements.txt
└── pytest.ini
Test Framework Layers
  • Driver Layer: driver_factory.py creates and manages WebDriver instances with browser options.
  • Page Objects: Classes in pages/ represent web pages and pop-ups, encapsulating locators and actions (e.g., popup_page.py handles pop-up window interactions).
  • Test Layer: Test scripts in tests/ use page objects to perform test scenarios, including pop-up handling.
  • Utilities: Helper functions like explicit waits in wait_utils.py to handle timing and synchronization.
  • Configuration: YAML files in config/ store environment settings, browser choices, and credentials.
Configuration Patterns
  • Environment Settings: Use config.yaml to define URLs and environment-specific data (dev, staging, prod).
  • Browser Selection: Pass browser type (e.g., Chrome, Firefox) via command line or config file to driver_factory.py.
  • Credentials: Store sensitive data like usernames and passwords securely in credentials.yaml, excluded from version control.
  • Pop-up Handling Settings: Timeout values for waiting on pop-ups can be configured in config.yaml for flexibility.
Test Reporting and CI/CD Integration
  • Use pytest with plugins like pytest-html to generate readable HTML test reports saved in reports/.
  • Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Configure CI to run tests in headless mode for pop-up handling to simulate real user scenarios without UI.
  • Reports include pass/fail status, screenshots on failure, and logs for pop-up window interactions.
Best Practices for Handling Pop-up Windows
  1. Use Explicit Waits: Always wait explicitly for pop-up windows or alerts to appear before interacting to avoid flaky tests.
  2. Switch Context Properly: Use Selenium's driver.switch_to.window() or driver.switch_to.alert() to interact with pop-ups.
  3. Encapsulate Pop-up Logic: Create dedicated page objects or helper methods for pop-up handling to keep tests clean and reusable.
  4. Close Pop-ups Cleanly: Always close or accept pop-ups after interaction to return to the main window and avoid test interference.
  5. Handle Unexpected Pop-ups: Implement global handlers or try-except blocks to manage unexpected alerts gracefully.
Self Check

Where in this folder structure would you add a new page object class to handle a confirmation pop-up window?

Key Result
Organize Selenium Python tests with clear layers: driver setup, page objects including pop-ups, tests, utilities, and config for robust pop-up handling.