Disable cache in Selenium Chrome Driver
Asked Answered
M

4

14

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!

Methylnaphthalene answered 1/5, 2015 at 13:56 Comment(1)
I had seen this to work in the case of IE, you should find similar capability for Chrome. capabilities={'ie.ensureCleanSession': True}. Using this, you always ensured you ran a clean instance of the browser.Brehm
L
5

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>
Lay answered 8/7, 2020 at 11:48 Comment(0)
C
3

As you mentioned, the option --disable-application-cache is deprecated now.

You can use --disk-cache-size and set it to zero. This will force the maximum disk space to be used by the disk cache to be zero. Although it may not work on some of the older browsers. I successfully tested it for firefox v67.0.1

Refer this command list for more details: https://peter.sh/experiments/chromium-command-line-switches/

Caecum answered 15/7, 2020 at 2:6 Comment(0)
W
0

Disable Cache in Google Chrome

You noted it right. There have been several discussions regarding the flag --disable-application-cache in the following discussions:

Both the items seems to blocked on certain other dependencies.


Manually accessing Disable cache

The option to Disable cache of can be accessed from the Network tab within .

Disable cache

Keyboard shortcuts for opening DevTools

Manually you can use either of the following Keyboard shortcuts for opening DevTools:

Keyboard shortcuts for opening DevTools

You can find a relevant discussion in Opening inspect (pressing F12) on Chrome via Selenium


ChromeDevtoolsProtocol support through Selenium

With the availability the support for ChromeDevtoolsProtocol(CDP) is now available through the DevTools interface.

  • v4.0.0.0-alpha-1: Basic support for CDP landed via the "DevTools" interface.
  • v4.0.0.0-alpha-2: Extra domains added for CDP: Network, Performance, Security, Target
  • v4.0.0-alpha-3: Add ApplicationCache, Fetch, Network, Performance, Profiler, ResourceTiming, Security and Target CDP domains.
  • v4.0.0-alpha-4: Chrome Debugging Protocol commands now mirror latest CDP spec.
  • v4.0.0-alpha-6: Ability to proxy CDP commands from the local end through the fully-distributed Grid. The end point to connect to is reported via "se:options -> cdp"

Network.setCacheDisabled

The Network.setCacheDisabled can be used to toggle ignoring cache for each request. If true, cache will not be used.

  • Usage:

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

Demonstration

Here is a demonstration on the usage of setCacheDisabled(true):

Environment details:

  • Selenium v4.0.0-alpha-6
  • ChromeDriver 83.0.4103.39 (2020-05-05)
  • Google Chrome Version 83.0.4103.116
  • TestNG

Code Block:

import java.util.Collections;
import java.util.Optional;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.network.Network;
import org.testng.Assert;
import org.testng.annotations.Test;

public class testngBasic {

    @Test
    public void foo() {
        System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        options.setExperimentalOption("useAutomationExtension", false);
        ChromeDriver driver = new ChromeDriver(options);
        DevTools devTools = driver.getDevTools();
        devTools.createSession();
        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.of(100000000)));
        devTools.send(Network.setCacheDisabled(true));
        devTools.addListener(Network.responseReceived(), responseReceived -> Assert.assertEquals(false, responseReceived.getResponse().getFromDiskCache()));
        driver.get("https://www.google.com/");
        devTools.send(Network.clearBrowserCache());
  }
}

Conclusion: Asserting responseReceived.getResponse().getFromDiskCache() as false establishes that cache was disabled

Weintrob answered 9/7, 2020 at 13:45 Comment(0)
H
-3

I was facing a similar, used driver.manage().deleteAllCookies(); before navigating to the page whose performance needs to be checked. This way we get a fresh instance of the browser every time we load a page. This works for Firefox as well as Chrome driver without any issues.

Hope it helps.

Hudis answered 16/2, 2016 at 9:49 Comment(1)
Cache can include more than just cookies. This doesn't answer the question and is a bad work around.Mazur

© 2022 - 2024 — McMap. All rights reserved.