0
0
Selenium Javatesting~10 mins

Network interception (CDP) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses Selenium with Chrome DevTools Protocol (CDP) to intercept network requests. It verifies that a specific API call is made and checks its response status.

Test Code - TestNG
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v114.network.Network;
import org.openqa.selenium.devtools.v114.network.model.Response;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

public class NetworkInterceptionTest {
    private ChromeDriver driver;
    private DevTools devTools;
    private AtomicBoolean apiCallMade;

    @BeforeClass
    public void setUp() {
        driver = new ChromeDriver();
        devTools = driver.getDevTools();
        devTools.createSession();
        apiCallMade = new AtomicBoolean(false);

        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

        devTools.addListener(Network.responseReceived(), responseReceived -> {
            Response response = responseReceived.getResponse();
            if (response.getUrl().contains("/api/data")) {
                if (response.getStatus() == 200) {
                    apiCallMade.set(true);
                }
            }
        });
    }

    @Test
    public void testApiCallIsMade() {
        driver.get("https://example.com");
        driver.findElement(By.id("loadDataBtn")).click();

        // Wait briefly to allow network events to be captured
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        Assert.assertTrue(apiCallMade.get(), "Expected API call to /api/data was not made or did not return 200 status.");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and ChromeDriver launches browserBrowser window opens with DevTools session created and network interception enabled-PASS
2Navigates to https://example.comPage loads with a button having id 'loadDataBtn' visible-PASS
3Finds element with id 'loadDataBtn' and clicks itButton clicked, triggering network requests-PASS
4Waits 2 seconds to capture network eventsNetwork events captured including response for /api/dataChecks if response for /api/data has status 200PASS
5Asserts that API call to /api/data was made and returned 200Assertion verifies apiCallMade is trueAssert.assertTrue(apiCallMade.get())PASS
6Test ends and browser closesBrowser window closes-PASS
Failure Scenario
Failing Condition: The API call to /api/data is not made or returns a status other than 200
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the network request?
AThat the button with id 'loadDataBtn' is visible
BThat the API call to /api/data is made and returns status 200
CThat the page title contains 'Example'
DThat the browser window opens successfully
Key Result
Using Chrome DevTools Protocol (CDP) with Selenium allows you to monitor and verify network requests directly, which is useful for testing API calls triggered by UI actions.