禁用Selenium Chrome驱动程序中的缓存 [英] Disable cache in Selenium Chrome Driver

查看:1332
本文介绍了禁用Selenium Chrome驱动程序中的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium ChromeDriver来衡量网页的性能.但默认情况下,Chrome驱动程序缓存处于启用状态.

I’m using the Selenium ChromeDriver in order to measure performance of web pages. But by default in Chrome driver cache is enabled.

选项--disable-application-cache现在已弃用 https://code.google .com/p/chromium/issues/detail?id = 447206

我每次都可以初始化一个新的驱动程序实例,但这不是很方便.

Also I can initialize a new instanсe of driver each time, but it is not very convenient.

我的问题是有办法正确禁用缓存吗?

My question is there a way for properly disable cache?

谢谢!

推荐答案

在Chrome Dev工具的网络"标签中,我们可以通过单击禁用缓存"复选框来禁用缓存. 参考

In Chrome Dev tools Network tab we can disable the cache by clicking on 'Disable Cache' checkbox. refer

可以使用Selenium 4中的 Chrome DevTools协议支持来复制相同的行为.

Same behavior can be replicated using the Chrome DevTools Protocol support in the Selenium 4.

我们可以使用'Network.setCacheDisabled' Chrome DevTools协议

We can use 'Network.setCacheDisabled' from Chrome DevTools Protocol

Toggles ignoring cache for each request. If true, cache will not be used.
parameters

cacheDisabled
    boolean

    Cache disabled state.

示例摘自DevTools的Selenium测试

import org.openqa.selenium.devtools.network.Network;

 @Test
  public void verifyCacheDisabledAndClearCache() {

    ChromeDriver driver = new ChromeDriver();
    DevTools devTools = driver.getDevTools();
    devTools.createSession();
   

    devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.of(100000000)));

    driver.get("http://www.google.com");

    devTools.send(Network.setCacheDisabled(true));

    devTools.addListener(Network.responseReceived(), responseReceived -> assertEquals(false, responseReceived.getResponse().getFromDiskCache()));

   driver.get("http://www.google.com");

    devTools.send(Network.clearBrowserCache());

  }

getFromDiskCache()-指定是否从磁盘缓存中满足请求.

getFromDiskCache() -- Specifies if request was served from the disk cache.

对于上述代码,它将为false

For above code it will be false

对于所有示例测试 用于开发工具Maven依赖项

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-devtools -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-devtools</artifactId>
    <version>4.0.0-alpha-6</version>
</dependency>

这篇关于禁用Selenium Chrome驱动程序中的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