0
0
Selenium Javatesting~8 mins

Network interception (CDP) in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Network interception (CDP)
Folder Structure
src/
└── test/
    └── java/
        └── com/
            └── example/
                ├── base/
                │   └── WebDriverManager.java       # Manages WebDriver and CDP sessions
                ├── pages/
                │   └── LoginPage.java             # Page Object Model classes
                ├── tests/
                │   └── LoginTest.java             # Test classes using CDP for network interception
                ├── utils/
                │   └── NetworkInterceptor.java   # Helper for CDP network interception
                └── config/
                    └── ConfigReader.java         # Reads environment and browser configs
Test Framework Layers
  • Driver Layer: WebDriverManager.java handles browser setup and creates CDP sessions for network interception.
  • Page Objects: Classes like LoginPage.java encapsulate UI elements and user actions.
  • Tests: Test classes (e.g., LoginTest.java) use page objects and invoke network interception to verify requests/responses.
  • Utilities: NetworkInterceptor.java provides methods to start, monitor, and analyze network traffic via CDP.
  • Configuration: ConfigReader.java manages environment variables, browser types, and credentials for flexible test runs.
Configuration Patterns
  • Environment Setup: Use property files or environment variables to define URLs, credentials, and environment types (dev, staging, prod).
  • Browser Selection: Configure browser type (Chrome) and enable CDP support in WebDriverManager.
  • CDP Session Initialization: WebDriverManager creates a DevTools session to enable network interception before tests run.
  • Credentials Management: Store sensitive data securely outside the codebase, accessed via ConfigReader.
Test Reporting and CI/CD Integration
  • Use TestNG or JUnit reports to capture test execution results including network interception validations.
  • Integrate with CI/CD pipelines (Jenkins, GitHub Actions) to run tests on code commits and pull requests.
  • Capture network logs and attach them to test reports for debugging failed tests.
  • Use screenshots and logs to enhance report clarity when network interception detects unexpected requests or responses.
Framework Design Principles
  1. Separate Concerns: Keep network interception logic in utility classes, not mixed with page objects or tests.
  2. Reusable CDP Sessions: Initialize DevTools sessions once per test or suite to improve performance.
  3. Explicit Waits for Network Events: Use waits or listeners to ensure network requests are captured before assertions.
  4. Data-Driven Testing: Use external data sources to test different network scenarios and payloads.
  5. Secure Configuration: Avoid hardcoding sensitive info; use environment variables or encrypted files.
Self Check

Where in this folder structure would you add a new utility class to mock network responses using CDP?

Key Result
Use a layered Selenium Java framework with WebDriverManager handling CDP sessions and utilities managing network interception separately.