10

How to clear browser cache before every test run? I tried with driver.manage().deleteAllCookies(); in setUp method after creating the driver instance, it is working for firefox, but for IE no use. Is there any solution for IE please provide me..

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
user1441341
  • 495
  • 2
  • 9
  • 23

5 Answers5

13

There is a driver capability you can set as follows:

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

It worked for me on IE11.

Source: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/ie/InternetExplorerDriver.html

Flyview
  • 1,899
  • 1
  • 28
  • 46
  • If you are using protractor, this can be set on the capabilities hash with 'ie.ensureCleanSession': 'true' - see https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities – emery Jan 24 '18 at 21:28
3

Use the below code to clear cache in IE

try {
    Runtime.getRuntime().exec("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255");
} catch (IOException e) {
    e.printStackTrace();
}
aimbire
  • 3,697
  • 1
  • 15
  • 28
praneel
  • 1,842
  • 4
  • 19
  • 24
2

Using this post, How To: Execute command line in C#, get STD OUT results, I came up with this C# code to delete cookies (and as a side effect it deletes all IE browser data).

public static void DeleteIECookiesAndData()
{
    Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "RunDll32.exe";
    p.StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 2";
    p.Start();
    p.StandardOutput.ReadToEnd();
    p.WaitForExit();
}

It isn't pretty at all, but it works. Maybe some pieces could be removed. (please let me know if you find a better way to do this).

Community
  • 1
  • 1
craastad
  • 6,222
  • 5
  • 32
  • 46
2

IE Browser clears the cache for each element after every page load

ieCapabilities.setCapability(InternetExplorerDriver.ENABLE_ELEMENT_CACHE_CLEANUP, true);

This will do session clean up:

ieCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
vefthym
  • 7,422
  • 6
  • 32
  • 58
-1

Using java you can achieve that:

protected void deleteCookie(String cookieName) {    
    String cookieDomain = CTPropertiesManager.getProperty("site.properties", "site.cookie.domain");
    try {
        //get all cookies
        Cookie cookies[] = request.getCookies();
        Cookie ctCookie=null;
        if (cookies !=null) {
            for(int i=0; i<cookies.length; i++) {
                ctCookie=cookies[i];
                if (ctCookie.getName().trim().equals(cookieName)) {
                    if ( cookieDomain != null ) {
                        ctCookie.setDomain(cookieDomain);
                    }
                    ctCookie.setPath("/ct");
                    ctCookie.setMaxAge(0);
                    response.addCookie(ctCookie);    
                }
            } //end for
        }//end if cookie
    } catch(Exception e) {
        CTLogManager.log(e);
    }
}//end deleteCookie()

For deleting cache You can create one bat file which clear your browser or application cache before test start. after creating bat file just call in your code before test start.

aimbire
  • 3,697
  • 1
  • 15
  • 28
Rajesh
  • 7
  • 1