Skip to content

CDP Network Domain

aqualityAutomation edited this page Mar 17, 2023 · 1 revision

The CDP Network domain allows tracking the network activity of a page. It provides information about different HTTP requests/responses, their headers, body, etc. The NetworkHandling class of the Aquality Selenium framework is largely based on the methods of the mentioned domain. This class also implements various listeners and interceptors which allow intercepting and modifying request (HttpRequest) and response (HttpResponse) objects. Examples of using methods of NetworkHandling class can be found in NetworkInterceptionTests, RegisterBasicAuthTest and NetworkSpeedEmulationTest tests.

interceptAllRequests

The interceptAllRequests(HttpResponse response) method intercepts all HttpRequests and replaces the HttpResponse with the response passed as the method parameter. An example of using this interceptor is shown below. image The NetworkHandling object is obtained using the network() method of an instance of the Browser class, and this object is always the same for a particular Browser. This test intercepts ALL requests made by the Browser instance and generates a static page with plain text content (SOME_PHRASE = "Creame, delicious cheese!") as a result. You can remove an interceptor using the clearNetworkInerceptor() method of the NetworkHandling object.

startNetworkInterceptor

Here are some very similar methods that intercept HttpRequest:

startNetworkInterceptor(Predicate requestMatcher, Supplier handler) - intercepts an HttpRequest that satisfies the condition of the passed requestMatcher and returns an HttpResponse generated by the handler based on the intercepted HttpRequest; startNetworkInterceptor(Routable routable) - the functionality is similar to the previous method, but the requestMatcher and handler logic is implemented by the two methods matches() and execute() of the Routable interface; startNetworkInterceptor(Filter filter) - intercepts all HttpRequest and generates an HttpResponse based on the given request passed using the Filter function or the Filter chain; startNetworkInterceptor(HttpHandler httpHandler) - intercepts all HttpRequest and generates an HttpResponse based on the passed request using the HttpHandler function. addRequestHandler(Predicate requestMatcher, Function<HttpRequest, HttpResponse> requestHandler) - similar in functionality to the method from point 1, the only difference is in the method of generating HttpResponse, the choice depends on the user's preferences. An example of using one of the startNetworkInterceptor methods is shown below. image The startNetworkInterceptor() method sets up a request interceptor for the current Browser instance, which replaces all HttpResponse with a response with a static page containing plain text content (SOME_PHRASE = "Creame, delicious cheese!"). The clearNetworkInterceptor() method removes any request interceptor.

An example of using the addRequestHandler() method is shown below. image

interceptTrafficWith

The interceptTrafficWith(Filter filter) method modifies the sent HttpRequest using the Filter function passed as a parameter of the method. In the example below this method is used to add a header to the request. image In this example the filter is removed using the resetNetworkFilter() method, but the operation of this method is currently the same as the clearNetworkInterceptor() method and they are implemented internally in the same way. As with startNetworkInterceptor(), there can only be one requestFilter per Browser.

addResponseHandler

The addResponseHandler(Predicate responseMatcher, Function<HttpResponse, HttpResponse> responseTransformer) method intercepts the HttpResponse object using the responseMatcher logic and transforms that object. The example below shows one use case for addResponseHandler. image Here all HttpResponse are intercepted (response -> true) and replaced by simple static pages with the text somePhrase = "delicious cheese!". The interceptor is removed using the clearNetworkInterceptor() method. As with addRequestHandler(), one Browser can have only one responseHandler.

addRequestTransformer

The addRequestTransformer(Predicate requestMatcher, Function<HttpRequest, HttpRequest> requestTransformer) method is almost the same as the interceptTrafficWith() method and also modifies the sent HttpRequest, the only difference is that the requestMatcher parameter is explicitly specified here to determine which requests will be intercepted. Below is an example of how to use this method. image Adding and removing this requestTransformer is similar to the interceptors discussed above.

addRequestListener

The addRequestListener(Consumer listener) method adds a request listener to the current instance of the NetworkHandling class. This listener uses an instance of the RequestWillBeSent class for its work, which is similar to the HttpRequest class, only with additional fields and methods that make it easier to get information about the request. Using addRequestListener() will not modify the request and one NetworkHandling object can have multiple listeners. Below is an example of how to use this method. image Here, a listener has been added that increments the counter each time a request is made. All listeners are removed using the clearListerners() method.

addResponseListener

The addResponseListener(Consumer listener) method adds a response listener to the current instance of the NetworkHandling class. This listener uses an instance of the ResponseReceived class for its work, which is similar to the HttpResponse class, only with additional fields and methods that make it easier to get information about the response. Using addResponseListener() will not modify the response and a single NetworkHandling object can have multiple listeners. An example of using addResponseListener() is shown below. image Here, a listener is added that increments the counter each time a response is received. All listeners are removed using the clearListerners() method.

HTTP exchange logging

Aquality Selenium implements the ability to automatically log HTTP requests and responses using the following methods:

void enableHttpExchangeLogging(HttpExchangeLoggingOptions loggingOptions) - enables logging, a loggingOptions object is passed as a parameter, which configures the logging of individual parts of the request/response (e.g. whether the request body will be logged, and at what level - debug, info, etc.); void enableHttpExchangeLogging() - enables logging using standard values of the HttpExchangeLoggingOptions class instance; void clearListerners() - logging is based on listeners, so calling this method disables HTTP request/response logging. The list of logged information includes: request method, URL, headers, request parameters, response body, etc. The work of the listed methods is demonstrated in LogHttpExchangeTest class. image In the testEnablingHttpExchangeLogging test, the last message from the log file (logMessage1) is first received, then the enableHttpExchangeLogging() method is called, the page is updated using the refresh() method of the web driver (this action itself does not create an additional entry in the logs) and it is checked that additional entries have been added to the log.log file.

basic auth

Selenium 4 tools allow you to work with basic authorization in a simpler way. The NetworkHandling class implements several methods that allow you to work with these tools.

The addBasicAuthentication(String hostPart, String username, String password) method sets username and password for a specific host. After calling this method, all calls to this host from this Browser will be performed with this data. One Browser can have several sets of host, username and password for basic authorization. The example below demonstrates adding and removing basic auth. image The basic auth is deleted using the clearBasicAuthentication() method. ALL basic auths created are deleted!

setPageLoadTimeout

DevTools also allow to emulate Internet connection conditions (online/offline, connection delay, incoming/outgoing speed, connection type) using Network.emulateNetworkConditions method. In Aquality Selenium this functionality is implemented in the following methods:

void emulateConditions(Boolean offline, Number latency, Number downloadThroughput, Number uploadThroughput, String connectionType) - emulates different internet connection conditions; void emulateConditions(Boolean offline, Number latency, Number downloadThroughput, Number uploadThroughput) - similar to the previous method, except for the absence of the connectionType parameter (it is optional for the Network.emulateNetworkConditions method). These methods are demonstrated in the NetworkSpeedEmulationTest class. image The networkSpeedEmulationTest demonstrates some use cases for the emulateConditions() method. First, it checks that if the offline=true parameter is set, the page will not be loaded. Next, it is checked that when setting the download speed to low (DOWNLOAD_THROUGHPUT = 53 bytes/sec), the page will take a very long time to load and a TimeoutException will be thrown. At the end, it is checked that when setting a (relatively) high download speed (DOWNLOAD_THROUGHPUT_BIG = 530000 bytes/sec), the page will be loaded within the specified pageLoadTimeout.