Chrome/Firefox do not retain cookies when invoked via (Watir-)Webdriver? Chrome/Firefox do not retain cookies when invoked via (Watir-)Webdriver? google-chrome google-chrome

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


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


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 (somethingwith Watir/Selenium) and would just ignore the settings, even whileFirefox recognized them.
  3. To fix the aforementioned problem, I found a user.js file inSelenium, edited the settings to "Restore Windows & Tabs" manuallyin this file, and pasted the file over the one in my Firefoxprofile's directory (~/.mozilla/firefox/*).
  4. Firefox replaces the preferences in prefs.js with the ones inuser.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.


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 nameswitches = %W[--user-data-dir=/Users/#{username}/Library/Application\ Support/Google/Chrome/]browser = Watir::Browser.new :chrome, switches: switchesbrowser.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: switchesbrowser.goto 'google.com'

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