Protractor: launch chrome with network throttling

2019-02-14 21:09发布

问题:

The network throttling feature from Chrome DevTools is available in ChromeDriver-2.26+ according to this issue. How can I specify this in our protractor config file?

Based on searching around, I've tried variations of a couple things in the config file. I've added a networkConnectionEnabled property and a prefs block to chromeOptions, as below. (Note that I didn't do them both at the same time.)

multiCapabilities: [
    {
        'browserName': 'chrome',
        'platform': 'ANY',
        'networkConnectionEnabled': {'type': 'GPRS'},
        'chromeOptions': {
            args: [
                '--lang=en',
                '--window-size=1280,1024'
            ],
            prefs: {
                'net.throttling_enabled': 'true,50,20'
            }
        }
    }
],

The second option I tried based on what I found here (line 1983). None of these change the behavior of the protractor run, which when I manually test and set the throttling triggers a certain condition in my code.

Edit: also tried adding something like this underchromeOptions: mobileEmulation: {networkConnectionEnabled: true, networkThrottle: '2G'}

回答1:

According to changelog APIs for manipulating network emulation in Chrome were added in Selenium 3.4.0. And they are available in Protract 5.2.0 or newer.

Network conditions can be configured like below:

browser.driver.setNetworkConditions({
    offline: false,
    latency: 5, // Additional latency (ms).
    download_throughput: 500 * 1024, // Maximal aggregated download throughput.
    upload_throughput: 500 * 1024 // Maximal aggregated upload throughput.
});

This code should be placed in protractor.conf.js inside onPrepare function.