8
votes

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.

3
Please share relevant code.Željko Filipin
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.Chuck van der Linden

3 Answers

3
votes

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

3
votes

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.

2
votes

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.