Closed lukeis closed 8 years ago
Reported by barancev
on 2013-04-16 15:07:10
I did some additional exploration, and there is a partial workaround for now: setting
cookie via native javascript.
But either it doesn't work as well as WebDriver's addCookie method (more native) or
I'm not using the optimal javascript code to set it (using document.cookie with example
from W3C site)
http://www.w3schools.com/js/js_cookies.asp
Here's an example:
String cookieScript = "var doSetCookie = function setCookie(c_name,value,exdays){ var
exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value)
+ ((exdays==null) ? \"\" : \"; expires=\"+exdate.toUTCString()); document.cookie=c_name
+ \"=\" + c_value; }; doSetCookie(\"tagName\",\"{\\\"\"+arguments[0]+\"\\\": {\\\"tagLabel\\\":
\\\"\"+arguments[1]+\"\\\", \\\"tagId\\\": \\\"\"+arguments[2]+\"\\\"}}\",1);";
((JavascriptExecutor) driver).executeScript(cookieScript, testTagName, testLabelName,
testTagId);
I am able to get back the cookie with driver.manage().getCookieNamed() while on same
page where cookie is set, but it seems cookie is lost on navigating to another page,
and also, I don't see the cookie in Safari Web Inspector (looking at the cookie resources
section). So the cookie doesn't seem to fully persist correctly with this workaround,
but it does appear to set the cookie properly to some extent as I can read it back,
and part of the web app code that checks the cookie executes properly as expected (but
another part didn't work).
So this workaround may kind of work in some situations.
Reported by mangaroo
on 2013-04-16 21:14:58
Hope we can up the priority of fixing this bug in list of SafariDriver related issues
(for next fix, release, etc.). While there is workaround, it's not optimal, so a real
fix would be greatly appreciated. :)
Reported by mangaroo
on 2013-06-14 01:06:49
Thanks to a colleague today, I noticed that there is similar issue with Safari browser
itself (Safari 5 Windows and Safari 6 Mac, I think just Safari in general). We have
a feature where on an admin config page, user can set a cookie via clicking a button
to then use for manual testing. That doesn't take effect on Safari but works fine on
other browsers. However, just like this issue, executing the javascript workaround
code snippet (in Safari error/javascript console followed by page refresh), it does
take effect to some extent (seems on current page only). I didn't notice this before
since I focused on automation side of using the cookie rather than just manual testing.
Doing some online search, I see some similar JSON related problems with Safari, but
I didn't seem to find any filed OpenRadar bug/issue regarding Safari and JSON (cookies
or not).
http://stackoverflow.com/questions/19359326/json-parse-fails-in-safari-when-a-string-value-contains-a-comma
http://www.linkedin.com/groups/JSONstringify-doesnt-seem-work-in-100943.S.208455154
https://forum.foxycart.com/discussion/1490/x&page=1
So this may not be SafariDriver/extension specific but rather Safari specific issue.
I'll file an Apple bug & OpenRadar if I can get more info first on the bug.
What a pain Safari is...
Reported by mangaroo
on 2014-01-15 00:34:44
There are a few things that come to mind. Make sure that your route for the cookie
is the root of the site. If it is not the root of the site it would not be read by
a more strict browser. You may also want to make sure what you are storing the cookie
has everything escaped or replaced as needed. There may be certain characters that
are not allowed in cookies. There is also a length limitation on cookies as well.
I hope this helps.
Reported by charley.george
on 2014-01-15 00:59:02
We can close this bug. It's actually not a (SafariDriver) bug, but rather how the JSON
is dealt with that can result in this bug.
I originally constructed the JSON as string escaping as needed (guess there was some
issues there still that I didn't notice and for which Safari didn't throw any complaints,
just not work when cookie read back, but for which works fine on all other browsers).
We later had to support some additional functionality that expanded the JSON object/format,
so it would have been ugly to build as a string. I built the JSON as a Java JSONObject
then called toString() to get it back as a string to add/create the cookie. Using this
approach, Safari works fine. But I never knew nor checked back on that, still resorting
to the workaround code I posted earlier since I thought the bug still existed (not
knowing the root cause). That is until coworker discovered related bug and me following
up on things.
So based on this, for all those dealing with JSON cookies in Selenium, best to construct
the JSON object from your language's JSON library then convert to string rather than
build as raw strings or else you might miss something and encounter this on Safari.
And/or run the string output through URL encode/decode (or escape/unescape) as needed
to also avoid this.
Thanks Charley, will keep your suggestions in mind for the future.
Reported by mangaroo
on 2014-01-15 23:26:16
Reported by jmleyba
on 2014-01-16 02:40:02
WorkingAsIntended
Reported by luke.semerau
on 2015-09-17 18:17:08
Originally reported on Google Code with ID 5503
Reported by
mangaroo
on 2013-04-16 07:09:50