Network throttling with chrome and selenium

感情迁移 提交于 2019-11-28 05:16:47
Yaroslav Admin

The API to control network emulation were added to ChromeDriver. And should be available for quite a while now. According to comment in the linked issue you should use version at least 2.26 because of some bugfix.

According to Selenium changelog bindings are available for these languages:

  • JavaScript as of version 3.4.0 (commit)
  • Python as of version 3.5.0 (commit)
  • Ruby as of version 3.11.0 (commit)
  • C# as of version 4 (commit)

If you need these binding in other languages you should probably open issue/contribute implementation similar to one of the above.

Example usage from Python is below:

driver.set_network_conditions(
    offline=False,
    latency=5,  # additional latency (ms)
    download_throughput=500 * 1024,  # maximal throughput
    upload_throughput=500 * 1024)  # maximal throughput

No, it is not possible to control Network Connectivity Emulation through Chrome preferences or command-line arguments. Network Connectivity Emulation is part of the build-in Chrome debugger. One way way in solving this is to control the debugger. This can be done via an extension or by directly controlling the debugger, see explanation. However, this will not work with WebDriver. The reason for this is that there can only be one "debug" session and WebDriver is already using it, see explanation. Since there is no public interface, there is also no way to control it via WebDriver.

For Device Mode & Mobile Emulation which is also part of the build-in debugger, there is a public interface (details), and thus can be controlled. This can be done through WebDriver Capabilities. Two options 1) Specify a device name 2) Enter your own parameters (limited).

Indeed C# Selenium latest (3.11) has NetworkConditions added. Now you can use it like this:

     var driver = new ChromeDriver(pathToDriver);
     driver.NetworkConditions = new ChromeNetworkConditions()
     {  DownloadThroughput = 5000, UploadThroughput = 5000, Latency = TimeSpan.FromMilliseconds(5) };

The problem is it's not yet usable because of the bug

https://github.com/SeleniumHQ/selenium/issues/5693

So .Net guys will have to wait until 3.12 Selenium Release.

symcbean

While this is a very welcome and useful bit of functionality, for serious testing I think the conventional methods of network simulation are still the way to go.

I am aware of 2 solutions in addition to those already linked - the Charles web proxy (very useful tool - commercial) and implementing your own recipe using Linux Traffic Control (e.g. see chapter 6 of LAMPe2e).

By interfering with the network connections rather than the browser, you then get a proper measure of the impact independently of the browser in use.

Why do you just want to use the Chrome functionality?

akash

You can use this method to run your test case in specified network conditions

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)))
  );
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!