'Network connection throtling with Selenium - Java
I have been trying to implement network throttling with Selenium-WebDriver based on the snippet available here in this post. https://stackoverflow.com/a/56526094/2122388
Re-posting the code from above link:
protected void networkThrotting() throws IOException {
Map map = new HashMap();
map.put("offline", false);
map.put("latency", 5);
map.put("download_throughput", 500);
map.put("upload_throughput", 1024);
CommandExecutor executor = ((ChromeDriver)driver).getCommandExecutor();
Response response = executor.execute(
new Command(((ChromeDriver)driver).getSessionId(), "setNetworkConditions", ImmutableMap.of("network_conditions", ImmutableMap.copyOf(map)))
);
}
It throws as exception like org.openqa.selenium.UnsupportedCommandException: setNetworkConditions
. With my debugging, found like there is no definition for setNetworkConditions
in AbstractHttpCommandCodec
but found setNetworkConnection
. SO tried changing this as below:
protected void networkThrotting() throws IOException {
Map map = new HashMap();
map.put("offline", false);
map.put("latency", 5);
map.put("download_throughput", 500);
map.put("upload_throughput", 1024);
CommandExecutor executor = ((ChromeDriver)driver).getCommandExecutor();
Response response = executor.execute(
new Command(((ChromeDriver)driver).getSessionId(), "setNetworkConnection", ImmutableMap.of("network_connection", ImmutableMap.copyOf(map)))
);
}
It does not throw any exception and let the test pass, but there is no change in the network speed as intended. I am using Selenium 3.x and Chrome 100.x.
So any input here is much appreciated.
Solution 1:[1]
If anyone else is here because of same issue, then here is the solutions (alternate) I figured out at the end:
- Upgrade to Selenium-4.x and use DevTools can make things much easier and straightforward
- Use BrowserMob-Proxy and play around
I chose Option-2 for implementing the network throttling, since I had some limitations in upgrading my Selenium version to 4.x (but upgrading the Selenium version is much simple and easy solution).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | zeal |