0
# Network Operations
1
2
Advanced network functionality for request/response interception, authentication handling, user agent modification, and comprehensive traffic monitoring using Chrome DevTools Protocol v110.
3
4
## Capabilities
5
6
### Network Domain Wrapper
7
8
High-level wrapper for CDP network functionality providing type-safe access to network monitoring and interception.
9
10
```java { .api }
11
/**
12
* Network domain wrapper for CDP v110 network operations
13
*/
14
public class v110Network extends Network<AuthRequired, RequestPaused> {
15
/**
16
* Initialize network domain with DevTools session
17
* @param devTools Active DevTools session
18
*/
19
public v110Network(DevTools devTools);
20
}
21
```
22
23
### User Agent Management
24
25
Override the browser's user agent string for all network requests.
26
27
```java { .api }
28
/**
29
* Set custom user agent for network requests
30
* @param userAgent User agent configuration
31
* @return Command to execute
32
*/
33
protected Command<Void> setUserAgentOverride(UserAgent userAgent);
34
35
/**
36
* User agent configuration
37
*/
38
public static class UserAgent {
39
private final String userAgent;
40
private final String acceptLanguage;
41
private final String platform;
42
43
public UserAgent(String userAgent);
44
public UserAgent(String userAgent, String acceptLanguage, String platform);
45
46
public String userAgent();
47
public String acceptLanguage();
48
public String platform();
49
}
50
```
51
52
**Usage Example:**
53
54
```java
55
import org.openqa.selenium.devtools.v110.v110Network;
56
import org.openqa.selenium.devtools.idealized.Network.UserAgent;
57
58
v110Network network = new v110Network(devTools);
59
UserAgent customAgent = new UserAgent(
60
"Mozilla/5.0 (Custom Browser) WebKit/537.36",
61
"en-US,en;q=0.9",
62
"Custom Platform"
63
);
64
65
devTools.send(network.setUserAgentOverride(customAgent));
66
```
67
68
### Network Caching Control
69
70
Enable or disable browser network caching for testing and debugging purposes.
71
72
```java { .api }
73
/**
74
* Enable network caching
75
* @return Command to execute
76
*/
77
protected Command<Void> enableNetworkCaching();
78
79
/**
80
* Disable network caching
81
* @return Command to execute
82
*/
83
protected Command<Void> disableNetworkCaching();
84
```
85
86
### Request Interception
87
88
Intercept and modify HTTP requests and responses using the Fetch domain.
89
90
```java { .api }
91
/**
92
* Enable fetch domain for all request patterns
93
* @return Command to enable request interception
94
*/
95
protected Command<Void> enableFetchForAllPatterns();
96
97
/**
98
* Disable fetch domain and request interception
99
* @return Command to disable interception
100
*/
101
protected Command<Void> disableFetch();
102
103
/**
104
* Get request paused events for interception
105
* @return Event stream for paused requests
106
*/
107
public Event<RequestPaused> requestPausedEvent();
108
```
109
110
### Authentication Handling
111
112
Handle HTTP authentication challenges automatically.
113
114
```java { .api }
115
/**
116
* Get authentication required events
117
* @return Event stream for auth challenges
118
*/
119
protected Event<AuthRequired> authRequiredEvent();
120
121
/**
122
* Continue with provided credentials
123
* @param authRequired Auth challenge event
124
* @param credentials Username and password
125
* @return Command to provide credentials
126
*/
127
protected Command<Void> continueWithAuth(AuthRequired authRequired, UsernameAndPassword credentials);
128
129
/**
130
* Cancel authentication challenge
131
* @param authRequired Auth challenge event
132
* @return Command to cancel auth
133
*/
134
protected Command<Void> cancelAuth(AuthRequired authRequired);
135
```
136
137
**Usage Example:**
138
139
```java
140
import org.openqa.selenium.UsernameAndPassword;
141
import org.openqa.selenium.devtools.v110.fetch.model.AuthRequired;
142
143
// Add authentication handler
144
network.addAuthHandler(
145
uri -> uri.contains("secure-api.com"),
146
new UsernameAndPassword("api-user", "secret-key")
147
);
148
149
// Or handle auth events manually
150
devTools.addListener(network.authRequiredEvent(), authRequired -> {
151
String origin = network.getUriFrom(authRequired);
152
if (origin.contains("trusted-site.com")) {
153
devTools.send(network.continueWithAuth(
154
authRequired,
155
new UsernameAndPassword("username", "password")
156
));
157
} else {
158
devTools.send(network.cancelAuth(authRequired));
159
}
160
});
161
```
162
163
### Request and Response Processing
164
165
Process intercepted requests and responses with full control over headers, body, and status.
166
167
```java { .api }
168
/**
169
* Continue request without modification
170
* @param pausedRequest Intercepted request
171
* @return Command to continue
172
*/
173
protected Command<Void> continueWithoutModification(RequestPaused pausedRequest);
174
175
/**
176
* Continue with modified request
177
* @param pausedReq Original intercepted request
178
* @param req Modified request to send
179
* @return Command to continue with modifications
180
*/
181
protected Command<Void> continueRequest(RequestPaused pausedReq, HttpRequest req);
182
183
/**
184
* Fulfill request with custom response
185
* @param pausedReq Intercepted request
186
* @param res Custom response to return
187
* @return Command to fulfill with response
188
*/
189
protected Command<Void> fulfillRequest(RequestPaused pausedReq, HttpResponse res);
190
191
/**
192
* Create Selenium HTTP message from CDP request/response
193
* @param pausedReq CDP request paused event
194
* @return Either HTTP request or response
195
*/
196
public Either<HttpRequest, HttpResponse> createSeMessages(RequestPaused pausedReq);
197
```
198
199
## CDP Domain Classes
200
201
### Fetch Domain
202
203
Direct access to CDP Fetch domain for advanced request interception.
204
205
```java { .api }
206
import org.openqa.selenium.devtools.v110.fetch.Fetch;
207
import org.openqa.selenium.devtools.v110.fetch.model.*;
208
209
/**
210
* Enable fetch domain with request patterns
211
*/
212
public static Command<Void> enable(
213
Optional<List<RequestPattern>> patterns,
214
Optional<Boolean> handleAuthRequests
215
);
216
217
/**
218
* Continue intercepted request
219
*/
220
public static Command<Void> continueRequest(
221
RequestId requestId,
222
Optional<String> url,
223
Optional<String> method,
224
Optional<String> postData,
225
Optional<List<HeaderEntry>> headers,
226
Optional<Boolean> interceptResponse
227
);
228
229
/**
230
* Fulfill request with response
231
*/
232
public static Command<Void> fulfillRequest(
233
RequestId requestId,
234
Integer responseCode,
235
Optional<List<HeaderEntry>> responseHeaders,
236
Optional<String> binaryResponseHeaders,
237
Optional<String> body,
238
Optional<String> responsePhrase
239
);
240
```
241
242
### Network Domain
243
244
Direct access to CDP Network domain for low-level network operations.
245
246
```java { .api }
247
import org.openqa.selenium.devtools.v110.network.Network;
248
249
/**
250
* Enable network domain
251
*/
252
public static Command<Void> enable(
253
Optional<Integer> maxTotalBufferSize,
254
Optional<Integer> maxResourceBufferSize,
255
Optional<Integer> maxPostDataSize
256
);
257
258
/**
259
* Set user agent override
260
*/
261
public static Command<Void> setUserAgentOverride(
262
String userAgent,
263
Optional<String> acceptLanguage,
264
Optional<String> platform,
265
Optional<UserAgentMetadata> userAgentMetadata
266
);
267
268
/**
269
* Control network cache
270
*/
271
public static Command<Void> setCacheDisabled(Boolean cacheDisabled);
272
```
273
274
## Model Classes
275
276
### Request and Response Models
277
278
```java { .api }
279
import org.openqa.selenium.devtools.v110.fetch.model.*;
280
import org.openqa.selenium.devtools.v110.network.model.*;
281
282
/**
283
* HTTP request information
284
*/
285
public class Request {
286
String getUrl();
287
String getMethod();
288
Map<String, String> getHeaders();
289
Optional<String> getPostData();
290
}
291
292
/**
293
* Request interception pattern
294
*/
295
public class RequestPattern {
296
Optional<String> getUrlPattern();
297
Optional<ResourceType> getResourceType();
298
Optional<RequestStage> getRequestStage();
299
}
300
301
/**
302
* Authentication challenge
303
*/
304
public class AuthRequired {
305
RequestId getRequestId();
306
AuthChallenge getAuthChallenge();
307
}
308
309
/**
310
* Paused request for interception
311
*/
312
public class RequestPaused {
313
RequestId getRequestId();
314
Request getRequest();
315
Optional<Integer> getResponseStatusCode();
316
Optional<List<HeaderEntry>> getResponseHeaders();
317
}
318
319
/**
320
* HTTP header entry
321
*/
322
public class HeaderEntry {
323
String getName();
324
String getValue();
325
}
326
```
327
328
## Error Handling
329
330
Network operations may encounter various error conditions:
331
332
```java
333
import org.openqa.selenium.devtools.DevToolsException;
334
335
// Handle network command failures
336
try {
337
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
338
} catch (DevToolsException e) {
339
System.err.println("Failed to enable network domain: " + e.getMessage());
340
}
341
342
// Handle request interception errors
343
devTools.addListener(network.requestPausedEvent(), pausedRequest -> {
344
try {
345
// Process request
346
devTools.send(network.continueWithoutModification(pausedRequest));
347
} catch (DevToolsException e) {
348
System.err.println("Failed to continue request: " + e.getMessage());
349
}
350
});
351
```