Chrome DevTools Protocol version 115 bindings for Selenium Java WebDriver enabling programmatic browser debugging capabilities
—
Advanced network traffic interception, request/response manipulation, authentication handling, and user agent override capabilities through the v115Network class.
Comprehensive network traffic control including interception, authentication, caching, and user agent management.
/**
* Network domain for traffic interception and manipulation
* Extends the idealized Network class with CDP v115 specific implementations
*/
public class v115Network extends Network<AuthRequired, RequestPaused> {
/**
* Creates network domain with DevTools connection
* @param devTools DevTools instance for CDP communication
*/
public v115Network(DevTools devTools);
/**
* Set user agent override for all requests
* @param userAgent UserAgent configuration with optional language and platform
* @return Command to set user agent override
*/
protected Command<Void> setUserAgentOverride(UserAgent userAgent);
/**
* Enable network caching
* @return Command to enable network caching
*/
protected Command<Void> enableNetworkCaching();
/**
* Disable network caching for testing and development
* @return Command to disable network caching
*/
protected Command<Void> disableNetworkCaching();
/**
* Enable fetch domain for all request patterns
* @return Command to enable fetch interception
*/
protected Command<Void> enableFetchForAllPatterns();
/**
* Disable fetch domain and stop request interception
* @return Command to disable fetch interception
*/
protected Command<Void> disableFetch();
/**
* Get authentication required event stream
* @return Event stream for authentication challenges
*/
protected Event<AuthRequired> authRequiredEvent();
/**
* Extract URI from authentication required event
* @param authRequired Authentication event from CDP
* @return URI string that requires authentication
*/
protected String getUriFrom(AuthRequired authRequired);
/**
* Continue request with authentication credentials
* @param authRequired Authentication event to respond to
* @param credentials Username and password for authentication
* @return Command to continue with authentication
*/
protected Command<Void> continueWithAuth(AuthRequired authRequired, UsernameAndPassword credentials);
/**
* Cancel authentication request
* @param authRequired Authentication event to cancel
* @return Command to cancel authentication
*/
protected Command<Void> cancelAuth(AuthRequired authRequired);
/**
* Get request paused event stream for traffic interception
* @return Event stream for paused requests
*/
public Event<RequestPaused> requestPausedEvent();
/**
* Create Selenium HTTP messages from paused request
* @param pausedReq Request paused event from CDP
* @return Either HttpRequest (for requests) or HttpResponse (for responses)
*/
public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedReq);
/**
* Get request ID from paused request for tracking
* @param pausedReq Request paused event
* @return String request identifier
*/
protected String getRequestId(RequestPaused pausedReq);
/**
* Continue request without any modifications
* @param pausedRequest Request to continue unchanged
* @return Command to continue request
*/
protected Command<Void> continueWithoutModification(RequestPaused pausedRequest);
/**
* Continue request with modifications
* @param pausedReq Original paused request
* @param req Modified HttpRequest to send instead
* @return Command to continue with modified request
*/
protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req);
/**
* Fulfill request with custom response
* @param pausedReq Original paused request
* @param res HttpResponse to return instead of making actual request
* @return Command to fulfill request with response
*/
protected Command<Void> fulfillRequest(RequestPaused pausedReq, HttpResponse res);
}Set custom user agent strings for browser identification and testing different client environments.
/**
* Set user agent string (inherited from Network base class)
* @param userAgent Simple user agent string
*/
public void setUserAgent(String userAgent);
/**
* Set detailed user agent configuration (inherited from Network base class)
* @param userAgent UserAgent object with language and platform options
*/
public void setUserAgent(UserAgent userAgent);Usage Examples:
import org.openqa.selenium.devtools.v115.v115Network;
import org.openqa.selenium.devtools.idealized.Network.UserAgent;
// Create network domain
v115Network network = new v115Network(devTools);
// Simple user agent override
network.setUserAgent("CustomBot/1.0");
// Detailed user agent with language and platform
UserAgent customAgent = new UserAgent(
"Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15",
Optional.of("en-US,en;q=0.9"),
Optional.of("iPhone")
);
network.setUserAgent(customAgent);
// Navigate with custom user agent
driver.get("https://httpbin.org/user-agent");
// Verify user agent was set
String userAgentCheck = (String) driver.executeScript(
"return navigator.userAgent;"
);
System.out.println("Active User Agent: " + userAgentCheck);Control browser caching behavior for testing and development scenarios.
Usage Examples:
// Disable caching for testing fresh requests
network.disableNetworkCaching();
driver.get("https://example.com/api/data");
// Re-enable caching for performance
network.enableNetworkCaching();
driver.get("https://example.com/static/resources");Automatically handle HTTP authentication challenges with credentials.
/**
* Add authentication handler (inherited from Network base class)
* @param whenThisMatches Predicate to match URIs that need authentication
* @param useTheseCredentials Supplier providing credentials for matched URIs
*/
public void addAuthHandler(Predicate<URI> whenThisMatches, Supplier<Credentials> useTheseCredentials);Usage Examples:
import java.net.URI;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.openqa.selenium.Credentials;
// Add authentication for specific domains
network.addAuthHandler(
uri -> uri.getHost().equals("secure.example.com"),
() -> new Credentials("username", "password")
);
// Add authentication for API endpoints
network.addAuthHandler(
uri -> uri.getPath().startsWith("/api/"),
() -> new Credentials("api_user", "api_key_123")
);
// Navigate to protected resources - authentication handled automatically
driver.get("https://secure.example.com/protected");
driver.get("https://api.example.com/api/users");Intercept and modify network requests and responses for testing and monitoring.
/**
* Intercept traffic with custom filter (inherited from Network base class)
* @param filter Filter implementation for request/response handling
*/
public void interceptTrafficWith(Filter filter);
/**
* Reset network filter and stop interception (inherited from Network base class)
*/
public void resetNetworkFilter();Usage Examples:
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.http.Filter;
// Create custom traffic filter
Filter loggingFilter = Filter.chain(
(req, next) -> {
System.out.println("Request: " + req.getMethod() + " " + req.getUri());
// Modify request headers
req.addHeader("X-Custom-Header", "Modified-by-Selenium");
HttpResponse response = next.execute(req);
System.out.println("Response: " + response.getStatus() + " for " + req.getUri());
// Modify response if needed
if (req.getUri().toString().contains("/api/")) {
response.addHeader("X-API-Response", "true");
}
return response;
}
);
// Start intercepting traffic
network.interceptTrafficWith(loggingFilter);
// Navigate with interception active
driver.get("https://example.com");
driver.get("https://example.com/api/data");
// Stop interception
network.resetNetworkFilter();Advanced request and response manipulation using low-level CDP events.
Usage Examples:
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.v115.fetch.model.RequestPaused;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
// Listen for paused requests
Event<RequestPaused> requestPaused = network.requestPausedEvent();
devTools.addListener(requestPaused, pausedReq -> {
try {
String requestId = network.getRequestId(pausedReq);
// Create Selenium HTTP objects
Either<HttpRequest, HttpResponse> either = network.createSeMessages(pausedReq);
if (either.isLeft()) {
// Handle request
HttpRequest request = either.left();
System.out.println("Intercepted request to: " + request.getUri());
// Modify request
if (request.getUri().toString().contains("/slow-endpoint")) {
// Replace slow endpoint with fast mock
HttpResponse mockResponse = new HttpResponse();
mockResponse.setStatus(200);
mockResponse.setContent(utf8String("{\"data\": \"mocked\"}"));
mockResponse.addHeader("Content-Type", "application/json");
devTools.send(network.fulfillRequest(pausedReq, mockResponse));
} else {
// Continue with original request
devTools.send(network.continueWithoutModification(pausedReq));
}
} else {
// Handle response
HttpResponse response = either.right();
System.out.println("Intercepted response: " + response.getStatus());
// Always continue responses
devTools.send(network.continueWithoutModification(pausedReq));
}
} catch (Exception e) {
System.err.println("Error handling request: " + e.getMessage());
devTools.send(network.continueWithoutModification(pausedReq));
}
});
// Enable interception
devTools.send(network.enableFetchForAllPatterns());
// Navigate with interception active
driver.get("https://example.com/slow-page");Replace API calls with mock responses for testing:
// Set up API mocking filter
Filter apiMockFilter = (req, next) -> {
String uri = req.getUri().toString();
if (uri.contains("/api/users")) {
// Return mock user data
HttpResponse mockResponse = new HttpResponse();
mockResponse.setStatus(200);
mockResponse.setContent(utf8String("""
{
"users": [
{"id": 1, "name": "Mock User 1"},
{"id": 2, "name": "Mock User 2"}
]
}
"""));
mockResponse.addHeader("Content-Type", "application/json");
return mockResponse;
}
return next.execute(req);
};
network.interceptTrafficWith(apiMockFilter);Monitor and analyze request timing:
Map<String, Long> requestTimes = new ConcurrentHashMap<>();
Filter timingFilter = (req, next) -> {
String url = req.getUri().toString();
long startTime = System.currentTimeMillis();
HttpResponse response = next.execute(req);
long duration = System.currentTimeMillis() - startTime;
requestTimes.put(url, duration);
System.out.println("Request to " + url + " took " + duration + "ms");
return response;
};
network.interceptTrafficWith(timingFilter);Inject network errors for resilience testing:
Filter errorInjectionFilter = (req, next) -> {
String uri = req.getUri().toString();
// Simulate 50% failure rate for specific endpoints
if (uri.contains("/flaky-service") && Math.random() < 0.5) {
HttpResponse errorResponse = new HttpResponse();
errorResponse.setStatus(500);
errorResponse.setContent(utf8String("Simulated server error"));
return errorResponse;
}
return next.execute(req);
};
network.interceptTrafficWith(errorInjectionFilter);Authentication challenges have timeouts and can be cancelled:
// Handle authentication with timeout
CompletableFuture<Void> authFuture = CompletableFuture.runAsync(() -> {
// Set up auth handler with timeout logic
network.addAuthHandler(
uri -> uri.getHost().equals("slow-auth.example.com"),
() -> {
// Simulate slow credential lookup
try {
Thread.sleep(2000);
return new Credentials("user", "pass");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Auth interrupted");
}
}
);
});
try {
authFuture.get(5, TimeUnit.SECONDS);
} catch (TimeoutException e) {
System.err.println("Authentication setup timed out");
}Handle errors in request interception gracefully:
devTools.addListener(requestPaused, pausedReq -> {
try {
// Process request
processInterceptedRequest(pausedReq);
} catch (Exception e) {
System.err.println("Error processing request: " + e.getMessage());
// Always continue to avoid hanging
devTools.send(network.continueWithoutModification(pausedReq));
}
});Properly disable network features when done:
// Disable all network interception
network.resetNetworkFilter();
network.disable();Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v115