Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear browser cache in Selenium test

I am running my Selenium tests with WebDriver. I am repeating the tests with some loop so now I want to Clear the cache before starting new test in JAVA.

@Test
public void ffAndIe() throws InterruptedException {
    int i = 0;
    while(i < 5000){

        driver.get("http://dit-map.appspot.com/");
        Thread.sleep(15000);
        driver.get("http://dit- map.appspot.com/#home:zoom=7&lat=37.04&lng=25.05&display=weather");
        Thread.sleep(15000);
        driver.get("http://dit-map.appspot.com/#home:zoom=9&lat=37.55&lng=23.83&display=weather,wind");
        Thread.sleep(10000);
        driver.get("http://dit-map.appspot.com/#home:zoom=9&lat=37.84&lng=23.22&display=weather,wind,cloud");
        Thread.sleep(10000);
        driver.get("http://dit-map.appspot.com/?lang=en#home:zoom=10&lat=38.13&lng=22.59&display=weather,wind,meteogram");
        Thread.sleep(10000);
        i++;
    }
}

with in this while loop the first thing I want to do is to CLEAR my CACHE (IE, MOZILLA & CHROME)

any idea how can I achieve this?

Thanks

like image 384
user1226162 Avatar asked Mar 05 '12 06:03

user1226162


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.

What is clear command in Selenium?

clear( ) predefined method of Selenium 'WebDriver' Class is used to clear the text entered or displayed in the text fields. Now lets clear the text inside the Text Area field using the clear( ) predefined method of Selenium 'WebDriver' class.

How do I reset Selenium?

We can reset or clear an edit box in Selenium with the help of clear() method. Code Implementation with clear().

Which Selenium method is used to delete all cookies from the web page?

We can delete cookies on all domains with Selenium. The method deleteAllCookies is used to delete all cookies from the present domain.


1 Answers

Currently, there is no way to clear the cache through the web driver API. However, if you can start a new instance of the browser each time, the cache should be cleared in FF and Chrome because a new profile is created on each launch.

The comments for issue #40 (Clear cache) in the Selenium issue tracker list two potential solutions to your problem if creating a new browser instance isn't possible:

  1. Clear the IE cache from the command line
  2. Disable the FF cache using a custom profile

HTH

like image 152
Jonathan McIntire Avatar answered Oct 28 '22 04:10

Jonathan McIntire