Overview
HasNetworkConditions is a Java interface that enables Chromium-based drivers to get, set, and delete simulated network conditions such as offline mode, latency, and throughput.
Description
The HasNetworkConditions interface, annotated with @Beta, declares three methods for network condition management: getNetworkConditions() retrieves the currently active network simulation settings, setNetworkConditions(ChromiumNetworkConditions) applies new network constraints, and deleteNetworkConditions() resets them to defaults. The network conditions must be set before they can be retrieved. This interface works in conjunction with the ChromiumNetworkConditions data class and is implemented by Chromium-based driver classes such as ChromeDriver and EdgeDriver.
Usage
Use HasNetworkConditions when testing web application behavior under constrained network environments -- for example, simulating slow 3G connections, high latency, or fully offline scenarios. Cast the driver instance to HasNetworkConditions to access the network simulation methods.
Code Reference
Source Location
Signature
@Beta
public interface HasNetworkConditions {
ChromiumNetworkConditions getNetworkConditions();
void setNetworkConditions(ChromiumNetworkConditions networkConditions);
void deleteNetworkConditions();
}
Import
import org.openqa.selenium.chromium.HasNetworkConditions;
I/O Contract
Methods
| Method |
Parameters |
Return Type |
Description
|
getNetworkConditions() |
none |
ChromiumNetworkConditions |
Returns the current network condition settings. Network conditions must have been set previously via setNetworkConditions before calling this method.
|
setNetworkConditions(ChromiumNetworkConditions) |
networkConditions -- an object containing the desired network simulation settings |
void |
Applies the specified network limitations (offline state, latency, throughput) to the browser session.
|
deleteNetworkConditions() |
none |
void |
Resets the network conditions to the default (unconstrained) settings, removing any previously applied simulation.
|
Input
| Parameter |
Type |
Description
|
networkConditions |
ChromiumNetworkConditions |
A data object containing offline (boolean), latency (Duration), downloadThroughput (int, kb/s), and uploadThroughput (int, kb/s) values.
|
Output
| Method |
Return Type |
Description
|
getNetworkConditions() |
ChromiumNetworkConditions |
The currently active network condition settings as a data object.
|
setNetworkConditions() |
void |
No return value.
|
deleteNetworkConditions() |
void |
No return value.
|
Usage Examples
ChromeDriver driver = new ChromeDriver();
// Cast the driver to HasNetworkConditions
HasNetworkConditions networkDriver = (HasNetworkConditions) driver;
// Simulate a slow 3G network
ChromiumNetworkConditions slow3G = new ChromiumNetworkConditions();
slow3G.setOffline(false);
slow3G.setLatency(Duration.ofMillis(200));
slow3G.setDownloadThroughput(500); // 500 kb/s
slow3G.setUploadThroughput(250); // 250 kb/s
networkDriver.setNetworkConditions(slow3G);
// Verify applied conditions
ChromiumNetworkConditions current = networkDriver.getNetworkConditions();
System.out.println("Latency: " + current.getLatency().toMillis() + "ms");
System.out.println("Download: " + current.getDownloadThroughput() + " kb/s");
// Test offline behavior
ChromiumNetworkConditions offline = new ChromiumNetworkConditions();
offline.setOffline(true);
networkDriver.setNetworkConditions(offline);
// Navigate and verify offline page behavior
driver.get("https://www.example.com");
// Restore normal network conditions
networkDriver.deleteNetworkConditions();
driver.quit();
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.