88import org .openqa .selenium .UsernameAndPassword ;
99import org .openqa .selenium .devtools .NetworkInterceptor ;
1010import org .openqa .selenium .devtools .idealized .Network ;
11- import org .openqa .selenium .devtools .v142 .network .model .*;
11+ import org .openqa .selenium .devtools .v143 .network .model .*;
1212import org .openqa .selenium .remote .http .*;
1313
1414import java .net .URI ;
2222
2323import static aquality .selenium .browser .AqualityServices .getBrowser ;
2424import static aquality .selenium .logging .LocalizedLoggerUtility .logByLevel ;
25- import static org .openqa .selenium .devtools .v142 .network .Network .*;
25+ import static org .openqa .selenium .devtools .v143 .network .Network .*;
2626
2727/**
2828 * DevTools commands for version-independent network interception.
29- * For more information, see {@link org.openqa.selenium.devtools.v142 .network.Network} and {@link Network}.
29+ * For more information, see {@link org.openqa.selenium.devtools.v143 .network.Network} and {@link Network}.
3030 */
3131public class NetworkHandling {
3232 public static final String LOC_NETWORK_INTERCEPTOR_START = "loc.browser.network.interceptor.start" ;
@@ -62,6 +62,7 @@ public void stopMonitoring() {
6262
6363 /**
6464 * Overrides the values of user agent.
65+ *
6566 * @param userAgent User agent to use.
6667 */
6768 public void setUserAgent (String userAgent ) {
@@ -71,6 +72,7 @@ public void setUserAgent(String userAgent) {
7172
7273 /**
7374 * Overrides the values of user agent.
75+ *
7476 * @param userAgent User agent to use.
7577 */
7678 public void setUserAgent (Network .UserAgent userAgent ) {
@@ -80,7 +82,8 @@ public void setUserAgent(Network.UserAgent userAgent) {
8082
8183 /**
8284 * Add basic authentication handler.
83- * @param whenThisMatches URI matcher.
85+ *
86+ * @param whenThisMatches URI matcher.
8487 * @param useTheseCredentials parameters, such as URI matcher and credentials.
8588 */
8689 public void addAuthHandler (Predicate <URI > whenThisMatches , Supplier <Credentials > useTheseCredentials ) {
@@ -90,6 +93,7 @@ public void addAuthHandler(Predicate<URI> whenThisMatches, Supplier<Credentials>
9093
9194 /**
9295 * Add basic authentication handler.
96+ *
9397 * @param hostPart part of the host name for URI matcher.
9498 * @param username authentication username.
9599 * @param password authentication password.
@@ -117,6 +121,7 @@ public void resetNetworkFilter() {
117121
118122 /**
119123 * Starts traffic interception with specified filter.
124+ *
120125 * @param filter HTTP filter.
121126 */
122127 public void interceptTrafficWith (Filter filter ) {
@@ -126,6 +131,7 @@ public void interceptTrafficWith(Filter filter) {
126131
127132 /**
128133 * Adds listener to network request sent event.
134+ *
129135 * @param listener a listener to add.
130136 */
131137 public void addRequestListener (Consumer <RequestWillBeSent > listener ) {
@@ -136,6 +142,7 @@ public void addRequestListener(Consumer<RequestWillBeSent> listener) {
136142
137143 /**
138144 * Adds listener to network response received event.
145+ *
139146 * @param listener a listener to add.
140147 */
141148 public void addResponseListener (Consumer <ResponseReceived > listener ) {
@@ -200,7 +207,7 @@ private Consumer<ResponseReceived> getResponseLogger(HttpExchangeLoggingOptions
200207 formatHeaders (response .getHeaders ()));
201208 }
202209 if (loggingOptions .getResponseBody ().isEnabled ()) {
203- String responseBody = tools .sendCommand (org . openqa . selenium . devtools . v142 . network . Network . getResponseBody (requestId )).getBody ();
210+ String responseBody = tools .sendCommand (getResponseBody (requestId )).getBody ();
204211 if (StringUtils .isNotEmpty (responseBody )) {
205212 logByLevel (loggingOptions .getResponseBody ().getLogLevel (),
206213 "loc.browser.network.event.responsereceived.log.body" ,
@@ -212,6 +219,7 @@ private Consumer<ResponseReceived> getResponseLogger(HttpExchangeLoggingOptions
212219
213220 /**
214221 * Enables HTTP Request/Response logging.
222+ *
215223 * @param loggingOptions logging parameters {@link HttpExchangeLoggingOptions}.
216224 */
217225 public void enableHttpExchangeLogging (HttpExchangeLoggingOptions loggingOptions ) {
@@ -221,6 +229,7 @@ public void enableHttpExchangeLogging(HttpExchangeLoggingOptions loggingOptions)
221229
222230 /**
223231 * Starts network interceptor.
232+ *
224233 * @param httpHandler HTTP handler.
225234 * @return an instance of {@link NetworkInterceptor}.
226235 */
@@ -231,6 +240,7 @@ public NetworkInterceptor startNetworkInterceptor(HttpHandler httpHandler) {
231240
232241 /**
233242 * Starts network interceptor.
243+ *
234244 * @param filter network filter.
235245 * @return an instance of {@link NetworkInterceptor}.
236246 */
@@ -241,6 +251,7 @@ public NetworkInterceptor startNetworkInterceptor(Filter filter) {
241251
242252 /**
243253 * Starts network interceptor.
254+ *
244255 * @param routable a filter with matcher.
245256 * @return an instance of {@link NetworkInterceptor}.
246257 */
@@ -251,8 +262,9 @@ public NetworkInterceptor startNetworkInterceptor(Routable routable) {
251262
252263 /**
253264 * Starts network interceptor.
265+ *
254266 * @param requestMatcher predicate to match the request.
255- * @param handler handler for matched requests.
267+ * @param handler handler for matched requests.
256268 * @return an instance of {@link NetworkInterceptor}.
257269 */
258270 public NetworkInterceptor startNetworkInterceptor (Predicate <HttpRequest > requestMatcher , Supplier <HttpHandler > handler ) {
@@ -261,6 +273,7 @@ public NetworkInterceptor startNetworkInterceptor(Predicate<HttpRequest> request
261273
262274 /**
263275 * Intercepts any request with predefined response.
276+ *
264277 * @param response HTTP response.
265278 * @return an instance of {@link NetworkInterceptor}.
266279 */
@@ -270,7 +283,8 @@ public NetworkInterceptor interceptAllRequests(HttpResponse response) {
270283
271284 /**
272285 * Adds request transformer.
273- * @param requestMatcher predicate to match the request.
286+ *
287+ * @param requestMatcher predicate to match the request.
274288 * @param requestTransformer function to transform the request.
275289 * @return an instance of {@link NetworkInterceptor}.
276290 */
@@ -281,6 +295,7 @@ public NetworkInterceptor addRequestTransformer(Predicate<HttpRequest> requestMa
281295
282296 /**
283297 * Adds request handler.
298+ *
284299 * @param requestMatcher predicate to match the request.
285300 * @param requestHandler handler for matched requests.
286301 * @return an instance of {@link NetworkInterceptor}.
@@ -292,7 +307,8 @@ public NetworkInterceptor addRequestHandler(Predicate<HttpRequest> requestMatche
292307
293308 /**
294309 * Adds response handler.
295- * @param responseMatcher predicate to match the response.
310+ *
311+ * @param responseMatcher predicate to match the response.
296312 * @param responseTransformer function to transform the response.
297313 * @return an instance of {@link NetworkInterceptor}.
298314 */
@@ -309,35 +325,83 @@ public NetworkInterceptor addResponseHandler(Predicate<HttpResponse> responseMat
309325 * Currently, Selenium supports only a single network interceptor. Any new Network interceptor will override the previous one.
310326 * And on {@link NetworkInterceptor#close()} the NetworkInterceptor class just calls the {@link Network#resetNetworkFilter()}, so it's enough to call it.
311327 * If multiple network interceptors at the same time would be allowed, we may want to store all
312- * {@link NetworkInterceptor} classes to call {@link NetworkInterceptor#close()} for each.
328+ * {@link NetworkInterceptor} classes to call {@link NetworkInterceptor#close()} for each.
313329 */
314330 public void clearNetworkInterceptor () {
315331 resetNetworkFilter ();
316332 }
317333
318334 /**
319335 * Activates emulation of network conditions.
320- * @param offline True to emulate internet disconnection.
321- * @param latency Minimum latency from request sent to response headers received (ms).
336+ *
337+ * @param offline True to emulate internet disconnection.
338+ * @param latency Minimum latency from request sent to response headers received (ms).
322339 * @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
323- * @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
340+ * @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
341+ * @deprecated This command is deprecated in favor of the {@link #emulateConditionsByRule(Boolean, List)}
342+ * and {@link #overrideState} commands, which can be used together to the same effect.
324343 */
344+ @ Deprecated
325345 public void emulateConditions (Boolean offline , Number latency , Number downloadThroughput , Number uploadThroughput ) {
326346 tools .sendCommand (enable (Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty ()));
327347 tools .sendCommand (emulateNetworkConditions (offline , latency , downloadThroughput , uploadThroughput , Optional .empty (),
328348 Optional .empty (), Optional .empty (), Optional .empty ()));
329349 }
330350
351+ /**
352+ * Activates emulation of network conditions for individual requests using URL match patterns.
353+ * Unlike the deprecated Network.emulateNetworkConditions this method does not affect `navigator` state.
354+ * Use Network.overrideNetworkState to explicitly modify `navigator` behavior.
355+ *
356+ * @param offline True to emulate internet disconnection.
357+ * @param matchedNetworkConditions List of network conditions to apply.
358+ */
359+ public void emulateConditionsByRule (Boolean offline , List <NetworkConditions > matchedNetworkConditions ) {
360+ tools .sendCommand (enable (Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty ()));
361+ tools .sendCommand (emulateNetworkConditionsByRule (offline , matchedNetworkConditions ));
362+ }
363+
364+ /**
365+ * Override the state of navigator.onLine and navigator.connection.
366+ *
367+ * @param offline True to emulate internet disconnection.
368+ * @param latency Minimum latency from request sent to response headers received (ms).
369+ * @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
370+ * @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
371+ */
372+ public void overrideState (Boolean offline , Number latency , Number downloadThroughput , Number uploadThroughput ) {
373+ tools .sendCommand (enable (Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty ()));
374+ tools .sendCommand (overrideNetworkState (offline , latency , downloadThroughput , uploadThroughput , Optional .empty ()));
375+ }
376+
377+ /**
378+ * Override the state of navigator.onLine and navigator.connection.
379+ *
380+ * @param offline True to emulate internet disconnection.
381+ * @param latency Minimum latency from request sent to response headers received (ms).
382+ * @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
383+ * @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
384+ * @param connectionType Connection type if known.
385+ */
386+ public void overrideState (Boolean offline , Number latency , Number downloadThroughput , Number uploadThroughput , ConnectionType connectionType ) {
387+ tools .sendCommand (enable (Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty ()));
388+ tools .sendCommand (overrideNetworkState (offline , latency , downloadThroughput , uploadThroughput , Optional .of (connectionType )));
389+ }
390+
331391 /**
332392 * Activates emulation of network conditions.
333- * @param offline True to emulate internet disconnection.
334- * @param latency Minimum latency from request sent to response headers received (ms).
393+ *
394+ * @param offline True to emulate internet disconnection.
395+ * @param latency Minimum latency from request sent to response headers received (ms).
335396 * @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
336- * @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
337- * @param connectionType Connection type if known.
338- * Possible values: "none", "cellular2g", "cellular3g", "cellular4g", "bluetooth", "ethernet",
339- * "wifi", "wimax", "other".
397+ * @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
398+ * @param connectionType Connection type if known.
399+ * Possible values: "none", "cellular2g", "cellular3g", "cellular4g", "bluetooth", "ethernet",
400+ * "wifi", "wimax", "other".
401+ * @deprecated This command is deprecated in favor of the {@link #emulateConditionsByRule(Boolean, List)}
402+ * and {@link #overrideState} commands, which can be used together to the same effect.
340403 */
404+ @ Deprecated
341405 public void emulateConditions (Boolean offline , Number latency , Number downloadThroughput , Number uploadThroughput , String connectionType ) {
342406 tools .sendCommand (enable (Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty (), Optional .empty ()));
343407 tools .sendCommand (emulateNetworkConditions (offline , latency , downloadThroughput , uploadThroughput ,
0 commit comments