Network throttling with chrome and selenium

后端 未结 7 1812
生来不讨喜
生来不讨喜 2020-12-08 09:40

Google Chrome 38 introduced the new \"Device Mode & Mobile Emulation\" functionality in devtools. In addition to choosing a device for emulation, it is also possible to

相关标签:
7条回答
  • 2020-12-08 10:13

    It looks like it's coming soon to Selenium (C#). The commit was on 01/28/2018:

    https://github.com/SeleniumHQ/selenium/blob/ef156067a583fe84b66ec338d969aeff6504595d/dotnet/src/webdriver/Chrome/ChromeNetworkConditions.cs

    0 讨论(0)
  • 2020-12-08 10:16

    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.

    0 讨论(0)
  • 2020-12-08 10:18

    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?

    0 讨论(0)
  • 2020-12-08 10:19

    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).

    0 讨论(0)
  • 2020-12-08 10:24

    I know this is an old question, but I recently had to solve for this problem and this page came up at the top of my Google search. Here are the main bits from how I did it in C#. Hope this helps someone in the future.

    var networkConditions = new ChromeNetworkConditions();
    networkConditions.Latency = new TimeSpan(150);
    networkConditions.IsOffline = false;
    networkConditions.DownloadThroughput = 120 * 1024;
    networkConditions.UploadThroughput = 150 * 1024;
    Driver.NetworkConditions = networkConditions;
    
    0 讨论(0)
  • 2020-12-08 10:30

    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)))
      );
    }
    
    0 讨论(0)
提交回复
热议问题