简体   繁体   中英

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.

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();

At least in Chrome, I strongly believe that if you go incognito you wont to have to clean up your cookies. You can set your options like following (the :

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def _options():
    options = Options()
    options.add_argument('--ignore-certificate-errors')
    #options.add_argument("--test-type")
    options.add_argument("--headless")
    options.add_argument("--incognito")
    options.add_argument('--disable-gpu') if os.name == 'nt' else None # Windows workaround
    options.add_argument("--verbose")
    return options

and call like this:

with webdriver.Chrome(options=options) as driver:
    driver.implicitly_wait(conf["implicitly_wait"])
    driver.get(conf["url"])

The following code is based on @An Khang 's answers. and it is working properly on Chrome 78.

ChromeDriver chromeDriver = new ChromeDriver();

    chromeDriver.manage().deleteAllCookies();
    chromeDriver.get("chrome://settings/clearBrowserData");
    chromeDriver.findElementByXPath("//settings-ui").sendKeys(Keys.ENTER);

    return chromeDriver;
    WebDriver driver = new ChromeDriver();
    driver.manage().deleteAllCookies();
    driver.get("chrome://settings/clearBrowserData");
    driver.findElement(By.xpath("//settings-ui")).sendKeys(Keys.ENTER);
import org.openqa.selenium.Keys;

you need to import the Keys in newer version and change the last line to findElement by xpath

WebDriver driver = new ChromeDriver();

driver.manage().deleteAllCookies();
driver.get("chrome://settings/clearBrowserData");
driver.findElement(By.xpath("//settings-ui")).sendKeys(Keys.ENTER);

On Google chrome you can use this script:

 driver.get("chrome://settings/clearBrowserData"); JavascriptExecutor jse = (JavascriptExecutor)driver; WebElement clearData = (WebElement) jse.executeScript("return document.querySelector(\\"body > settings-ui\\").shadowRoot.querySelector(\\"#main\\").shadowRoot.querySelector(\\"settings-basic-page\\").shadowRoot.querySelector(\\"#basicPage > settings-section:nth-child(8) > settings-privacy-page\\").shadowRoot.querySelector(\\"settings-clear-browsing-data-dialog\\").shadowRoot.querySelector(\\"#clearBrowsingDataConfirm\\")"); ((JavascriptExecutor)driver).executeScript("arguments[0].click();", clearData);

what i found working for myself was adding chromium flag:

--disk-cache-size=0

not sure if it clears cache, but any cache related problems disappeared in my case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM