Chrome/Firefox do not retain cookies when invoked via (Watir-)Webdriver?
Asked Answered
A

3

8

I am working on a project for a client using Ruby & Watir. He requires that the session is restored when the application is reloaded (so to save his login states). This is the normal behavior of Chrome (it seems), and Firefox has the option to do this as well.

I've tried everything I can think of, and cannot seem to get this working using watir-webdriver. I've specified to Watir to use a specific profile (which you'd think would've resolved the problem), made sure all the settings are correct (which they are, because they work when I start the browser myself). I've tried Chrome with various switches and profile options I thought may help. Nothing.

So, the question is, why is this happening, and what might I do to resolve or get around this issue? I've run out of ideas almost completely at this point. I've even tried saving and reloading the cookies manually in IRB, but to no avail...

I know it must be possible, as when I login to the websites manually, close the browser, and re-open it, I am still logged in. I need to reproduce this exact behavior, but something is going wrong somewhere in between the manual instance of Chrome/Firefox, and the one controlled by Watir.

Lastly, I'll mention I noticed that when I specify my profile, it DOES load my history entries up, but not any of the cookies. It's just befuddling me more and more.

If anyone can help me, it would be greatly appreciated.

Alage answered 8/5, 2013 at 1:2 Comment(2)
Please share relevant code.Parkway
This sounds like an issue with webdriver-chromedriver. Watir-webdriver is riding on top of those, and the creation of the browser session etc is entirely up to webdriver. I'm going to retag the question accordingly.Mandiemandingo
A
3

Well, the good news is I've come up with a solution. I wanted to share it, in case anybody else ever finds their way into my shoes with this problem. My sincerest apologies to those of you who have.

My eventual solution was as follows:

  1. Create a Firefox profile specifically for the application
  2. Set Firefox to "Restore Windows & Tabs". This didn't work (something with Watir/Selenium) and would just ignore the settings, even while Firefox recognized them.
  3. To fix the aforementioned problem, I found a user.js file in Selenium, edited the settings to "Restore Windows & Tabs" manually in this file, and pasted the file over the one in my Firefox profile's directory (~/.mozilla/firefox/*).
  4. Firefox replaces the preferences in prefs.js with the ones in user.js on every run. This fixed it.

I will note that I had to actually tweak quite a bit of code in selenium-webdriver itself to make this all run smoothly. watir-webdriver sits on top of selenium-webdriver.

Alage answered 10/5, 2013 at 5:16 Comment(2)
Glad to hear you found a way to accomplish what you needed.Mandiemandingo
It's been quite a while, Chuck. I wanted to say thanks again for your answer. It was a fine answer, and you were completely right about it being by design. It blows my mind that there are no open-source projects more in the vein of the types of things I'm trying to do. Just seems insane to me with the volume of these kinds of questions around the net. Anyhow, just wanted to let you know you're appreciated. Thanks!Alage
M
3

I believe that by design, webdriver always starts out sessions with a clean slate cookie wise.

This can make things a bit tricky when you are trying to do a test that simulates closing and re-opening the browser (which is really in a lot of ways, testing the browser more than the website, since the webserver really has no way to know that the browser was closed and re-opened)

If you want to try to save and restore cookies, an important caveat is exposed by reading some of the webdriver docs, in the section on cookies where it says this

Before we leave these next steps, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for. If you are trying to preset cookies before you start interacting with a site and your homepage is large / takes a while to load an alternative is to find a smaller page on the site, typically the 404 page is small (http://example.com/some404page)

So if you are going to try saving cookies, then restoring them after bouncing the browser, you may need to navigate to someplace on the site before you try to re-create the cookies.

I'd try that via IRB and see what you get

Mandiemandingo answered 8/5, 2013 at 22:12 Comment(1)
Thanks so much for the comment. I had actually tried that when I came here, and was looking for a different solution. I thought, "why not just find a way to use Firefox's built-in ability to save its session between closes"? Check my answer here for more details.Alage
A
3

Well, the good news is I've come up with a solution. I wanted to share it, in case anybody else ever finds their way into my shoes with this problem. My sincerest apologies to those of you who have.

My eventual solution was as follows:

  1. Create a Firefox profile specifically for the application
  2. Set Firefox to "Restore Windows & Tabs". This didn't work (something with Watir/Selenium) and would just ignore the settings, even while Firefox recognized them.
  3. To fix the aforementioned problem, I found a user.js file in Selenium, edited the settings to "Restore Windows & Tabs" manually in this file, and pasted the file over the one in my Firefox profile's directory (~/.mozilla/firefox/*).
  4. Firefox replaces the preferences in prefs.js with the ones in user.js on every run. This fixed it.

I will note that I had to actually tweak quite a bit of code in selenium-webdriver itself to make this all run smoothly. watir-webdriver sits on top of selenium-webdriver.

Alage answered 10/5, 2013 at 5:16 Comment(2)
Glad to hear you found a way to accomplish what you needed.Mandiemandingo
It's been quite a while, Chuck. I wanted to say thanks again for your answer. It was a fine answer, and you were completely right about it being by design. It blows my mind that there are no open-source projects more in the vein of the types of things I'm trying to do. Just seems insane to me with the volume of these kinds of questions around the net. Anyhow, just wanted to let you know you're appreciated. Thanks!Alage
O
2

If you use any existing folder as user-data-dir switch, you keep all files and sessions after browser is closed. Otherwise, it creates folder and deletes it (with all cookies, tmp files and sessions) afterwards.

So you cold use any of existing profiles located as described here or default path at chrome://version/ url

In my case it is /Users/mikhail/Library/Application Support/Google/Chrome/Default

For some reason proper path would be this string without last '/Default' part of the path:

require 'watir-webdriver'

username = ENV['USER'] #or just your name
switches = %W[--user-data-dir=/Users/#{username}/Library/Application\ Support/Google/Chrome/]

browser = Watir::Browser.new :chrome, switches: switches
browser.goto 'google.com'

In this case you keep all of history and installed extensions.

Or simpler:

require 'watir-webdriver'

switches = %W[--user-data-dir=/some\ folder]
browser = Watir::Browser.new :chrome, switches: switches
browser.goto 'google.com'

In this case some folder should exist and you will create new profile from scratch.

Octillion answered 26/12, 2015 at 20:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.