Network throttling with chrome and selenium

alecxe picture alecxe · Jan 10, 2015 · Viewed 26.6k times · Source

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 emulate different network conditions:

Optimizing your site's performance under varying network conditions is a key aspect of developing for a mobile audience.

Device mode's network conditioning allows you to test your site on a variety of network connections, including Edge, 3G, and even offline. Select a connection from the preset dropdown to apply network throttling and latency manipulation.

For example, we can set it to be like in good old days - GPRS 50 Kbps:

enter image description here

Now we have a good use case for it - we have an internal application for network speed testing. And this new emulation functionality is very helpful for manual testing. But, we'd like to automate it.

Question is:

Is it possible to start chrome via selenium with specified network conditions? Is it something that can be controlled through chrome preferences or command-line arguments?


There are certainly multiple options to simulate slow internet connection, but the question is specifically about chrome+selenium.

Answer

Yaroslav Admin picture Yaroslav Admin · Oct 10, 2017

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