How to set Network Conditions in Selenium?


If you want to run your Selenium script in different network conditions, you can do it using Chrome CommandExecutor.
Using Chrome Developer Tools, you can emulate different network conditions to check your app's performance. If you want this to be implemented in automation testing, we have a command to do that. Let's see with an example.

Set Network Conditions in Selenium
System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");

ChromeDriver driver = new ChromeDriver();
        
CommandExecutor executor = driver.getCommandExecutor();
        
//Set the conditions
Map map = new HashMap();
map.put("offline", false);
map.put("latency", 5);
map.put("download_throughput", 5000);
map.put("upload_throughput", 5000);

Response response = executor.execute(new Command(driver.getSessionId(),"setNetworkConditions", ImmutableMap.of("network_conditions", ImmutableMap.copyOf(map))));

driver.get("http://google.com");

Initially, we didn't have this implementation in Java bindings to set network conditions. After a fix from Andrii Rohovets, GET_NETWORK_CONDITIONS, SET_NETWORK_CONDITIONS and DELETE_NETWORK_CONDITIONS are available to emulate network conditions in Selenium & Java.


Leave a Reply

Your email address will not be published. Required fields are marked *