Selenium和Service Workers离线模式



我测试过在chrome上使用网络节流来关闭互联网连接。我正在测试SPA。从85版本的Chrome开始,由于打开了连接,这些测试都失败了。有人知道这个坏功能的解决方法吗?

使用以下代码关闭连接:

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

UPD:当我用chrome dev工具注销服务工作者时,我发现连接丢失了。

因此,我的SPA使用服务工作者作为请求的代理。这就是为什么set_network_conditions((不会关闭连接的原因。但是,对于所有其他url,都没有连接。我在这里找到了我的问题。

也许使用了代理?

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/

最新更新