0
0
Selenium Pythontesting~8 mins

Confirmation alert handling in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Confirmation alert handling
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_confirmation_alert.py
│   └── __init__.py
├── pages/
│   ├── base_page.py
│   ├── alert_page.py
│   └── __init__.py
├── utils/
│   ├── driver_factory.py
│   ├── wait_utils.py
│   └── __init__.py
├── config/
│   ├── config.yaml
│   └── __init__.py
├── reports/
│   └── (test reports here)
├── conftest.py
└── requirements.txt
Test Framework Layers
  • Driver Layer: utils/driver_factory.py manages browser setup and teardown.
  • Page Objects: pages/alert_page.py contains methods to interact with confirmation alerts.
  • Test Cases: tests/test_confirmation_alert.py holds test functions that verify alert handling.
  • Utilities: utils/wait_utils.py provides explicit wait helpers for alert presence.
  • Configuration: config/config.yaml stores environment and browser settings.
  • Fixtures: conftest.py defines reusable setup and teardown for tests.
Configuration Patterns

Use a YAML file (config/config.yaml) to store environment URLs, browser types, and credentials. Example:

environment:
  base_url: "https://example.com"
browser: "chrome"
credentials:
  username: "testuser"
  password: "password123"

Load this config in conftest.py to initialize the driver and pass settings to tests. This keeps tests flexible and easy to run on different browsers or environments without code changes.

Test Reporting and CI/CD Integration
  • Use pytest with pytest-html plugin to generate readable HTML reports after test runs.
  • Store reports in the reports/ folder for easy access.
  • Integrate tests into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Fail the build if confirmation alert handling tests fail, ensuring alert flows remain stable.
Best Practices
  1. Use Explicit Waits: Always wait for the alert to be present before interacting to avoid flaky tests.
  2. Page Object Model: Encapsulate alert handling methods inside page objects for reusability and clarity.
  3. Clear Test Assertions: Assert alert text and confirm or dismiss actions to verify correct behavior.
  4. Isolate Tests: Each test should handle its own alert to avoid side effects.
  5. Configurable Browsers: Allow running tests on different browsers by reading from config files.
Self Check

Where would you add a new method to handle a confirmation alert that appears after clicking a "Delete" button?

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