Comment #22 on issue 5212 by
vincebow...@gmail.com: Safari driver - clear
This is a java method I've successfully used to expire each cookie one by
one; I call it from the @Before method to reset the browser, and it works
even before any page has been loaded.
public void deleteAllCookiesOneByOne() {
int noOfCookies = driver.manage().getCookies().size();
if (noOfCookies > 0) {
System.out.println("Number of cookies found: " +
Integer.toString(noOfCookies));
}
Set<Cookie> cookies = driver.manage().getCookies();
for (Cookie cookie: cookies) {
System.out.println("Cookie found with name: " +
cookie.getName() + " and path: " + cookie.getPath() + " and domain: " +
cookie.getDomain());
String javascriptCall = "document.cookie = \"" +
cookie.getName() + "=; path=" + cookie.getPath() + "; expires=Thu,
01-Jan-1970 00:00:01 GMT;\"";
System.out.println("Attempting to expire the cookie with the
following script: " + javascriptCall);
((JavascriptExecutor)driver).executeScript(javascriptCall);
}
System.out.println("Number of cookies is now: " +
Integer.toString(driver.manage().getCookies().size()));
}
My console output shows it doing the job:
Number of cookies found: 1
Cookie found with name: .ASPXAUTH and path: / and domain: null
Attempting to expire the cookie with the following script:
document.cookie = ".ASPXAUTH=; path=/; expires=Thu, 01-Jan-1970 00:00:01
GMT;"
Number of cookies is now: 0
This was inspired by, and adapted from johan's ruby code in #21, around the
structure suggested by nhu in #14.