Protractor:启用网络限速的Chrome浏览器启动

6
Chrome DevTools 的网络限制功能在 ChromeDriver-2.26+ 中可用,参见 this issue。我该如何在我们的 Protractor 配置文件中指定此功能?
根据搜索结果,我已尝试在配置文件中添加了 networkConnectionEnabled 属性和一个 prefs 块到 chromeOptions 中,如下所示。(请注意,我没有同时执行这两个操作。)
multiCapabilities: [
    {
        'browserName': 'chrome',
        'platform': 'ANY',
        'networkConnectionEnabled': {'type': 'GPRS'},
        'chromeOptions': {
            args: [
                '--lang=en',
                '--window-size=1280,1024'
            ],
            prefs: {
                'net.throttling_enabled': 'true,50,20'
            }
        }
    }
],

我尝试了基于这里(第1983行)找到的第二个选项。但是,这些都没有改变protractor运行的行为,当我手动测试并设置节流时,会触发我的代码中的某个条件。
编辑:还尝试在chromeOptions下添加类似以下内容:mobileEmulation:{networkConnectionEnabled: true, networkThrottle: '2G'}

如果我没记错的话,当移动设备仿真被激活时,才能使用限流功能。 - Florent B.
@FlorentB。我尝试了类似的东西(请参见编辑),但仍然不确定我是否设置正确。 - eflat
网络限速现在已经从设备模式中分离出来,并且能够在任何时候运行。 - Garbee
1个回答

6

根据变更日志,Selenium 3.4.0增加了Chrome网络仿真的API。并且这些API在Protract 5.2.0或更新版本中可用。

可以通过以下方式配置网络条件:

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

这段代码应该放在protractor.conf.js文件中的onPrepare函数内。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接