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?
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");
Think about what Network.setBlockedURLs does and how listeners behave.
The code blocks image requests by URL patterns (*://*.png, *://*.jpg). The listener prints URLs of requests that are sent. Since images are blocked, their requests are not sent, so their URLs are not printed. Finally, 'Page loaded' is printed after navigation.
You want to assert that an image request was blocked by CDP network interception. Which assertion correctly checks this in 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?Think about what imageBlocked represents after the listener runs.
The imageBlocked flag is set to true if an image request failed due to blocking. To verify blocking, assert that this flag is true.
Review the following code snippet intended to block image requests. Why does it fail to block images?
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");
Check the format expected by setBlockedURLs for URL patterns.
The setBlockedURLs method expects URL patterns with scheme and domain parts or valid wildcards. Patterns like '*.png' are not valid and do not match requests, so images are not blocked.
You want to add a custom header to all outgoing HTTP requests using Selenium CDP in Java. Which code snippet correctly achieves this?
Consider when and how to set extra HTTP headers for all requests.
Option D creates a session, enables the Network domain, sets the extra HTTP headers once before navigation, which applies to all requests. Option D tries to set headers inside a listener, which is incorrect. Option D misses enabling Network domain. Option D blocks all URLs, unrelated to headers.
Choose the best explanation for why Selenium's CDP network interception is preferred over using an external proxy for network control in browser automation.
Think about integration level and performance.
CDP works inside the browser's own debugging protocol, allowing precise and fast interception without needing external proxy setup. Proxy-based methods add network hops and complexity.