0
0
Selenium Javatesting~8 mins

Window handles (getWindowHandles) in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Window handles (getWindowHandles)
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── pages/
                │   ├── MainPage.java
                │   └── PopupPage.java
                ├── tests/
                │   └── WindowHandleTests.java
                ├── utils/
                │   └── WebDriverManager.java
                └── config/
                    └── TestConfig.java
Test Framework Layers
  • Driver Layer: WebDriverManager.java manages browser driver setup and teardown.
  • Page Objects: Classes like MainPage.java and PopupPage.java encapsulate page elements and actions, including window handle logic.
  • Tests: WindowHandleTests.java contains test methods that use getWindowHandles() to switch between windows.
  • Utilities: Helper methods for window switching, waits, and common actions.
  • Configuration: TestConfig.java holds environment settings, browser types, and credentials.
Configuration Patterns
  • Use TestConfig.java to define environment URLs, browser types (Chrome, Firefox), and timeouts.
  • Use system properties or a config file (e.g., config.properties) to switch environments without code changes.
  • Manage credentials securely, avoid hardcoding; use environment variables or encrypted files.
  • Configure browser options (headless, window size) in WebDriverManager.java.
Test Reporting and CI/CD Integration
  • Use TestNG reports or Allure for detailed test execution reports showing pass/fail and screenshots on failure.
  • Integrate tests into CI/CD pipelines (Jenkins, GitHub Actions) to run on code commits.
  • Configure reports to archive and notify teams on test results.
  • Include logs for window handle switches to help debug multi-window tests.
Best Practices
  • Use Page Object Model to encapsulate window handle logic inside page classes.
  • Use explicit waits to ensure new windows are fully loaded before switching.
  • Always store the original window handle before opening new windows to switch back safely.
  • Use descriptive method names like switchToNewWindow() for clarity.
  • Close popup windows after tests to avoid resource leaks.
Self Check

Where in this folder structure would you add a new page object class to handle a popup window that opens after clicking a button?

Key Result
Organize Selenium Java tests with clear layers: driver management, page objects handling window switches, tests, utilities, and config.