Reputation: 447
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!
Upvotes: 14
Views: 31152
Reputation: 434
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/
Upvotes: 3
Reputation: 4349
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>
Upvotes: 5
Reputation: 193308
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.
Disable cache
The option to Disable cache
of google-chrome can be accessed from the Network tab within google-chrome-devtools.
Manually you can use either of the following Keyboard shortcuts for opening DevTools:
You can find a relevant discussion in Opening inspect (pressing F12) on Chrome via Selenium
With the availability selenium4 the support for ChromeDevtoolsProtocol(CDP) is now available through the DevTools
interface.
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));
Here is a demonstration on the usage of setCacheDisabled(true)
:
Environment details:
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
Upvotes: 0
Reputation: 1
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.
Upvotes: -3