Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Safari driver - clear cookies not working #5212

Closed
lukeis opened this issue Mar 4, 2016 · 22 comments
Closed

Safari driver - clear cookies not working #5212

lukeis opened this issue Mar 4, 2016 · 22 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 4, 2016

Originally reported on Google Code with ID 5212

Clear cookies doesn't work on safariDriver

What steps will reproduce the problem?
1.Do a login to any web site
2.Call "driver.manage().deleteAllCookies()"
3.navigate to one page that requieres the login cookie

What is the expected output? What do you see instead?
The page should not keep the cookie and the user should not be loggein

Selenium version: 2.30.0
OS: mac 10.8.2
Browser: safari 
Browser version: 6.02

Reported by turturiellomartino on 2013-02-21 16:01:52

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Reported by barancev on 2013-02-24 21:00:29

  • Labels added: Component-WebDriver, Browser-Safari

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

I am also facing the same problem. Unable to clear the cookies through selenium web
driver...

Reported by blazergreat7 on 2013-03-21 10:28:53

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

I am also having the same problem. I need this because of how Safari is setup. The weird
part here is that getCookies(_) works and returns 17 to the Set but the delete method
makes no change. 

Selenium Version: 2.31.0
OS X, 10.8.3 
Browser: 6.0.3

Repro steps are the same as above. Seems like this would be a higher priority. Its
impossible to run multiple tests if you have to manually reset Safari between each
test. 

Reported by jackwestis on 2013-04-08 23:43:44

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

I think there is a workaround to this - you're welcome to try it. But first, so this
issue only affects Safari and works fine for all other browsers?

Bear in mind that deleteAllCookies() may still behave same as the Selenium RC version:
deleteAllVisibleCookies(). So you have to be sure you can see those cookies before
you delete them (can check via Selenium get cookie calls or using javascript "document.cookie"
in browser javascript/developer console. if get cookie back then it is visible, else
not).

The workaround is to go to page where the cookie is checked/invoked (e.g. at the login
/ my account page that checks if logged in via cookie and either shows page or redirects
back to login page based on cookie check. Then forcefully delete that cookie via deleteCookie()
or use deleteAllCookies(). Then do a forced page refresh. If you were on the my accounts
page (already logged in), deleting cookie followed by page refresh should then take
you back to login page since your cookie not exist anymore. Use this workaround as
a test "precondition" to execute before proceeding with test flow that is expecting
cookie not to exist. So to reiterate the workaround: follow the reproduce steps of
this bug but swap steps 3 & 2, then add step 4 - page refresh. Now can proceed with
test as if no bug occurred.

I've ran into this issue for Safari and IE (since both don't offer a "clean" profile,
and deleteAllCookies() is not functioning good enough) and that was my workaround which
has worked well for me.

Reported by mangaroo on 2013-05-08 01:32:12

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Forgot to mention, the workaround has been working for my team where we do run Safari
with multiple instances in parallel on same machine as well as multiple tests being
run one after another. But of course we use Safari 5 on Mac with Selenium v2.28, but
that might not make much of a difference from the reported environment for this bug.

Reported by mangaroo on 2013-05-08 01:34:38

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

The SafariDriver doesn't have access to the browser's cookie jar, so it has to delete
cookies through javascript (i.e. manipulating document.cookie). This means any HttpOnly
cookie, like many login cookies, cannot be modified by the driver.

For deleteAllCookies, we can probably just nuke the cookie file on disk (although that
may require a browser restart, which would be a non-starter for us)

Reported by jmleyba on 2013-05-19 17:08:07

  • Labels removed: Status-Untriaged

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

for IE you can set the below capability as true:
ie.ensureCleanSession

for Safari:
safari.cleanSession as true

I tested in IE, it works.
For safari it does not.

http://code.google.com/p/selenium/wiki/DesiredCapabilities

Reported by nagarazang on 2013-08-27 12:30:40

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

I did face this issue and resolved it by starting with a clean session. 

This option essentially removes cookies, cache, local storage etc.

DesiredCapabilities desiredCapabilities = DesiredCapabilities.safari();
SafariOptions safariOptions = new SafariOptions();
safariOptions.setUseCleanSession(true);
desiredCapabilities.setCapability(SafariOptions.CAPABILITY, safariOptions);

Reported by jkandula on 2014-02-01 22:51:00

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Hi, Are you sure that you have resolved the issue by starting with a clean session?

I tried using the desired capability mentioned below:
DesiredCapabilities desiredCapabilities = DesiredCapabilities.safari();
SafariOptions safariOptions = new SafariOptions();
safariOptions.setUseCleanSession(true);
desiredCapabilities.setCapability(SafariOptions.CAPABILITY, safariOptions);

But, unfortunately, it couldn't clear cookies.

