Disable cache in Selenium Chrome Driver Disable cache in Selenium Chrome Driver selenium selenium

Disable cache in Selenium Chrome Driver


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.parameterscacheDisabled    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>


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/


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