Selenium Chrome Driver provides WebDriver implementation for Google Chrome with Chrome DevTools Protocol support, network conditions simulation, permissions management, media casting capabilities, and Chrome-specific service configuration.
—
Network conditions simulation allows testing applications under various connectivity scenarios including different latencies, throughput limitations, and offline conditions.
Retrieve current network condition settings.
public ChromiumNetworkConditions getNetworkConditions();Returns: ChromiumNetworkConditions - Current network condition settings
Usage Example:
// ChromiumDriver is typically used through subclasses like ChromeDriver, EdgeDriver
ChromiumDriver driver = ...; // Obtain from ChromeDriver, EdgeDriver, etc.
// Get current network conditions
ChromiumNetworkConditions conditions = driver.getNetworkConditions();
boolean isOffline = conditions.getOffline();
Duration latency = conditions.getLatency();Apply network condition settings to simulate different connectivity scenarios.
public void setNetworkConditions(ChromiumNetworkConditions networkConditions);Parameters:
networkConditions (ChromiumNetworkConditions): Network conditions to applyUsage Example:
import java.time.Duration;
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
// Simulate slow 3G connection
conditions.setOffline(false);
conditions.setLatency(Duration.ofMillis(300));
conditions.setDownloadThroughput(400); // kb/s
conditions.setUploadThroughput(200); // kb/s
driver.setNetworkConditions(conditions);Reset network conditions to default (no simulation).
public void deleteNetworkConditions();Usage Example:
// Reset to normal network conditions
driver.deleteNetworkConditions();Control whether the network is simulated as offline.
public boolean getOffline();
public void setOffline(boolean offline);Parameters:
offline (boolean): Whether to simulate offline modeUsage Example:
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
// Enable offline mode
conditions.setOffline(true);
driver.setNetworkConditions(conditions);
// Navigate - this will fail due to offline mode
driver.get("https://example.com"); // Will show Chrome's offline page
// Disable offline mode
conditions.setOffline(false);
driver.setNetworkConditions(conditions);
driver.get("https://example.com"); // Will work normallyConfigure simulated network latency.
public Duration getLatency();
public void setLatency(Duration latency);Parameters:
latency (Duration): Network latency to simulateUsage Example:
import java.time.Duration;
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
// Simulate high latency (500ms)
conditions.setLatency(Duration.ofMillis(500));
// Simulate moderate latency (100ms)
conditions.setLatency(Duration.ofMillis(100));
// No additional latency
conditions.setLatency(Duration.ZERO);Set download bandwidth limitations.
public int getDownloadThroughput();
public void setDownloadThroughput(int downloadThroughput);Parameters:
downloadThroughput (int): Download speed in kilobytes per secondUsage Example:
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
// Simulate different connection speeds
conditions.setDownloadThroughput(50); // Slow 2G: ~50 KB/s
conditions.setDownloadThroughput(250); // 3G: ~250 KB/s
conditions.setDownloadThroughput(1500); // 4G: ~1.5 MB/s
conditions.setDownloadThroughput(10000); // Fast broadband: ~10 MB/sSet upload bandwidth limitations.
public int getUploadThroughput();
public void setUploadThroughput(int uploadThroughput);Parameters:
uploadThroughput (int): Upload speed in kilobytes per secondUsage Example:
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
// Typical upload speeds (usually lower than download)
conditions.setUploadThroughput(25); // Slow 2G upload
conditions.setUploadThroughput(125); // 3G upload
conditions.setUploadThroughput(750); // 4G upload
conditions.setUploadThroughput(5000); // Fast broadband upload// Slow 2G
ChromiumNetworkConditions slow2G = new ChromiumNetworkConditions();
slow2G.setOffline(false);
slow2G.setLatency(Duration.ofMillis(2000));
slow2G.setDownloadThroughput(50);
slow2G.setUploadThroughput(25);
// 3G
ChromiumNetworkConditions threeG = new ChromiumNetworkConditions();
threeG.setOffline(false);
threeG.setLatency(Duration.ofMillis(300));
threeG.setDownloadThroughput(400);
threeG.setUploadThroughput(200);
// 4G
ChromiumNetworkConditions fourG = new ChromiumNetworkConditions();
fourG.setOffline(false);
fourG.setLatency(Duration.ofMillis(50));
fourG.setDownloadThroughput(1500);
fourG.setUploadThroughput(750);// Complete offline mode
ChromiumNetworkConditions offline = new ChromiumNetworkConditions();
offline.setOffline(true);
driver.setNetworkConditions(offline);
// Test offline functionality
driver.get("https://example.com"); // Will show offline page
// Test service worker, cached content, etc.// Simulate satellite connection (high latency, decent throughput)
ChromiumNetworkConditions satellite = new ChromiumNetworkConditions();
satellite.setOffline(false);
satellite.setLatency(Duration.ofMillis(800)); // High latency
satellite.setDownloadThroughput(2000); // Good throughput
satellite.setUploadThroughput(1000);public static final String OFFLINE = "offline";
public static final String LATENCY = "latency";
public static final String DOWNLOAD_THROUGHPUT = "download_throughput";
public static final String UPLOAD_THROUGHPUT = "upload_throughput";These constants represent the keys used in the underlying network conditions implementation.
The network conditions functionality integrates with ChromiumDriverCommandExecutor through these commands:
// Command constants from AddHasNetworkConditions
public static final String GET_NETWORK_CONDITIONS = "getNetworkConditions";
public static final String SET_NETWORK_CONDITIONS = "setNetworkConditions";
public static final String DELETE_NETWORK_CONDITIONS = "deleteNetworkConditions";import org.openqa.selenium.chromium.ChromiumDriver;
import org.openqa.selenium.chromium.ChromiumNetworkConditions;
import java.time.Duration;
public class NetworkConditionsTest {
public void testUnderVariousNetworkConditions() {
ChromiumDriver driver = new ChromiumDriver(commandExecutor, capabilities, "chrome");
try {
// Test under normal conditions
driver.get("https://example.com");
long normalLoadTime = measurePageLoadTime();
// Test under slow 3G
ChromiumNetworkConditions slow3G = new ChromiumNetworkConditions();
slow3G.setLatency(Duration.ofMillis(300));
slow3G.setDownloadThroughput(400);
slow3G.setUploadThroughput(200);
driver.setNetworkConditions(slow3G);
driver.get("https://example.com");
long slow3GLoadTime = measurePageLoadTime();
// Test offline functionality
ChromiumNetworkConditions offline = new ChromiumNetworkConditions();
offline.setOffline(true);
driver.setNetworkConditions(offline);
driver.get("https://example.com");
// Verify offline page or cached content
// Reset conditions
driver.deleteNetworkConditions();
} finally {
driver.quit();
}
}
private long measurePageLoadTime() {
// Implementation to measure page load time
return System.currentTimeMillis();
}
}Network conditions operations may encounter these exceptions:
try {
ChromiumNetworkConditions conditions = new ChromiumNetworkConditions();
conditions.setLatency(Duration.ofMillis(100));
driver.setNetworkConditions(conditions);
} catch (IllegalArgumentException e) {
System.err.println("Invalid network conditions: " + e.getMessage());
} catch (WebDriverException e) {
System.err.println("Failed to set network conditions: " + e.getMessage());
}Install with Tessl CLI
npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-chromium-driver