SeleniumLibrary icon indicating copy to clipboard operation
SeleniumLibrary copied to clipboard

Get Cookies always Returns Empty List using the default Firefox profile

Open butterdave opened this issue 13 years ago • 15 comments

Here's a simple test to run...hopefully I'm wrong! Dependencies:

  • robotframework-selenium2library==1.1.0
  • firefox 19
  • robotframework==2.7.7

*** Settings *** Resource Collections Library Selenium2Library

*** Testcases *** Test Browser Should Have Cookies Open Browser http://www.google.com browser=firefox ${cookies}= Get Cookies Should Not Be Empty ${cookies}

butterdave avatar Mar 04 '13 14:03 butterdave

The default Firefox profile has several privacy settings, like browser.privatebrowsing.autostart, turned on so I wouldn't be surprised that the above test fails. I wouldn't expect any cookies. See the Open Browser keyword for more information about the Firefox profile.

emanlove avatar Mar 14 '13 19:03 emanlove

hi, thanks for the reply, are you talking about a default FF profile that's packaged up with Selenium2Library or my own ff profile? If the former, was this setting changed recently? It works on ff 18 :(

DP

butterdave avatar Mar 14 '13 20:03 butterdave

Yes, I was talking about the default FF profile within the Selenium2Library. If it works for FF18 and not FF19 the problem might lie within Selenium and not the Selenium2library. You could try to confirm this by creating a selenium only test as outlined here.

Sorry I am not much more helpful.

emanlove avatar Mar 14 '13 20:03 emanlove

@butterdave For what it is worth our internal tests for cookies are currently failing under Firefox 19

emanlove avatar Mar 20 '13 20:03 emanlove

thanks! i haven't had a chance to write the test to narrow who the guilty party may be. will let you know...also need to peruse the selenium issues list

butterdave avatar Mar 20 '13 21:03 butterdave

Long time and no action, but for what it's worth. If the line: user_pref("browser.privatebrowsing.autostart", true); is changed to: user_pref("browser.privatebrowsing.autostart", false); in Selenium2Library/resources/firefoxprofile/prefs.js it works for me.

oscarh avatar Mar 17 '14 12:03 oscarh

Wierd, but Oscarhs solution doesn't work for me :(

Can anybody told me is it an RF or Sel issue or no? So should we expect some new build fith fiz or just leave this and start looking for ... workaround except using coockies ?

Kristina001 avatar Jun 18 '14 14:06 Kristina001

I think @oscarh is onto something. On ESR 24, with browser.privatebrowsing.autostart = true, cookies set with Add Cookie show up in Get Cookies, but other cookies do not. Here's a test that passes if private browsing autostart is false, but not when it is true:

    Open Browser    http://www.w3schools.com/js/js_cookies.asp    ff
    Click Button    Create Cookie 1    # this sets a simple, session-only cookie
    ${cookies}=    Get Cookies
    Should Contain    ${cookies}    firstname=John

ombre42 avatar Jun 19 '14 03:06 ombre42

I noticed that even in the latest release of FF (29), the GCLI/Developer Toolbar (Shift+F2) has a command "cookie list" that will not show cookies that are visible via document.cookie when private browsing. I think the native interface that Selenium uses simply will not work during private browsing except to retrieve cookies set through that same cookie manager.

I think we should stop using private browsing, but set the profile to behave close to it - not remembering forms, deleting history and cookies on exit, etc. Here are the settings I think we should put in place of user_pref("browser.privatebrowsing.autostart", true);.

user_pref("places.history.enabled", false);
user_pref("privacy.sanitize.sanitizeOnShutdown", true);
user_pref("browser.formfill.enable", false);
user_pref("network.cookie.lifetimePolicy", 2);
user_pref("privacy.clearOnShutdown.offlineApps", true);
user_pref("privacy.clearOnShutdown.passwords", true);
user_pref("privacy.clearOnShutdown.siteSettings", true);

ombre42 avatar Jun 20 '14 02:06 ombre42

I have similar problem. I tweaked FF profile: user_pref("app.update.auto", false); user_pref("app.update.enabled", false); user_pref("browser.preferences.advanced.selectedTabIndex", 3); user_pref("browser.migration.version", 22); user_pref("browser.startup.homepage", "about:blank"); user_pref("browser.tabs.warnOnClose", false); user_pref("network.cookie.prefsMigrated", true); user_pref("privacy.sanitize.migrateFx3Prefs", true)

And by this configuration I get the cookies. Could the S2L default profile be updated to allow the Get Cookie* keywords to work.

aaltat avatar Jun 30 '14 13:06 aaltat

It looks like the capability to access the cookie manager used in private browsing via an API has not been implemented, so I am not going to bother the Selenium developers. See Mozilla bugs 823941 and 777620

ombre42 avatar Oct 01 '14 15:10 ombre42

I am still working on a fix.

I'm pretty sure my fix works but it is causing failures which are due to Get Cookies not working as described. It uses Selenium's get_cookies to fetch cookies which claims are 'of the current page' but it is not filtering. Chrome and Firefox (when not private browsing) return cookies set on the empty path (host?) as well. The same cookie set for /html is also setting it for /, so in Get Cookie's return value, it shows up twice. This is not an issue for Get Cookie. In Py and Java, it returns the first cookie with a matching name, and it appears that the cookie for the current page appears before the cookie for an empty path. All three things below are describing the exact same functionality:

  • Selenium Py doc for get_cookies: Returns a set of dictionaries, corresponding to cookies visible in the current session.
  • Selenium doc for GET /session/:sessionId/cookie: Retrieve all cookies visible to the current page.
  • Selenium Javadoc for getCookies(): Get all the cookies for the current domain. This is the equivalent of calling "document.cookie" and parsing the result

Guess we aren't the only ones keeping the documentation consistent and correct :)

I'm thinking of just changing the documentation for Get Cookies and adjusting the tests to handle the existing behavior.

After executing

    Add Cookie  test    seleniumlibrary
    Add Cookie  another     value

get_cookies is returning this for Chrome and FF (not private browsing):

    [{u'domain': u'localhost', u'name': u'test', u'value': u'seleniumlibrary', u'expiry': None, u'path': u'', u'secure': False}, 
    {u'domain': u'localhost', u'name': u'another', u'value': u'value', u'expiry': None, u'path': u'', u'secure': False}, 
    {u'domain': u'localhost', u'name': u'test', u'value': u'seleniumlibrary', u'expiry': None, u'path': u'/html/', u'secure': False}, 
    {u'domain': u'localhost', u'name': u'another', u'value': u'value', u'expiry': None, u'path': u'/html/', u'secure': False}]

ombre42 avatar Oct 04 '14 03:10 ombre42

I am OK with that. Specially if the documentation points to Mozilla doc/bug which explains that in the private browsing one can not access cookies and gives an example ff profile where private browsing is disabled.

aaltat avatar Oct 04 '14 09:10 aaltat

can be for this reason that my website with "remember me" functionality that uses cookies will not work?

i tried to modify

c:\Python27\Lib\site-packages\Selenium2Library\resources\firefoxprofile\prefs.js

adding @oscarh or @aaltat code but i still have the problem : cookies seems not to be stored so "remember me" doesn't works.

andreabisello avatar Jan 19 '16 15:01 andreabisello

No. It is caused in the way selenium works. More details can be found from the answer, to your question, in the user group.

aaltat avatar Jan 19 '16 15:01 aaltat