How to clear browser cache automatically in Selenium WebDriver?

How to clear browser cache before each run? I tried with driver.manage().deleteAllCookies(); in setUp after creating the driver instance, it works for firefox, but is not used for IE. Is there any solution for IE, please provide me ..

+8
internet-explorer-8 junit4 selenium-webdriver
source share
5 answers

There is a driver feature that you can install as follows:

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

This worked for me in IE11.

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

+11
source share

Use the code below to clear the cache in IE

 try { Runtime.getRuntime().exec("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255"); } catch (IOException e) { e.printStackTrace(); } 
+3
source share

Using this post, How to: execute a command line in C #, get the results of STD OUT , I came up with this C # code to delete cookies (and as a side, the effect 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(); } 

This is not very good, but it works. Some parts may be removed. (please let me know if you find a better way to do this).

+1
source share

IE Browser clears cache for each item after each page load

 ieCapabilities.setCapability(InternetExplorerDriver.ENABLE_ELEMENT_CACHE_CLEANUP, true); 

This will do a session cleanup:

 ieCapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true); 
+1
source share

Using java, you can achieve this:

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

To remove the cache, you can create one bat file that will clear your browser or application cache before starting the test. after creating the bat file, just call the code before starting the test.

0
source share

All Articles