0
# Network Operations
1
2
Advanced network interception and monitoring capabilities for Chrome DevTools Protocol v105. Provides comprehensive control over network traffic including request/response modification, authentication handling, and traffic analysis.
3
4
## Capabilities
5
6
### Network Monitoring
7
8
Configure network monitoring to intercept and analyze HTTP traffic through high-level methods.
9
10
```java { .api }
11
/**
12
* Initialize network handler for monitoring and interception
13
* @param devTools DevTools session instance
14
*/
15
public V105Network(DevTools devTools);
16
17
// Inherited methods from Network base class
18
/**
19
* Disable network interception and reset to defaults
20
*/
21
public void disable();
22
23
/**
24
* Set up request interception for authentication and filtering
25
*/
26
public void prepareToInterceptTraffic();
27
28
/**
29
* Add filter for intercepting and modifying HTTP traffic
30
* @param filter HTTP filter for request/response modification
31
*/
32
public void interceptTrafficWith(Filter filter);
33
34
// Protected methods implemented by V105Network
35
protected Command<Void> setUserAgentOverride(UserAgent userAgent);
36
protected Command<Void> enableNetworkCaching();
37
protected Command<Void> disableNetworkCaching();
38
protected Command<Void> enableFetchForAllPatterns();
39
protected Command<Void> disableFetch();
40
```
41
42
**Usage Examples:**
43
44
```java
45
import org.openqa.selenium.devtools.v105.V105Network;
46
import org.openqa.selenium.devtools.DevTools;
47
import org.openqa.selenium.remote.http.Filter;
48
49
// Initialize network monitoring
50
V105Network network = new V105Network(devTools);
51
52
// Set up request interception
53
network.prepareToInterceptTraffic();
54
55
// Add custom filter for modifying requests
56
Filter customFilter = next -> req -> {
57
// Add custom header to all requests
58
req.addHeader("X-Test-Mode", "true");
59
return next.execute(req);
60
};
61
network.interceptTrafficWith(customFilter);
62
63
// Clean up when done
64
network.disable();
65
```
66
67
### User Agent Override
68
69
Override the browser's user agent string for testing different browsers or devices.
70
71
```java { .api }
72
/**
73
* Set user agent override with full configuration (protected method)
74
* @param userAgent User agent configuration including platform and language
75
* @return Command to set user agent override
76
*/
77
protected Command<Void> setUserAgentOverride(UserAgent userAgent);
78
79
/**
80
* User agent configuration (inherited from Network base class)
81
*/
82
public static class UserAgent {
83
String userAgent();
84
String acceptLanguage();
85
String platform();
86
}
87
```
88
89
**Usage Examples:**
90
91
```java
92
// Override user agent for mobile testing - use CDP Network domain directly
93
devTools.send(org.openqa.selenium.devtools.v105.network.Network.setUserAgentOverride(
94
"Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15",
95
Optional.of("en-US,en;q=0.9"),
96
Optional.of("iOS"),
97
Optional.empty()
98
));
99
```
100
101
### Authentication Handling
102
103
Handle HTTP authentication challenges automatically.
104
105
```java { .api }
106
/**
107
* Get authentication required event stream
108
* @return Event stream for authentication challenges
109
*/
110
protected Event<AuthRequired> authRequiredEvent();
111
112
/**
113
* Extract URI from authentication challenge
114
* @param authRequired Authentication challenge event
115
* @return Origin URI requiring authentication
116
*/
117
protected String getUriFrom(AuthRequired authRequired);
118
119
/**
120
* Continue with provided credentials
121
* @param authRequired Authentication challenge event
122
* @param credentials Username and password
123
* @return Command to provide authentication credentials
124
*/
125
protected Command<Void> continueWithAuth(AuthRequired authRequired, UsernameAndPassword credentials);
126
127
/**
128
* Cancel authentication challenge
129
* @param authRequired Authentication challenge event
130
* @return Command to cancel authentication
131
*/
132
protected Command<Void> cancelAuth(AuthRequired authRequired);
133
```
134
135
**Usage Examples:**
136
137
```java
138
import org.openqa.selenium.UsernameAndPassword;
139
140
// Set up authentication listener
141
network.authRequiredEvent().addListener(authRequired -> {
142
String origin = network.getUriFrom(authRequired);
143
144
if (origin.contains("secure-api.example.com")) {
145
// Provide credentials for specific domain
146
UsernameAndPassword creds = new UsernameAndPassword("user", "pass");
147
devTools.send(network.continueWithAuth(authRequired, creds));
148
} else {
149
// Cancel authentication for other domains
150
devTools.send(network.cancelAuth(authRequired));
151
}
152
});
153
```
154
155
### Request and Response Interception
156
157
Intercept, modify, and fulfill HTTP requests and responses.
158
159
```java { .api }
160
/**
161
* Get request paused event stream for intercepted requests
162
* @return Event stream for paused requests
163
*/
164
public Event<RequestPaused> requestPausedEvent();
165
166
/**
167
* Create Selenium HTTP messages from intercepted request
168
* @param pausedReq Paused request event data
169
* @return Either HTTP request or response object
170
*/
171
public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedReq);
172
173
/**
174
* Extract request ID from paused request
175
* @param pausedReq Paused request event
176
* @return String request identifier
177
*/
178
protected String getRequestId(RequestPaused pausedReq);
179
180
/**
181
* Continue request without modification
182
* @param pausedRequest Paused request to continue
183
* @return Command to continue request unchanged
184
*/
185
protected Command<Void> continueWithoutModification(RequestPaused pausedRequest);
186
187
/**
188
* Continue request with modifications
189
* @param pausedReq Original paused request
190
* @param req Modified HTTP request
191
* @return Command to continue with modified request
192
*/
193
protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req);
194
195
/**
196
* Fulfill request with custom response
197
* @param pausedReq Original paused request
198
* @param res Custom HTTP response to return
199
* @return Command to fulfill request with response
200
*/
201
protected Command<Void> fulfillRequest(RequestPaused pausedReq, HttpResponse res);
202
```
203
204
**Usage Examples:**
205
206
```java
207
import org.openqa.selenium.remote.http.HttpRequest;
208
import org.openqa.selenium.remote.http.HttpResponse;
209
import org.openqa.selenium.internal.Either;
210
211
// Set up request interception
212
network.requestPausedEvent().addListener(pausedRequest -> {
213
Either<HttpRequest, HttpResponse> message = network.createSeMessages(pausedRequest);
214
215
if (message.isLeft()) {
216
// Handle intercepted request
217
HttpRequest request = message.left();
218
219
if (request.getUri().contains("/api/data")) {
220
// Modify request headers
221
request.addHeader("X-Test-Mode", "true");
222
devTools.send(network.continueRequest(pausedRequest, request));
223
} else {
224
// Continue without modification
225
devTools.send(network.continueWithoutModification(pausedRequest));
226
}
227
} else {
228
// Handle intercepted response
229
HttpResponse response = message.right();
230
231
if (response.getStatus() == 404) {
232
// Fulfill with custom response
233
HttpResponse customResponse = new HttpResponse()
234
.setStatus(200)
235
.setContent("Custom content".getBytes());
236
devTools.send(network.fulfillRequest(pausedRequest, customResponse));
237
}
238
}
239
});
240
```
241
242
## Types
243
244
### Core Network Types
245
246
```java { .api }
247
// Network domain implementation
248
public class V105Network extends Network<AuthRequired, RequestPaused> {
249
public V105Network(DevTools devTools);
250
}
251
252
// User agent configuration
253
public static class UserAgent {
254
String userAgent();
255
String acceptLanguage();
256
String platform();
257
}
258
```
259
260
### Authentication Types
261
262
```java { .api }
263
// Authentication challenge event
264
public class AuthRequired {
265
AuthChallenge getAuthChallenge();
266
RequestId getRequestId();
267
}
268
269
// Authentication challenge details
270
public class AuthChallenge {
271
String getOrigin();
272
String getScheme();
273
String getRealm();
274
}
275
276
// Username and password credentials
277
public class UsernameAndPassword {
278
UsernameAndPassword(String username, String password);
279
String username();
280
String password();
281
}
282
```
283
284
### Request Interception Types
285
286
```java { .api }
287
// Paused request for interception
288
public class RequestPaused {
289
RequestId getRequestId();
290
Request getRequest();
291
Optional<Integer> getResponseStatusCode();
292
Optional<String> getResponseErrorReason();
293
Optional<List<HeaderEntry>> getResponseHeaders();
294
}
295
296
// HTTP request details
297
public class Request {
298
String getMethod();
299
String getUrl();
300
Map<String, Object> getHeaders();
301
Optional<String> getPostData();
302
}
303
304
// HTTP header entry
305
public class HeaderEntry {
306
HeaderEntry(String name, String value);
307
String getName();
308
String getValue();
309
}
310
311
// Request identifier
312
public class RequestId {
313
RequestId(String id);
314
String toString();
315
}
316
```