Skip to content

Instantly share code, notes, and snippets.

@gwpantazes
Last active April 14, 2021 13:47
Show Gist options
  • Save gwpantazes/3bc7af273bd80695383c40c90c3938d7 to your computer and use it in GitHub Desktop.
Save gwpantazes/3bc7af273bd80695383c40c90c3938d7 to your computer and use it in GitHub Desktop.
SafariDriver can't clear cookies

SOLUTION/WORKAROUND: See SafariBugWorkaround.java in this gist for a solution to the problem. Feel free to test it yourself by running TestDriverCookieDeletion.java with the substitute method.


I've filed the following bug at Apple Feedback Assistant: https://feedbackassistant.apple.com/feedback/6935056

SafariDriver WebDriver deleteAllCookies API does not delete cookies

SafariDriver does not delete cookies when the WebDriver driver.manage().deleteAllCookies() gets called.

Specifically, in the Selenium Java client the method call would be: driver.manage().deleteAllCookies(); // WebDriver.Options::deleteAllCookies;

For a short, self contained, correct (compilable) example, see this Github Gist: https://gist.github.com/gwpantazes/3bc7af273bd80695383c40c90c3938d7 . The example compares behavior of Chrome, Firefox, and Safari. Safari fails to delete the cookies, while the other browsers completely delete the cookie list as expected.

Steps to reproduce: (The following steps are done through scripting in order to hit SafariDriver)

  • Start a SafariDriver instance
  • Navigate to https://www.apple.com
  • Check all current cookies (notice there are some cookies)
  • Use the WebDriver to delete all cookies (driver.manage().deleteAllCookies() in Java Selenium Client)
  • Check all current cookies again. Observe that cookies are still there; the cookies were not deleted.

Expected Result: The cookies would all be deleted.

Actual Result: All the cookies are still there.

Related issues elsewhere:

