Bird
Raised Fist0
Selenium Javatesting~20 mins

Network interception (CDP) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CDP Network Interception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium CDP network interception code?

Consider the following Java Selenium code snippet using Chrome DevTools Protocol (CDP) to intercept network requests and block images. What will be the output printed to the console?

Selenium Java
DevTools devTools = driver.getDevTools();
devTools.createSession();

// Enable Network domain
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

// Block image requests
devTools.send(Network.setBlockedURLs(List.of("*://*.png", "*://*.jpg")));

// Add listener to print request URLs
devTools.addListener(Network.requestWillBeSent(), request -> {
    System.out.println("Request URL: " + request.getRequest().getUrl());
});

// Navigate to a page with images
driver.get("https://example.com");

System.out.println("Page loaded");
ARequest URLs printed for all resources including images; then 'Page loaded' printed
BRequest URLs printed for all resources except images; then 'Page loaded' printed
CNo request URLs printed; only 'Page loaded' printed
DThrows runtime exception due to missing permissions
Attempts:
2 left
💡 Hint

Think about what Network.setBlockedURLs does and how listeners behave.

❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies a blocked image request using Selenium CDP?

You want to assert that an image request was blocked by CDP network interception. Which assertion correctly checks this in Java?

Selenium Java
AtomicBoolean imageBlocked = new AtomicBoolean(false);
devTools.addListener(Network.loadingFailed(), loadingFailed -> {
    if (loadingFailed.getType() == ResourceType.IMAGE && loadingFailed.getBlockedReason() != null) {
        imageBlocked.set(true);
    }
});

// Trigger page load with images
// ...

// Which assertion below is correct?
AassertFalse(imageBlocked.get(), "Image request was blocked");
BassertNull(imageBlocked.get(), "Image request was blocked");
CassertTrue(imageBlocked.get(), "Image request was blocked");
DassertEquals(false, imageBlocked.get(), "Image request was blocked");
Attempts:
2 left
💡 Hint

Think about what imageBlocked represents after the listener runs.

🔧 Debug
advanced
2:00remaining
Why does this Selenium CDP network interception code not block images as expected?

Review the following code snippet intended to block image requests. Why does it fail to block images?

Selenium Java
DevTools devTools = driver.getDevTools();
devTools.createSession();

// Enable Network domain
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

// Attempt to block images
devTools.send(Network.setBlockedURLs(List.of("*.png", "*.jpg")));

// Navigate to page
driver.get("https://example.com");
AThe URL patterns '*.png' and '*.jpg' are invalid; they must be full URLs or regex
BNetwork.setBlockedURLs must be called before Network.enable
CThe driver does not support CDP commands
DThe session was not created before sending commands
Attempts:
2 left
💡 Hint

Check the format expected by setBlockedURLs for URL patterns.

❓ framework
advanced
2:00remaining
Which Selenium Java code snippet correctly sets up CDP network interception to modify request headers?

You want to add a custom header to all outgoing HTTP requests using Selenium CDP in Java. Which code snippet correctly achieves this?

A
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTools.send(Network.setBlockedURLs(List.of("*")));
driver.get("https://example.com");
B
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTools.addListener(Network.requestWillBeSent(), event -> {
    Map<String, Object> headers = new HashMap<>(event.getRequest().getHeaders());
    headers.put("X-Custom-Header", "MyValue");
    devTools.send(Network.setExtraHTTPHeaders(new Headers(headers)));
});
C
devTools.createSession();
devTools.send(Network.setExtraHTTPHeaders(new Headers(Map.of("X-Custom-Header", "MyValue"))));
driver.get("https://example.com");
D
devTools.createSession();
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
Map<String, String> headers = Map.of("X-Custom-Header", "MyValue");
devTools.send(Network.setExtraHTTPHeaders(new Headers(headers)));
driver.get("https://example.com");
Attempts:
2 left
💡 Hint

Consider when and how to set extra HTTP headers for all requests.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using Selenium CDP network interception over traditional proxy-based interception?

Choose the best explanation for why Selenium's CDP network interception is preferred over using an external proxy for network control in browser automation.

ACDP network interception integrates directly with the browser, providing lower latency and more accurate control without external dependencies
BProxy-based interception is faster and more reliable than CDP, so CDP is rarely used
CCDP requires no browser support and works with all browsers equally
DProxy-based interception can modify requests, but CDP cannot
Attempts:
2 left
💡 Hint

Think about integration level and performance.

Practice

(1/5)
1. What is the main purpose of using Network Interception with Selenium DevTools Protocol (CDP)?
easy
A. To replace Selenium WebDriver with a faster browser engine
B. To speed up test execution by skipping UI rendering
C. To monitor and control network requests and responses during tests
D. To automatically generate test data from network traffic

