Skip to content

Commit 4453933

Browse files
asashourlukeis
authored andcommitted
HtmlUnitDriver.deleteAllCookies() fix
WebDriver.deleteAllCookies() deletes all the cookies for the current domain, however HtmlUnitDriver implementation deletes all cookies for all domains, which is incorrect. This was pointed out in https://sourceforge.net/p/htmlunit/bugs/1695/ Test case attached as well Signed-off-by: Luke Inman-Semerau <[email protected]>
1 parent 03b2798 commit 4453933

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Diff for: java/client/src/org/openqa/selenium/htmlunit/HtmlUnitDriver.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,13 @@ public void deleteCookie(Cookie cookie) {
15011501

15021502
@Override
15031503
public void deleteAllCookies() {
1504-
getWebClient().getCookieManager().clearCookies();
1504+
CookieManager cookieManager = getWebClient().getCookieManager();
1505+
1506+
URL url = getRawUrl();
1507+
Set<com.gargoylesoftware.htmlunit.util.Cookie> rawCookies = getWebClient().getCookies(url);
1508+
for (com.gargoylesoftware.htmlunit.util.Cookie cookie : rawCookies) {
1509+
cookieManager.removeCookie(cookie);
1510+
}
15051511
}
15061512

15071513
@Override

Diff for: java/client/test/org/openqa/selenium/CookieImplementationTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,35 @@ private void addCookieOnServerSide(Cookie cookie) {
572572
}
573573
driver.get(url.toString());
574574
}
575+
576+
@Test
577+
public void deleteAllCookies() throws Exception {
578+
Cookie cookie1 = new Cookie.Builder("fish1", "cod")
579+
.domain(appServer.getHostName()).build();
580+
Cookie cookie2 = new Cookie.Builder("fish2", "tune")
581+
.domain(appServer.getAlternateHostName()).build();
582+
583+
String url1 = domainHelper.getUrlForFirstValidHostname("/common");
584+
String url2 = domainHelper.getUrlForSecondValidHostname("/common");
585+
586+
WebDriver.Options options = driver.manage();
587+
588+
options.addCookie(cookie1);
589+
assertCookieIsPresentWithName(cookie1.getName());
590+
591+
driver.get(url2);
592+
options.addCookie(cookie2);
593+
assertCookieIsNotPresentWithName(cookie1.getName());
594+
assertCookieIsPresentWithName(cookie2.getName());
595+
596+
driver.get(url1);
597+
assertCookieIsPresentWithName(cookie1.getName());
598+
assertCookieIsNotPresentWithName(cookie2.getName());
599+
600+
options.deleteAllCookies();
601+
assertCookieIsNotPresentWithName(cookie1.getName());
602+
603+
driver.get(url2);
604+
assertCookieIsPresentWithName(cookie2.getName());
605+
}
575606
}

0 commit comments

Comments
 (0)