CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-seleniumhq-selenium--selenium-devtools-v138

Chrome DevTools Protocol (CDP) bindings for Selenium WebDriver version 138

Pending
Overview
Eval results
Files

network-operations.mddocs/

Network Operations

Network traffic monitoring, request/response interception, and authentication handling for Chrome DevTools Protocol v138. Essential for API testing, performance monitoring, and request modification with full HTTP message support.

Capabilities

Network Handler

Main network handler providing traffic monitoring and interception capabilities.

/**
 * Network monitoring and interception for CDP v138
 * @param devTools DevTools connection for network communication
 */
public class v138Network extends Network<AuthRequired, RequestPaused> {
    private static final Logger LOG = Logger.getLogger(v138Network.class.getName());
    
    public v138Network(DevTools devTools);
    
    // Public API methods inherited from Network base class
    public void disable();
    public void setUserAgent(String userAgent);
    public void setUserAgent(UserAgent userAgent);
    public void addAuthHandler(Predicate<URI> uriPredicate, Supplier<Credentials> credentials);
    public void resetNetworkFilter();
    public void interceptTrafficWith(Filter filter);
    public void prepareToInterceptTraffic();
}

Usage Example:

import org.openqa.selenium.devtools.v138.v138Network;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.idealized.Network.UserAgent;

v138Network network = new v138Network(devTools);

// Set custom user agent using public API
network.setUserAgent("MyBot/1.0 (Linux; x86_64)");

// Prepare for traffic interception
network.prepareToInterceptTraffic();

// Add authentication handler
network.addAuthHandler(
    uri -> uri.getHost().contains("secure-api.com"),
    () -> new Credentials("username", "password")
);

Public Network API Methods

High-level methods for network configuration and traffic management without dealing with raw CDP commands.

/**
 * Set user agent string for all requests
 * @param userAgent User agent string to use
 */
public void setUserAgent(String userAgent);

/**
 * Set user agent with full configuration options
 * @param userAgent UserAgent object with string, language, and platform
 */
public void setUserAgent(UserAgent userAgent);

/**
 * Add authentication handler for protected resources
 * @param uriPredicate Predicate to match URIs requiring authentication
 * @param credentials Supplier providing credentials when needed
 */
public void addAuthHandler(Predicate<URI> uriPredicate, Supplier<Credentials> credentials);

/**
 * Prepare network domain for traffic interception
 */
public void prepareToInterceptTraffic();

/**
 * Intercept network traffic with custom filter
 * @param filter Filter to apply to intercepted traffic
 */
public void interceptTrafficWith(Filter filter);

/**
 * Reset all network filters and configurations
 */
public void resetNetworkFilter();

/**
 * Disable network monitoring and clean up resources
 */
public void disable();

Usage Example:

v138Network network = new v138Network(devTools);

// Set mobile user agent
network.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X)");

// Set up authentication for API endpoints
network.addAuthHandler(
    uri -> uri.getPath().startsWith("/api/"),
    () -> new Credentials("api-key", "secret-token")
);

// Prepare for request modification
network.prepareToInterceptTraffic();

// Add custom filter for specific domains
Filter domainFilter = new Filter() {
    @Override
    public boolean test(HttpRequest request) {
        return request.getUri().contains("example.com");
    }
};
network.interceptTrafficWith(domainFilter);

// Navigate - authentication and filtering will be applied
driver.get("https://example.com/api/data");

// Clean up when done
network.resetNetworkFilter();
network.disable();

User Agent Override

Controls the browser's user agent string and related headers.

/**
 * Override the browser's user agent string
 * @param userAgent UserAgent configuration with string, language, and platform
 * @return Command to set user agent override
 */
@Override
protected Command<Void> setUserAgentOverride(UserAgent userAgent);

Usage Example:

// Create custom user agent
UserAgent mobileUA = new UserAgent(
    "Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15",
    "en-US,en;q=0.9",
    "iPhone"
);

// Apply the override
devTools.send(network.setUserAgentOverride(mobileUA));

// All subsequent requests will use the mobile user agent
driver.get("https://example.com");

Network Caching Control

Enables or disables the browser's network cache for testing scenarios.

/**
 * Enable network caching for improved performance
 * @return Command to enable network cache
 */
@Override
protected Command<Void> enableNetworkCaching();

/**
 * Disable network caching to force fresh requests
 * @return Command to disable network cache
 */
@Override
protected Command<Void> disableNetworkCaching();

Request Interception Control

Manages request interception for modification and analysis.

/**
 * Enable fetch interception for all request patterns
 * @return Command to enable request interception
 */
@Override
protected Command<Void> enableFetchForAllPatterns();

/**
 * Disable fetch interception to restore normal operation
 * @return Command to disable request interception
 */
@Override
protected Command<Void> disableFetch();

Authentication Event Handling

Handles HTTP authentication challenges with credential provision.

/**
 * Stream of authentication required events
 * @return Event stream for authentication challenges
 */
@Override
protected Event<AuthRequired> authRequiredEvent();

/**
 * Extract URI from authentication challenge
 * @param authRequired Authentication challenge event
 * @return URI string requiring authentication
 */
@Override
protected String getUriFrom(AuthRequired authRequired);

/**
 * Continue request with provided credentials
 * @param authRequired Authentication challenge event
 * @param credentials Username and password for authentication
 * @return Command to provide credentials
 */
@Override
protected Command<Void> continueWithAuth(AuthRequired authRequired, 
                                       UsernameAndPassword credentials);

/**
 * Cancel authentication and fail the request
 * @param authRequired Authentication challenge event
 * @return Command to cancel authentication
 */
@Override
protected Command<Void> cancelAuth(AuthRequired authRequired);