Solution

  1. Step 1: Understand Network Interception

    Network interception allows tests to watch and manipulate network traffic between the browser and server.
  2. Step 2: Identify the purpose in Selenium CDP context

    Selenium's DevTools Protocol integration enables listening to and controlling network events during tests.
  3. Final Answer:

    To monitor and control network requests and responses during tests -> Option C
  4. Quick Check:

    Network interception = monitor and control traffic [OK]
Hint: Network interception means watching and controlling web traffic [OK]
Common Mistakes:
  • Thinking it speeds up tests by skipping UI
  • Confusing it with test data generation
  • Assuming it replaces WebDriver
2. Which Java code snippet correctly creates a DevTools session to enable network interception in Selenium 4+?
easy
A. DevTools devTools = driver.getDevTools(); devTools.startSession(); Network.enable();
B. DevTools devTools = new DevTools(); devTools.startSession(); devTools.enableNetwork();
C. DevTools devTools = driver.createDevTools(); devTools.start(); Network.enable();
D. DevTools devTools = driver.getDevTools(); devTools.createSession(); devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

Solution

  1. Step 1: Check correct DevTools session creation

    In Selenium 4+, use driver.getDevTools() and then createSession() to start.
  2. Step 2: Verify enabling network interception

    Use devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty())) to enable network events.
  3. Final Answer:

    DevTools devTools = driver.getDevTools(); devTools.createSession(); devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty())); -> Option D
  4. Quick Check:

    Use getDevTools(), createSession(), then send Network.enable [OK]
Hint: Use driver.getDevTools() then createSession() before enabling network [OK]
Common Mistakes:
  • Trying to instantiate DevTools with new keyword
  • Calling Network.enable() without devTools.send()
  • Missing createSession() call
3. Given the following code snippet, what will be printed when a network request is intercepted?
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.com
medium
A. Request URL: https://example.com/
B. Request URL: null
C. No output because listener is not added correctly
D. Compilation error due to missing imports

Solution

  1. Step 1: Understand listener setup

    The listener is added to Network.requestWillBeSent event, printing the URL of each request.
  2. 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.
  3. Final Answer:

    Request URL: https://example.com/ -> Option A
  4. Quick Check:

    Listener prints request URL on navigation [OK]
Hint: Listener prints URL of each request sent during navigation [OK]
Common Mistakes:
  • Assuming listener won't trigger without explicit start
  • Expecting null URL in request
  • Thinking listener setup is incorrect
4. Identify the error in this Selenium Java code snippet for blocking image requests using CDP:
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())));
    }
});
medium
A. Calling setBlockedURLs inside the listener causes repeated blocking calls
B. Missing call to devTools.createSession() before enabling network
C. Using endsWith(".png") is invalid for URL filtering
D. Network.enable() should not be called with Optional.empty()

Solution

  1. Step 1: Analyze listener behavior

    The listener calls setBlockedURLs every time a request ends with .png, causing repeated calls.
  2. Step 2: Understand correct usage of setBlockedURLs

    setBlockedURLs should be called once with all URLs to block, not repeatedly inside the listener.
  3. Final Answer:

    Calling setBlockedURLs inside the listener causes repeated blocking calls -> Option A
  4. Quick Check:

    setBlockedURLs called repeatedly inside listener is wrong [OK]
Hint: Call setBlockedURLs once, not inside each request event [OK]
Common Mistakes:
  • Calling setBlockedURLs repeatedly inside listener
  • Forgetting to create session before enabling network
  • Misusing string methods for URL filtering
5. You want to modify the response body of a JSON API call during a Selenium test using CDP. Which approach correctly achieves this?
hard
A. Use Network.requestWillBeSent to change the request URL to a local JSON file
B. Enable Network.requestIntercepted event, fetch response body, modify it, then use Network.continueInterceptedRequest with modified body
C. Call Network.setBlockedURLs to block the API call and serve a local file instead
D. Modify the DOM after page load to replace JSON data

Solution

  1. Step 1: Understand response modification with CDP

    To modify a response, intercept it, get the body, change it, and continue with the new content.
  2. Step 2: Identify correct event and method usage

    Use Network.requestIntercepted to detect response, fetch body, then Network.continueInterceptedRequest to send modified response.
  3. Final Answer:

    Enable Network.requestIntercepted event, fetch response body, modify it, then use Network.continueInterceptedRequest with modified body -> Option B
  4. Quick Check:

    Modify response by intercepting and continuing with new body [OK]
Hint: Intercept response, modify body, then continue intercepted request [OK]
Common Mistakes:
  • Trying to modify request instead of response
  • Blocking URL instead of modifying response
  • Changing DOM instead of network response
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard