Using the pOdeje loop to refill the cookie doll, here is a solution that involves saving cookies to a file, which can be read the next time you run Ruby. Direct File.open writing and reading the array had some problems that I didn’t want to work with (parsing?), But Serializing YAML objects already associated with Ruby was well suited for the task.
require 'yaml' # Save/serialize cookies # File.open("ST.cookies.txt", 'w').write $browser.cookies.to_a.to_s File.open("ST.cookies.yaml", 'w').write YAML::dump($browser.cookies.to_a) # Load/deserialize cookies # $cookies = File.open("ST.cookies.txt", 'r').to_a # returns 1-elem array of single long line, not indiv elements $cookies = YAML::load(File.open("ST.cookies.yaml", 'r')) $browser.cookies.clear $cookies.each do |saved_cookie| $browser.cookies.add saved_cookie[:name], saved_cookie[:value], :domain => saved_cookie[:domain], :expires => saved_cookie[:expires], :path => saved_cookie[:path], :secure => saved_cookie[:secure] end
However, when searching for the pre watir-webdriver 0.5.x .
CAVEAT
Not fully verified, but it seems to me that I must first download the URL to which cookies are applied, and then load in the cookie jar using the above method, and finally load this URL into the $browser object a second time . These are just minor inconveniences and time costs for my case when I stay in the same domain throughout my web session, but I see that this turns into a real thorn for cookie banners affecting several unrelated sites (as it was in fact the expectation my old programs using other languages and libraries like Mechanize). Curl and Wget, and in general other tools that I used for SOAP interaction, always allow me to control the POST / session / cookie environment before the site loads. Just a thought.
source share