Usage Example:

import org.openqa.selenium.UsernameAndPassword;

// Listen for authentication challenges
devTools.addListener(network.authRequiredEvent(), authEvent -> {
    String uri = network.getUriFrom(authEvent);
    System.out.println("Authentication required for: " + uri);
    
    // Provide credentials automatically
    UsernameAndPassword creds = new UsernameAndPassword("user", "pass");
    devTools.send(network.continueWithAuth(authEvent, creds));
});

// Enable interception to catch auth requests
devTools.send(network.enableFetchForAllPatterns());

// Navigate to protected resource
driver.get("https://httpbin.org/basic-auth/user/pass");

Request Interception and Modification

Intercepts and modifies HTTP requests and responses in real-time.

/**
 * Stream of paused request events for interception
 * @return Event stream for intercepted requests
 */
@Override
public Event<RequestPaused> requestPausedEvent();

/**
 * Create Selenium HTTP messages from paused request
 * @param pausedReq Intercepted request/response data
 * @return Either HttpRequest or HttpResponse for processing
 */
@Override
public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedReq);

/**
 * Check if paused request has error response
 * @param pausedReq Intercepted request data
 * @return true if request failed with error
 */
@Override
protected boolean hasErrorResponse(RequestPaused pausedReq);

/**
 * Extract request ID for correlation
 * @param pausedReq Intercepted request data  
 * @return String request identifier
 */
@Override
protected String getRequestId(RequestPaused pausedReq);

Request Continuation Control

Controls how intercepted requests are processed and forwarded.

/**
 * Continue request without any modifications
 * @param pausedRequest Intercepted request to continue
 * @return Command to continue unmodified
 */
@Override
protected Command<Void> continueWithoutModification(RequestPaused pausedRequest);

/**
 * Continue request with modifications
 * @param pausedReq Intercepted request
 * @param req Modified HttpRequest to send
 * @return Command to continue with modifications
 */
@Override
protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req);

/**
 * Fulfill request with custom response
 * @param pausedReq Intercepted request
 * @param res Custom HttpResponse to return
 * @return Command to fulfill with custom response
 */
@Override
protected Command<Void> fulfillRequest(RequestPaused pausedReq, HttpResponse res);

Complete Network Interception Example

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v138.v138Network;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.internal.Either;

ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();

v138Network network = new v138Network(devTools);

// Disable cache for testing
devTools.send(network.disableNetworkCaching());

// Enable request interception
devTools.send(network.enableFetchForAllPatterns());

// Intercept and modify requests
devTools.addListener(network.requestPausedEvent(), pausedReq -> {
    String requestId = network.getRequestId(pausedReq);
    
    // Check if this is a request or response
    Either<HttpRequest, HttpResponse> message = network.createSeMessages(pausedReq);
    
    if (message.isLeft()) {
        // Handle request modification
        HttpRequest request = message.left();
        System.out.println("Intercepted request: " + request.getUri());
        
        if (request.getUri().contains("/api/")) {
            // Add custom header to API requests
            request.addHeader("X-Custom-Header", "Modified-by-DevTools");
            devTools.send(network.continueRequest(pausedReq, request));
        } else {
            // Continue other requests unchanged
            devTools.send(network.continueWithoutModification(pausedReq));
        }
        
    } else {
        // Handle response modification
        HttpResponse response = message.right();
        System.out.println("Intercepted response: " + response.getStatus());
        
        if (response.getStatus() == 404) {
            // Replace 404s with custom response
            HttpResponse customResponse = new HttpResponse();
            customResponse.setStatus(200);
            customResponse.setContent(utf8String("Custom 404 replacement content"));
            customResponse.addHeader("Content-Type", "text/html");
            
            devTools.send(network.fulfillRequest(pausedReq, customResponse));
        } else {
            // Continue other responses unchanged
            devTools.send(network.continueWithoutModification(pausedReq));
        }
    }
});

// Handle authentication automatically
devTools.addListener(network.authRequiredEvent(), authEvent -> {
    String uri = network.getUriFrom(authEvent);
    
    if (uri.contains("secure-api")) {
        // Provide credentials for secure API
        UsernameAndPassword creds = new UsernameAndPassword("apikey", "secret");
        devTools.send(network.continueWithAuth(authEvent, creds));
    } else {
        // Cancel auth for other requests
        devTools.send(network.cancelAuth(authEvent));
    }
});

// Navigate and trigger network activity
driver.get("https://httpbin.org/get");
driver.get("https://httpbin.org/status/404");

// Clean up
devTools.send(network.disableFetch());
devTools.close();
driver.quit();

Types

// User agent configuration
class UserAgent {
    String userAgent();      // User agent string
    String acceptLanguage(); // Language preferences  
    String platform();       // Platform identifier
}

// Network authentication
class AuthRequired {
    AuthChallenge getAuthChallenge(); // Authentication challenge details
    RequestId getRequestId();         // Request requiring auth
}

class AuthChallenge {
    String getOrigin();   // Origin requiring authentication
    String getScheme();   // Authentication scheme (Basic, Digest, etc.)
    String getRealm();    // Authentication realm
}

// Request interception
class RequestPaused {
    RequestId getRequestId();                    // Unique request identifier
    Request getRequest();                        // Request details
    Optional<Integer> getResponseStatusCode();   // Response status if available
    Optional<List<HeaderEntry>> getResponseHeaders(); // Response headers if available
    Optional<ErrorReason> getResponseErrorReason(); // Error reason if failed
}

Install with Tessl CLI

npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-devtools-v138

docs

console-logging.md

domain-management.md

index.md

javascript-integration.md

network-operations.md

runtime-events.md

target-management.md

version-system.md

tile.json