Selenium和Service Worker离线模式

时间:2020-10-08 07:54:25

标签: python selenium google-chrome selenium-chromedriver google-chrome-devtools

我在Chrome上使用网络限制功能进行了测试,以关闭互联网连接。我正在测试SPA。 从85版本的Chrome开始,这些测试由于连接已打开而失败。 有人知道此损坏功能的解决方法吗?

使用以下代码关闭连接:

self.driver.set_network_conditions(
                latency=0,
                offline=True,
                download_throughput=500 * 1024,
                upload_throughput=500 * 1024)

UPD:我发现当我注销与chrome开发工具的连接丢失的服务人员时。

因此,我的SPA使用Service Worker作为请求的代理。这就是为什么set_network_conditions()不会关闭连接的原因。但是,对于所有其他URL,都没有连接。 I found my issue here.

1 个答案:

答案 0 :(得分:0)

也许使用代理?

PROXY = "12.345.678.910:8080" -- Fake address
chrome_options = WebDriverWait.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)

您只需要设置一个不存在的代理地址

//编辑 您已经提到必须模拟测试中间的连接丢失。我认为,代理仍然是唯一的解决方案。步骤:

1. Init and run local proxy server in your test
2. Run WebDriver with the reference to running proxy server created in step 1.
3. Perform online steps
4. Stop ProxyServer
5. Perform offline steps

有关代理的更多信息:https://wonderproxy.com/blog/a-step-by-step-guide-to-setting-up-a-proxy-in-selenium/

相关问题