Environment

  • MacOS Mojave 10.14.5 (18F132)
  • Safari Version 12.1.1 (14607.2.6.1.1) (and corresponding SafariDriver)
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;
class SafariBugWorkaround {
/**
* Delete all the cookies for the current domain.
*
* <p>Should be used in-place of {@link WebDriver.Options#deleteAllCookies()} until <a
* href="">this {@code SafariDriver} bug</a> is fixed.
*
* @param driver the WebDriver
*
* @apiNote Wrapper around {@link SafariDriver.Options#deleteAllCookies()} bug workaround.
* For info on the bug, see
* <a href="https://gist.github.com/gwpantazes/3bc7af273bd80695383c40c90c3938d7">this
* gist</a>.
*/
public static void deleteAllCookiesWithSafariWorkaround(final WebDriver driver) {
if (driver instanceof SafariDriver) {
driver.manage().getCookies().forEach(cookie -> driver.manage().deleteCookie(cookie));
} else {
driver.manage().deleteAllCookies();
}
}
}
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
class TestDriverCookieDeletion {
public static void main(String[] args) {
final String url = "https://www.google.com";
System.out.println("Printing cookie diagnostics for " + url);
System.out.println();
WebDriverManager.chromedriver().setup();
printCookies(new ChromeDriver(), url);
WebDriverManager.firefoxdriver().setup();
printCookies(new FirefoxDriver(), url);
printCookies(new SafariDriver(), url);
}
private static void printCookies(final WebDriver driver, final String url) {
System.out.println();
System.out.println("Printing before and after cookies for " + driver);
driver.get(url);
System.out.printf("%50s", "Cookies upon page load, before cookie deletion: ");
System.out.println(driver.manage().getCookies());
driver.manage().deleteAllCookies();
System.out.printf("%50s", "Cookies after deleting cookies: ");
System.out.println(driver.manage().getCookies());
driver.quit();
System.out.println();
}
}
Printing cookie diagnostics for https://www.google.com
2019-08-05 14:21:19 INFO WebDriverManager:575 - Using chromedriver 75.0.3770.8 (since Google Chrome 75 is installed in your machine)
2019-08-05 14:21:19 INFO WebDriverManager:1050 - Exporting webdriver.chrome.driver as /Users/georgep/.m2/repository/webdriver/chromedriver/mac64/75.0.3770.8/chromedriver
Starting ChromeDriver 75.0.3770.8 (681f24ea911fe754973dda2fdc6d2a2e159dd300-refs/branch-heads/3770@{#40}) on port 21830
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Aug 05, 2019 2:21:20 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Printing before and after cookies for ChromeDriver: chrome on MAC (c7e4ed0168385d45a6dd47f21a6d23f6)
Cookies upon page load, before cookie deletion: [NID=188=Fr6MosF_rXRjH8wCTNAVY7UU83zVJ-iQLVFdTyE6-MpFDOTGZnZgVM39TzoyEYrTb9exOvCO3CAGhme0Zv-eZoCEemExR3O9uqExeFjAkVnTxbG8Xg9Vrs5M9Fw4MN4r-_n4xsGFwRDc27RBGQbFrpInBhm5QgPEh6g7eno6ZHQ; expires=Tue, 04 Feb 2020 01:21:20 CST; path=/; domain=google.com, 1P_JAR=2019-08-05-19; expires=Wed, 04 Sep 2019 02:21:20 CDT; path=/; domain=google.com]
Cookies after deleting cookies: []
2019-08-05 14:21:21 INFO WebDriverManager:575 - Using geckodriver 0.24.0 (since Mozilla Firefox 68 is installed in your machine)
2019-08-05 14:21:21 INFO WebDriverManager:1050 - Exporting webdriver.gecko.driver as /Users/georgep/.m2/repository/webdriver/geckodriver/macos/0.24.0/geckodriver
1565032881334 mozrunner::runner INFO Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-foreground" "-no-remote" "-profile" "/var/folders/sv/5gh4tk990539nzhdt311pg0h000_fp/T/rust_mozprofile.yh3ftt7VB2MN"
1565032881772 [email protected] WARN Loading extension '[email protected]': Reading manifest: Invalid extension permission: mozillaAddons
1565032881772 [email protected] WARN Loading extension '[email protected]': Reading manifest: Invalid extension permission: telemetry
1565032881772 [email protected] WARN Loading extension '[email protected]': Reading manifest: Invalid extension permission: resource://pdf.js/
1565032881772 [email protected] WARN Loading extension '[email protected]': Reading manifest: Invalid extension permission: about:reader*
1565032883205 Marionette INFO Listening on port 60664
1565032883286 Marionette WARN TLS certificate errors will be ignored for this session
Aug 05, 2019 2:21:23 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Printing before and after cookies for FirefoxDriver: firefox on MAC (4fa0590e-b2fb-9641-a3fa-5f4698205c05)
Cookies upon page load, before cookie deletion: [NID=188=WZ5lji4_fc7dcrVXKV-i8ej-Hx0Hp3NlGtHlGEOybFQ16aUouUO6T34-6gy-mdH1odceozRv6RX7Eg1EOOxR0sjQYV0aO9cibHSPC3ghwcuL1GRJH-APjXezOMpNs1LKXVElr5hC0LC23qn9z5w8rr5d_k5qJBTSDFVwcZC9C7I; expires=Tue, 04 Feb 2020 01:21:23 CST; path=/; domain=.google.com, 1P_JAR=2019-8-5-19; expires=Wed, 04 Sep 2019 02:21:24 CDT; path=/; domain=.google.com]
Cookies after deleting cookies: []
1565032884470 Marionette INFO Stopped listening on port 60664
Aug 05, 2019 2:21:26 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Printing before and after cookies for SafariDriver: Safari on MAC (AB6EBF39-8FB4-444E-9365-D83B49CB8527)
Cookies upon page load, before cookie deletion: [NID=188=RaeXbFJpWfTsW2QQwnu9JXbKRjCyB5aU6Lvy0MTGI42rHZPUmmmX5whWGiIMLS_OFGYFzddaqN5NFxXow_4AvsoxZh_f6PbOGCf_75gD5X40HtxR0qqhHLVS37pYm0pV_HhXtL_25us2_Nld1-nbigG4R9n72NN2cON9rbSQAQI; expires=Tue, 04 Feb 2020 01:21:26 CST; path=/; domain=.google.com, 1P_JAR=2019-08-05-19; expires=Wed, 04 Sep 2019 02:21:26 CDT; path=/; domain=.google.com]
Cookies after deleting cookies: [NID=188=RaeXbFJpWfTsW2QQwnu9JXbKRjCyB5aU6Lvy0MTGI42rHZPUmmmX5whWGiIMLS_OFGYFzddaqN5NFxXow_4AvsoxZh_f6PbOGCf_75gD5X40HtxR0qqhHLVS37pYm0pV_HhXtL_25us2_Nld1-nbigG4R9n72NN2cON9rbSQAQI; expires=Tue, 04 Feb 2020 01:21:26 CST; path=/; domain=.google.com, 1P_JAR=2019-08-05-19; expires=Wed, 04 Sep 2019 02:21:26 CDT; path=/; domain=.google.com]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment