Chrome DevTools Protocol (CDP) bindings for Selenium WebDriver targeting Chromium version 85
—
Network monitoring and HTTP request/response interception capabilities for the Chrome DevTools Protocol v85. This functionality allows you to monitor network traffic, modify requests and responses, handle authentication challenges, and control caching behavior.
The main class for network interception extending the base Network functionality.
/**
* Network monitoring and request/response interception for Chrome DevTools v85
*/
public class V85Network extends Network<AuthRequired, RequestPaused> {
/**
* Creates a new V85Network instance
* @param devTools - DevTools session for sending commands
*/
public V85Network(DevTools devTools);
}Control the browser's user agent string and related headers.
/**
* Sets user agent override for network requests
* @param userAgent - User agent configuration including UA string, language, and platform
* @return Command to set user agent override
*/
protected Command<Void> setUserAgentOverride(UserAgent userAgent);Usage Example:
import org.openqa.selenium.devtools.v85.V85Network;
import org.openqa.selenium.devtools.idealized.Network.UserAgent;
V85Network network = domains.network();
UserAgent customUA = new UserAgent("Mozilla/5.0 (Custom Browser) WebKit/537.36")
.acceptLanguage("en-US,en")
.platform("Linux x86_64");
devTools.send(network.setUserAgentOverride(customUA));Enable or disable network caching for the browser session.
/**
* Enables network caching for the session
* @return Command to enable caching
*/
protected Command<Void> enableNetworkCaching();
/**
* Disables network caching for the session
* @return Command to disable caching
*/
protected Command<Void> disableNetworkCaching();Usage Example:
// Disable caching for testing
devTools.send(network.disableNetworkCaching());
// Re-enable caching later
devTools.send(network.enableNetworkCaching());Set up and manage request interception for all network patterns.
/**
* Enables fetch interception for all request patterns
* @return Command to enable request/response interception
*/
protected Command<Void> enableFetchForAllPatterns();
/**
* Disables fetch interception
* @return Command to disable interception
*/
protected Command<Void> disableFetch();
/**
* Event fired when a request is paused for interception
* @return Event for paused requests
*/
public Event<RequestPaused> requestPausedEvent();Usage Example:
// Enable request interception
devTools.send(network.enableFetchForAllPatterns());
// Listen for paused requests
devTools.addListener(network.requestPausedEvent(), pausedRequest -> {
// Handle the intercepted request
handleRequest(pausedRequest);
});Process intercepted requests and responses with modification capabilities.
/**
* Creates HttpRequest or HttpResponse from paused request data
* @param pausedReq - The paused request data from CDP
* @return Either HttpRequest (for requests) or HttpResponse (for responses)
*/
public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedReq);
/**
* Checks if the paused request has an error response
* @param pausedReq - The paused request to check
* @return true if there's an error response
*/
protected boolean hasErrorResponse(RequestPaused pausedReq);
/**
* Gets the request ID from a paused request
* @param pausedReq - The paused request
* @return Request ID as string
*/
protected String getRequestId(RequestPaused pausedReq);Continue requests with or without modifications.
/**
* Continues a paused request without any modifications
* @param pausedRequest - The paused request to continue
* @return Command to continue request unmodified
*/
protected Command<Void> continueWithoutModification(RequestPaused pausedRequest);
/**
* Continues a paused request with modifications
* @param pausedReq - The paused request to modify
* @param req - Modified HttpRequest to send instead
* @return Command to continue with modified request
*/
protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req);
/**
* Fulfills a paused request with a custom response
* @param pausedReq - The paused request to fulfill
* @param res - Custom HttpResponse to return
* @return Command to fulfill request with custom response
*/
protected Command<Void> fulfillRequest(RequestPaused pausedReq, HttpResponse res);Usage Example:
import org.openqa.selenium.remote.http.Contents;
devTools.addListener(network.requestPausedEvent(), pausedRequest -> {
if (pausedRequest.getRequest().getUrl().contains("/api/data")) {
// Fulfill with custom response
HttpResponse customResponse = new HttpResponse()
.setStatus(200)
.setContent(Contents.utf8String("{\"custom\": \"data\"}"));
devTools.send(network.fulfillRequest(pausedRequest, customResponse));
} else {
// Continue without modification
devTools.send(network.continueWithoutModification(pausedRequest));
}
});Handle HTTP authentication challenges automatically.
/**
* Event fired when authentication is required
* @return Event for authentication challenges
*/
protected Event<AuthRequired> authRequiredEvent();
/**
* Gets the URI from an authentication challenge
* @param authRequired - The authentication challenge
* @return URI that requires authentication
*/
protected String getUriFrom(AuthRequired authRequired);
/**
* Continues authentication with provided credentials
* @param authRequired - The authentication challenge
* @param credentials - Username and password to use
* @return Command to provide credentials
*/
protected Command<Void> continueWithAuth(AuthRequired authRequired, UsernameAndPassword credentials);
/**
* Cancels authentication challenge
* @param authRequired - The authentication challenge to cancel
* @return Command to cancel authentication
*/
protected Command<Void> cancelAuth(AuthRequired authRequired);Usage Example:
// Handle authentication challenges
devTools.addListener(network.authRequiredEvent(), authChallenge -> {
String uri = network.getUriFrom(authChallenge);
if (uri.contains("secure-api.example.com")) {
UsernameAndPassword creds = new UsernameAndPassword("username", "password");
devTools.send(network.continueWithAuth(authChallenge, creds));
} else {
devTools.send(network.cancelAuth(authChallenge));
}
});Authentication challenge information from the browser.
public class AuthRequired {
public RequestId getRequestId();
public AuthChallenge getAuthChallenge();
}
public class AuthChallenge {
public String getOrigin();
public String getScheme();
public String getRealm();
}Information about a paused network request or response.
public class RequestPaused {
public RequestId getRequestId();
public Request getRequest();
public Optional<Integer> getResponseStatusCode();
public Optional<List<HeaderEntry>> getResponseHeaders();
public Optional<String> getResponseErrorReason();
}Network request details.
public class Request {
public String getUrl();
public String getMethod();
public Map<String, String> getHeaders();
public Optional<String> getPostData();
}HTTP header name/value pair.
public class HeaderEntry {
public HeaderEntry(String name, String value);
public String getName();
public String getValue();
}Pattern for request interception configuration.
public class RequestPattern {
public RequestPattern(Optional<String> urlPattern, Optional<String> resourceType, Optional<RequestStage> interceptionStage);
}
public enum RequestStage {
REQUEST,
RESPONSE
}Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v85