Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable cache in Selenium Chrome Driver

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

Option --disable-application-cache is deprecated now 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?

Thanks!

like image 855
zablotski Avatar asked May 01 '15 13:05

zablotski


People also ask

Can I turn off caching in Chrome?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

How do I disable cookies in selenium?

public void ClearBrowserCache() { webDriver. Manage(). Cookies. DeleteAllCookies(); //delete all cookies Thread.


1 Answers

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

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

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.

Example is from the Selenium Test for DevTools

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() -- Specifies if request was served from the disk cache.

For above code it will be false

You can refer the selenium repository for all the example tests devtools/ChromeDevToolsNetworkTest.java

For Dev Tools Maven Dependency

<!-- 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>
like image 200
Rahul L Avatar answered Oct 06 '22 01:10

Rahul L