I am working on Selenium WebDriver automation in java programming language. In my test suite that initiates the browser window once and perform all the tests. I want to clear the browser cache before running some tests without restarting the browser. Is there any command/function, that can achieve the purpose? Thanks.
Selenium does not provide a way to delete cache, here are some workarounds. There are a few ways in which cache can be deleted using Selenium Webdriver for regular Chrome browsers. For instance, here they explain how selenium can be used to clear cache like a real user i.e., by navigating to chrome settings.
Navigate to the chrome settings page with Selenium by executing the driver. get('chrome://settings/clearBrowserData') . Click on the Clear Data button to clear the cache.
We can enter text on any field in Selenium. After entering the text, we may need to remove or clear the text we entered in that field. This interaction with the web elements can be achieved with the help of clear() method. Thus a clear() method is used to clear or reset an input field belonging to a form/ edit box.
This is what I use in Python:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('chrome://settings/clearBrowserData')
driver.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER)
You can try converting these into Java. Hope this will help! :)
For IE
DesiredCapabilities ieCap = DesiredCapabilities.internetExplorer();
ieCap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
For Chrome:
https://code.google.com/p/chromedriver/issues/detail?id=583
To delete cookies:
driver.manage().deleteAllCookies();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With