0
# Network Conditions
1
2
Network conditions simulation allows testing applications under various connectivity scenarios including different latencies, throughput limitations, and offline conditions.
3
4
## Network Conditions Management
5
6
### Get Network Conditions
7
8
Retrieve current network condition settings.
9
10
```java { .api }
11
public ChromiumNetworkConditions getNetworkConditions();
12
```
13
14
**Returns:** ChromiumNetworkConditions - Current network condition settings
15
16
**Usage Example:**
17
```java
18
// ChromiumDriver is typically used through subclasses like ChromeDriver, EdgeDriver
19
ChromiumDriver driver = ...; // Obtain from ChromeDriver, EdgeDriver, etc.
20
21
// Get current network conditions
22
ChromiumNetworkConditions conditions = driver.getNetworkConditions();
23
boolean isOffline = conditions.getOffline();
24
Duration latency = conditions.getLatency();
25
```
26
27
### Set Network Conditions
28
29
Apply network condition settings to simulate different connectivity scenarios.
30
31
```java { .api }
32
public void setNetworkConditions(ChromiumNetworkConditions networkConditions);
33
```
34
35
**Parameters:**
36
- `networkConditions` (ChromiumNetworkConditions): Network conditions to apply
37
38
**Usage Example:**
39
```java
40
import java.time.Duration;
41
42
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
43
44
// Simulate slow 3G connection
45
conditions.setOffline(false);
46
conditions.setLatency(Duration.ofMillis(300));
47
conditions.setDownloadThroughput(400); // kb/s
48
conditions.setUploadThroughput(200); // kb/s
49
50
driver.setNetworkConditions(conditions);
51
```
52
53
### Delete Network Conditions
54
55
Reset network conditions to default (no simulation).
56
57
```java { .api }
58
public void deleteNetworkConditions();
59
```
60
61
**Usage Example:**
62
```java
63
// Reset to normal network conditions
64
driver.deleteNetworkConditions();
65
```
66
67
## ChromiumNetworkConditions Configuration
68
69
### Offline Mode
70
71
Control whether the network is simulated as offline.
72
73
```java { .api }
74
public boolean getOffline();
75
public void setOffline(boolean offline);
76
```
77
78
**Parameters:**
79
- `offline` (boolean): Whether to simulate offline mode
80
81
**Usage Example:**
82
```java
83
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
84
85
// Enable offline mode
86
conditions.setOffline(true);
87
driver.setNetworkConditions(conditions);
88
89
// Navigate - this will fail due to offline mode
90
driver.get("https://example.com"); // Will show Chrome's offline page
91
92
// Disable offline mode
93
conditions.setOffline(false);
94
driver.setNetworkConditions(conditions);
95
driver.get("https://example.com"); // Will work normally
96
```
97
98
### Network Latency
99
100
Configure simulated network latency.
101
102
```java { .api }
103
public Duration getLatency();
104
public void setLatency(Duration latency);
105
```
106
107
**Parameters:**
108
- `latency` (Duration): Network latency to simulate
109
110
**Usage Example:**
111
```java
112
import java.time.Duration;
113
114
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
115
116
// Simulate high latency (500ms)
117
conditions.setLatency(Duration.ofMillis(500));
118
119
// Simulate moderate latency (100ms)
120
conditions.setLatency(Duration.ofMillis(100));
121
122
// No additional latency
123
conditions.setLatency(Duration.ZERO);
124
```
125
126
### Download Throughput
127
128
Set download bandwidth limitations.
129
130
```java { .api }
131
public int getDownloadThroughput();
132
public void setDownloadThroughput(int downloadThroughput);
133
```
134
135
**Parameters:**
136
- `downloadThroughput` (int): Download speed in kilobytes per second
137
138
**Usage Example:**
139
```java
140
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
141
142
// Simulate different connection speeds
143
conditions.setDownloadThroughput(50); // Slow 2G: ~50 KB/s
144
conditions.setDownloadThroughput(250); // 3G: ~250 KB/s
145
conditions.setDownloadThroughput(1500); // 4G: ~1.5 MB/s
146
conditions.setDownloadThroughput(10000); // Fast broadband: ~10 MB/s
147
```
148
149
### Upload Throughput
150
151
Set upload bandwidth limitations.
152
153
```java { .api }
154
public int getUploadThroughput();
155
public void setUploadThroughput(int uploadThroughput);
156
```
157
158
**Parameters:**
159
- `uploadThroughput` (int): Upload speed in kilobytes per second
160
161
**Usage Example:**
162
```java
163
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
164
165
// Typical upload speeds (usually lower than download)
166
conditions.setUploadThroughput(25); // Slow 2G upload
167
conditions.setUploadThroughput(125); // 3G upload
168
conditions.setUploadThroughput(750); // 4G upload
169
conditions.setUploadThroughput(5000); // Fast broadband upload
170
```
171
172
## Common Network Profiles
173
174
### Mobile Network Profiles
175
176
```java
177
// Slow 2G
178
ChromiumNetworkConditions slow2G = new ChromiumNetworkConditions();
179
slow2G.setOffline(false);
180
slow2G.setLatency(Duration.ofMillis(2000));
181
slow2G.setDownloadThroughput(50);
182
slow2G.setUploadThroughput(25);
183
184
// 3G
185
ChromiumNetworkConditions threeG = new ChromiumNetworkConditions();
186
threeG.setOffline(false);
187
threeG.setLatency(Duration.ofMillis(300));
188
threeG.setDownloadThroughput(400);
189
threeG.setUploadThroughput(200);
190
191
// 4G
192
ChromiumNetworkConditions fourG = new ChromiumNetworkConditions();
193
fourG.setOffline(false);
194
fourG.setLatency(Duration.ofMillis(50));
195
fourG.setDownloadThroughput(1500);
196
fourG.setUploadThroughput(750);
197
```
198
199
### Offline Testing
200
201
```java
202
// Complete offline mode
203
ChromiumNetworkConditions offline = new ChromiumNetworkConditions();
204
offline.setOffline(true);
205
driver.setNetworkConditions(offline);
206
207
// Test offline functionality
208
driver.get("https://example.com"); // Will show offline page
209
// Test service worker, cached content, etc.
210
```
211
212
### High Latency Testing
213
214
```java
215
// Simulate satellite connection (high latency, decent throughput)
216
ChromiumNetworkConditions satellite = new ChromiumNetworkConditions();
217
satellite.setOffline(false);
218
satellite.setLatency(Duration.ofMillis(800)); // High latency
219
satellite.setDownloadThroughput(2000); // Good throughput
220
satellite.setUploadThroughput(1000);
221
```
222
223
## Constants
224
225
```java { .api }
226
public static final String OFFLINE = "offline";
227
public static final String LATENCY = "latency";
228
public static final String DOWNLOAD_THROUGHPUT = "download_throughput";
229
public static final String UPLOAD_THROUGHPUT = "upload_throughput";
230
```
231
232
These constants represent the keys used in the underlying network conditions implementation.
233
234
## Command Integration
235
236
The network conditions functionality integrates with ChromiumDriverCommandExecutor through these commands:
237
238
```java { .api }
239
// Command constants from AddHasNetworkConditions
240
public static final String GET_NETWORK_CONDITIONS = "getNetworkConditions";
241
public static final String SET_NETWORK_CONDITIONS = "setNetworkConditions";
242
public static final String DELETE_NETWORK_CONDITIONS = "deleteNetworkConditions";
243
```
244
245
## Complete Testing Example
246
247
```java
248
import org.openqa.selenium.chromium.ChromiumDriver;
249
import org.openqa.selenium.chromium.ChromiumNetworkConditions;
250
import java.time.Duration;
251
252
public class NetworkConditionsTest {
253
public void testUnderVariousNetworkConditions() {
254
ChromiumDriver driver = new ChromiumDriver(commandExecutor, capabilities, "chrome");
255
256
try {
257
// Test under normal conditions
258
driver.get("https://example.com");
259
long normalLoadTime = measurePageLoadTime();
260
261
// Test under slow 3G
262
ChromiumNetworkConditions slow3G = new ChromiumNetworkConditions();
263
slow3G.setLatency(Duration.ofMillis(300));
264
slow3G.setDownloadThroughput(400);
265
slow3G.setUploadThroughput(200);
266
267
driver.setNetworkConditions(slow3G);
268
driver.get("https://example.com");
269
long slow3GLoadTime = measurePageLoadTime();
270
271
// Test offline functionality
272
ChromiumNetworkConditions offline = new ChromiumNetworkConditions();
273
offline.setOffline(true);
274
275
driver.setNetworkConditions(offline);
276
driver.get("https://example.com");
277
// Verify offline page or cached content
278
279
// Reset conditions
280
driver.deleteNetworkConditions();
281
282
} finally {
283
driver.quit();
284
}
285
}
286
287
private long measurePageLoadTime() {
288
// Implementation to measure page load time
289
return System.currentTimeMillis();
290
}
291
}
292
```
293
294
## Error Handling
295
296
Network conditions operations may encounter these exceptions:
297
298
- **IllegalArgumentException**: When null network conditions are provided
299
- **WebDriverException**: When network condition commands fail to execute
300
301
```java
302
try {
303
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
304
conditions.setLatency(Duration.ofMillis(100));
305
driver.setNetworkConditions(conditions);
306
} catch (IllegalArgumentException e) {
307
System.err.println("Invalid network conditions: " + e.getMessage());
308
} catch (WebDriverException e) {
309
System.err.println("Failed to set network conditions: " + e.getMessage());
310
}
311
```
312
313
## Best Practices
314
315
1. **Realistic Profiles**: Use realistic network profiles based on actual mobile network conditions
316
2. **Gradual Testing**: Test with progressively slower conditions to identify performance bottlenecks
317
3. **Offline Testing**: Always test offline scenarios for PWAs and applications with offline capabilities
318
4. **Cleanup**: Reset network conditions after tests to avoid affecting subsequent tests
319
5. **Load Time Monitoring**: Measure and assert on page load times under different conditions
320
6. **Progressive Enhancement**: Use network simulation to test progressive loading strategies
321
7. **Error Scenarios**: Test how applications handle network timeouts and interruptions
322
8. **Mobile First**: Start testing with mobile network conditions, then improve for faster connections