1

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.

JIST
  • 1,139
  • 2
  • 8
  • 30
zeal
  • 475
  • 4
  • 11
  • 25

2 Answers2

0

If anyone else is here because of same issue, then here is the solutions (alternate) I figured out at the end:

  1. Upgrade to Selenium-4.x and use DevTools can make things much easier and straightforward
  2. 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).

zeal
  • 475
  • 4
  • 11
  • 25
0

Here is a working example to help others about how to use Selenium-4.x version with DevTools.

try (DevTools devTools = ((ChromeDriver) driver).getDevTools()) {
        devTools.createSession();

        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

        // network offline
        devTools.send(Network.emulateNetworkConditions(true, 0, 0, 0, Optional.of(ConnectionType.WIFI)));

        // Your offline code puts here.

}
// Your online code puts here, if any.

Note: Your Chrome Driver version should match the Selenium-Devtools version, for example

'org.seleniumhq.selenium:selenium-devtools-vNN:SELENIUM_VERSION'

vNN is your chrome version like selenium-devtools-v111:4.8.3 with Selenium 4.8.3 version.

jumperchen
  • 1,463
  • 12
  • 27