What if you could control every web request your test makes, like a puppet master behind the scenes?
Why Network interception (CDP) in Selenium Java? - Purpose & Use Cases
Imagine testing a web app where you need to check every network request manually by watching browser logs or using external tools.
You have to guess if the right data was sent or received, and you can't easily change or block requests on the fly.
Manually tracking network calls is slow and confusing.
You might miss important requests or responses.
It's hard to repeat tests exactly the same way, and fixing bugs takes longer.
Network interception with CDP lets you watch, modify, or block network requests directly inside your test code.
This means you can control what the browser sends and receives, making tests faster, more reliable, and easier to debug.
System.out.println("Check logs manually for network calls");devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTools.addListener(Network.requestWillBeSent(), request -> { System.out.println(request.getRequest().getUrl()); });You can fully control and verify network traffic during tests, catching hidden bugs and simulating real-world scenarios easily.
Testing a login page where you intercept the login request to simulate a server error without changing the backend.
Manual network checks are slow and unreliable.
CDP network interception gives direct control over browser traffic.
This makes tests faster, clearer, and more powerful.
Practice
Solution
Step 1: Understand Network Interception
Network interception allows tests to watch and manipulate network traffic between the browser and server.Step 2: Identify the purpose in Selenium CDP context
Selenium's DevTools Protocol integration enables listening to and controlling network events during tests.Final Answer:
To monitor and control network requests and responses during tests -> Option CQuick Check:
Network interception = monitor and control traffic [OK]
- Thinking it speeds up tests by skipping UI
- Confusing it with test data generation
- Assuming it replaces WebDriver
Solution
Step 1: Check correct DevTools session creation
In Selenium 4+, use driver.getDevTools() and then createSession() to start.Step 2: Verify enabling network interception
Use devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty())) to enable network events.Final Answer:
DevTools devTools = driver.getDevTools(); devTools.createSession(); devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty())); -> Option DQuick Check:
Use getDevTools(), createSession(), then send Network.enable [OK]
- Trying to instantiate DevTools with new keyword
- Calling Network.enable() without devTools.send()
- Missing createSession() call
DevTools devTools = driver.getDevTools();
devTools.createSession();
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTools.addListener(Network.requestWillBeSent(), request -> {
System.out.println("Request URL: " + request.getRequest().getUrl());
});
// Browser navigates to https://example.comSolution
Step 1: Understand listener setup
The listener is added to Network.requestWillBeSent event, printing the URL of each request.Step 2: Predict output on navigating to example.com
When browser navigates, a request is sent to https://example.com/, triggering the listener and printing the URL.Final Answer:
Request URL: https://example.com/ -> Option AQuick Check:
Listener prints request URL on navigation [OK]
- Assuming listener won't trigger without explicit start
- Expecting null URL in request
- Thinking listener setup is incorrect
DevTools devTools = driver.getDevTools();
devTools.createSession();
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTools.addListener(Network.requestWillBeSent(), request -> {
if(request.getRequest().getUrl().endsWith(".png")) {
devTools.send(Network.setBlockedURLs(List.of(request.getRequest().getUrl())));
}
});Solution
Step 1: Analyze listener behavior
The listener calls setBlockedURLs every time a request ends with .png, causing repeated calls.Step 2: Understand correct usage of setBlockedURLs
setBlockedURLs should be called once with all URLs to block, not repeatedly inside the listener.Final Answer:
Calling setBlockedURLs inside the listener causes repeated blocking calls -> Option AQuick Check:
setBlockedURLs called repeatedly inside listener is wrong [OK]
- Calling setBlockedURLs repeatedly inside listener
- Forgetting to create session before enabling network
- Misusing string methods for URL filtering
Solution
Step 1: Understand response modification with CDP
To modify a response, intercept it, get the body, change it, and continue with the new content.Step 2: Identify correct event and method usage
Use Network.requestIntercepted to detect response, fetch body, then Network.continueInterceptedRequest to send modified response.Final Answer:
Enable Network.requestIntercepted event, fetch response body, modify it, then use Network.continueInterceptedRequest with modified body -> Option BQuick Check:
Modify response by intercepting and continuing with new body [OK]
- Trying to modify request instead of response
- Blocking URL instead of modifying response
- Changing DOM instead of network response
Ready to prove it?
Pass the test and this pattern moves unknown
