0
0
Selenium Javatesting~8 mins

Switching between windows in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Switching between windows
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   ├── BasePage.java
                │   ├── HomePage.java
                │   └── NewWindowPage.java
                ├── tests/
                │   └── WindowSwitchTest.java
                ├── utils/
                │   └── WindowManager.java
                └── config/
                    └── TestConfig.java
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., WebDriverFactory class).
  • Page Objects: Classes representing pages/windows (e.g., HomePage, NewWindowPage) with methods to interact with UI elements.
  • Window Manager Utility: Helper class (WindowManager) to handle switching between browser windows or tabs safely.
  • Tests: Test classes (WindowSwitchTest) containing test methods that use page objects and utilities to perform window switching scenarios.
  • Configuration: Centralized config (TestConfig) for environment URLs, browser types, timeouts, and credentials.
Configuration Patterns
  • Environment Properties: Use a properties file or Java class (TestConfig) to store URLs for different environments (dev, staging, prod).
  • Browser Selection: Parameterize browser choice via system properties or config files to run tests on Chrome, Firefox, etc.
  • Timeouts: Define implicit and explicit wait times centrally to handle dynamic page loads and window switching delays.
  • Credentials: Store sensitive data securely outside source code, inject at runtime or use environment variables.
Test Reporting and CI/CD Integration
  • TestNG Reports: Use TestNG's built-in HTML reports to show test pass/fail status including window switching tests.
  • Logging: Add logs in WindowManager and tests to trace window handles and switching actions for easier debugging.
  • CI/CD: Integrate with Jenkins or GitHub Actions to run tests on code commits, with reports archived and notifications sent.
  • Screenshots: Capture screenshots on test failures, especially after window switches, to help identify UI issues.
Best Practices
  1. Use Page Object Model: Encapsulate window-specific actions in page classes to keep tests clean and maintainable.
  2. Explicit Waits for Windows: Use explicit waits to wait for new window handles before switching to avoid flaky tests.
  3. Window Handle Management: Store original window handle before opening new windows and switch back after test steps.
  4. Utility Methods: Create reusable methods in WindowManager for switching by title, handle, or index to simplify test code.
  5. Clean Up: Close extra windows after tests to avoid resource leaks and keep test environment stable.
Self Check

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

Key Result
Organize Selenium Java tests with page objects, utilities for window switching, and centralized config for clean, maintainable automation.