Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear cache before running some Selenium WebDriver tests using Java

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.

like image 206
Uziii Avatar asked Oct 06 '15 13:10

Uziii


People also ask

Does Selenium clear cache?

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.

How we can clear the cookie in Selenium?

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.

How do you clear the test in Selenium?

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.


2 Answers

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! :)

like image 113
An Khang Avatar answered Oct 10 '22 01:10

An Khang


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();
like image 25
vins Avatar answered Oct 10 '22 00:10

vins