Network throttling is a technique used to simulate slower internet connection speeds to test the performance of a website or web application. Selenium is a popular open-source tool that can automate web browsers and perform network throttling during tests.
We will show you how to set network conditions in chrome with the help of selenium.
- Using CommandExecutor ( can be used with Selenium 3 )
- and by using DevTools ( Only works with Selenium 4 )
Let’s look at each of them one by one.
Network Throttling using CommandExecutor
We will use the getCommandExecutor() to get the CommandExecutor and then put the network conditions in a Map.
CommandExecutor executor = driver.getCommandExecutor();
// Set the network conditions
Map<String, Object> map = new HashMap<String, Object>();
map.put("offline", false);
map.put("latency", 1);
map.put("download_throughput", 5500);
map.put("upload_throughput", 5500);
Then we will execute the conditions using the below code.
Response response = executor.execute(new Command(driver.getSessionId(), "setNetworkConditions",
ImmutableMap.of("network_conditions", ImmutableMap.copyOf(map))));
And after this, we can run start our UI automation which will now be executed under the set network conditions.
driver.get("https://www.youtube.com");
Whole Code for Network throttling
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.Response;
import com.google.common.collect.ImmutableMap;
public class CodekruTest {
public static void main(String[] args) throws IOException {
// pass the path of the chromedriver location in the second argument
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
CommandExecutor executor = driver.getCommandExecutor();
// Set the network conditions
Map<String, Object> map = new HashMap<String, Object>();
map.put("offline", false);
map.put("latency", 1);
map.put("download_throughput", 5500);
map.put("upload_throughput", 5500);
Response response = executor.execute(new Command(driver.getSessionId(), "setNetworkConditions",
ImmutableMap.of("network_conditions", ImmutableMap.copyOf(map))));
driver.get("https://www.youtube.com");
}
}
Above code will open “https://www.youtube.com” under the network conditions set in the program.
You might have noticed that we have used ChromeDriver driver = new ChromeDriver()
instead of WebDriver driver = new ChromeDriver()
. WebDriver interface doesn’t have the getCommandExecutor() method, so we must use the specific driver to make objects.
Note: The above program will not work with the firefox browser, and we will get the below error if we try this.
Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: setNetworkConditions
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'LAPTOP-9A8M7TQJ', ip: '192.168.1.3', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '15.0.1'
Driver info: driver.version: unknown
at org.openqa.selenium.remote.http.AbstractHttpCommandCodec.encode(AbstractHttpCommandCodec.java:218)
at org.openqa.selenium.remote.http.AbstractHttpCommandCodec.encode(AbstractHttpCommandCodec.java:117)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:152)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
Network Throttling using DevTools
Selenium 4 has added a feature to access the developer tools and thus provides the support to simulate various network conditions.
Note: We are using Selenium 4.7.0 with chrome version 109.
First, we will create a session with DevTools.
DevTools devTools = ((ChromeDriver) driver).getDevTools();
devTools.createSession();
and then we can use network throttling using the below commands.
devTools.send(Network.enable(Optional.of(1000000), Optional.empty(), Optional.empty()));
devTools.send(Network.emulateNetworkConditions(false, 1000, 1000000, 1000000, Optional.of(ConnectionType.CELLULAR3G)));
You can see that we have used emulateNetworkConditions function, which accepts five parameters –
- offline – It accepts a boolean value.
- latency – The minimum latency from the request sent to the response headers received. The measurement used here is in milliseconds (ms)
- downloadThroughput – It is the maximal aggregated download throughput (bytes/sec). If we send -1 in this parameter, it will disable the download throttling.
- uploadThroughput – It is the maximal aggregated upload throughput (bytes/sec). If we send -1 in this parameter, it will disable the upload throttling.
- connectionType – An enum allows multiple connection types like CELLULAR2G, CELLULAR3G, CELLULAR4G, etc.
Whole Code for Network throttling using DevTools
import java.io.IOException;
import java.util.Optional;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v85.network.Network;
import org.openqa.selenium.devtools.v85.network.model.ConnectionType;
public class CodekruTest {
public static void main(String[] args) throws IOException {
// pass the path of the chromedriver location in the second argument
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
DevTools devTools = ((ChromeDriver) driver).getDevTools();
devTools.createSession();
devTools.send(Network.enable(Optional.of(1000000), Optional.empty(), Optional.empty()));
devTools.send(
Network.emulateNetworkConditions(false, 100, 1000000, 1000000, Optional.of(ConnectionType.CELLULAR3G)));
driver.get("https://www.youtube.com");
}
}
Above code will set the network conditions as passed in the emulateNetworkConditions() method and then execute the selenium automation on the set conditions only.
It is important to note that network throttling in Selenium is not a substitute for real-world testing. The results of network throttling tests may not always accurately reflect the performance of a website or web application in a real-world environment. However, it can be a useful tool for identifying potential issues and improving the overall performance of a website or web application.
This is it. We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.