Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

watir-webdriver cookie jar saving and loading

I'm surprised there didn't seem to be much discussion on this.

in Mechanize I can easily read an entire cookie jar from the browser, store it to a file, and load it in to a later session/run before loading that website's pages again.

How can one do the same with watir-webdriver?

UPDATE

Now with 0.5.2 I do see new methods browser.cookies.to_hash which would turn this question into "How to implement .from_hash or similar loader using eg. .clear and .add?"

However I'd be especially keen on loading and saving all cookies using previous versions (0.4.1) which my servers are likely to be stuck with for a while. Via the Selenium driver maybe?

like image 681
Marcos Avatar asked Feb 11 '12 10:02

Marcos


1 Answers

browser = Watir::Browser.new :firefox
browser.goto 'http://google.com'
# save cookies
saved_cookies = browser.cookies.to_a
# clear and get new cookies
browser.cookies.clear
browser.goto 'http://google.com'
# set new cookies
browser.cookies.clear
saved_cookies.each do |saved_cookie|
  browser.cookies.add(saved_cookie[:name], saved_cookie[:value])
end
like image 189
p0deje Avatar answered Oct 01 '22 22:10

p0deje