Reported by santu62 on 2014-02-26 01:01:07

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Reported by jmleyba on 2014-02-26 04:50:16

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Same here - running on safari 7.0.2 (MacOS), selenium server 2.32 (can't use anything
higher because it's broken totally due to #7091).

Cookies are not cleared, session cookies persist, I was able to confirm that using
developer console - resources tab.

Reported by abodera on 2014-03-13 16:35:02

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

I just tried this out and it works
public void deleteCookies() {
        Set<Cookie> cookies = driver.manage().getCookies();
        driver.manage().deleteAllCookies();
        if (!cookies.isEmpty()) {
            System.out.println(cookies.size());
            System.out.println(cookies.toString());
            Iterator<Cookie> iter= driver.manage().getCookies().iterator();
            while(iter.hasNext()){
                Cookie C = iter.next();
                System.out.println(C.getName()+"\n" + C.getPath()+"\n"+ C.getDomain()+"\n"+C.getValue()+"\n"+C.getExpiry());
            }
            cookies.clear();
            System.out.println(cookies.size());
            System.out.println(cookies.toString());
        }
    }

Reported by nhu.tran2011 on 2014-04-17 07:54:18

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Above method deleteCookies() doesn't work for me. Any other suggestions?

Reported by taylortai1031 on 2015-01-20 01:57:22

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Same problem here. deleteAllCookies() does not clear (at least session-) cookies.
driver.manage().getCookies() does not return any results.

We call this in our "@Before" test-method, so that all single tests have a clean system
(no login). But on Safari the login persists and tests fail.

Reported by michael.lorenz.persis on 2015-02-17 08:02:26

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Selenium 2.44
WebDriver 2.38
Safari 8.0.3
OS X 10.10.2

Reported by michael.lorenz.persis on 2015-02-17 08:05:32

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

On comment 17, have you tried navigating to the page/site where the session cookies
would take effect and then get cookies there and delete them there? Can make that as
part of @Before. It may not work if you just blindly call deleteAllCookies() or getCookies()
w/o being on the actual site/page that uses the cookies. It's not the same as how a
browser works for cookie management where you can be on any page to manage cookie store/cache.
I mentioned that in an earlier comment.

Reported by mangaroo on 2015-02-17 23:50:27

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

I too am having this problem:

Env:
OSX 10.10.2
Selenium 2.45
Ruby 2.0.0-p643
Capybara 2.4.4

Error:
 Timed out awaiting response to command "deleteAllCookies" after 30007 ms (Selenium::WebDriver::Error::ScriptTimeOutError)
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/selenium-webdriver-2.45.0/lib/selenium/webdriver/safari/bridge.rb:73:in
`raw_execute'
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/selenium-webdriver-2.45.0/lib/selenium/webdriver/remote/bridge.rb:618:in
`execute'
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/selenium-webdriver-2.45.0/lib/selenium/webdriver/remote/bridge.rb:367:in
`deleteAllCookies'
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/selenium-webdriver-2.45.0/lib/selenium/webdriver/common/options.rb:67:in
`delete_all_cookies'
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara/selenium/driver.rb:95:in
`reset!'
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara/session.rb:103:in
`reset!'
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara.rb:257:in
`block in reset_sessions!'
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara.rb:257:in
`each'
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara.rb:257:in
`reset_sessions!'
      /Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara/cucumber.rb:8:in
`After'

driver setup:

safari_opts = Selenium::WebDriver::Safari::Options.new
          safari_opts.skip_extension_installation = true
          # convert to capabilites
          safari_caps = safari_opts.to_capabilities
          # adjust clean session flag from nil to true
          safari_caps['safari.options']['cleanSession'] = true
          Capybara::Selenium::Driver.new(app, browser: suffix, desired_capabilities:
safari_caps)


Unfortunately the site i'm testing on is internal, but i'm confident that this problem
would exist on any site where login information is saved in a cookie.

Reported by Brian.Michael.Carlson on 2015-04-08 14:41:15

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

I've successfully used this method to expire login cookies rather than deleting them:
    domain = "." + $base_url.gsub("https://", "")
    cookie = "document.cookie = 'svssid=; domain=#{domain}; path=/; expires=Thu, 01
Jan 1970 00:00:01 GMT;'"
    execute_script(cookie)

Reported by johan.djuplunda on 2015-06-04 13:11:33

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

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.

Reported by vincebowdren on 2015-07-09 14:46:45

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Hi Everyone, 

Finally I was able to find a workaround for Safari clean session issue. I execute the
following lines of code when ever I need clean session. 

String[] args = {"/usr/bin/osascript", "/Users/automation/Documents/ClearCacheScript3.scpt"};


            try {
                Process process = Runtime.getRuntime().exec(args);
                process.waitFor();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Here ClearCacheScript3.scpt is the applescript used to clear the browser data. Following
is the scripts:

tell application "Safari" to activate
delay 2
tell application "System Events" to tell process "Safari"
    keystroke "," using command down
    tell window 1
        delay 3
        click button "Privacy" of toolbar 1
        delay 3
        click button 2 of group 1 of group 1
        delay 1
        click button "Remove Now" of sheet 1

    end tell
    keystroke "w" using command down
end tell

Created the above script based on the one in Link 1 below. I have modified it to clear
the data for all the websites. If you need to clear data for specific website, refer
to the below link. But it is not working around the "Details..." button, so you might
have to modify it to :
        click button "Details..." of group 1 of group 1


Link 1: https://discussions.apple.com/thread/4011487?tstart=0

Reported by santus444 on 2015-08-19 22:14:14


- _Attachment: [Preferences.tiff](https://storage.googleapis.com/google-code-attachments/selenium/issue-5212/comment-23/Preferences.tiff)_ - _Attachment: [ClearCacheScript3.scpt](https://storage.googleapis.com/google-code-attachments/selenium/issue-5212/comment-23/ClearCacheScript3.scpt)_

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Reported by luke.semerau on 2015-09-17 17:45:11

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

https://github.com/SeleniumHQ/selenium/issues/1196

Reported by luke.semerau on 2016-01-29 16:45:01

  • Status changed: Duplicate

@lukeis lukeis closed this as completed Mar 4, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant