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.
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:
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With