Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Applying <a href="https://stackoverflow.com/a/9239869/1069375">pOdeje</a>'s loop to repopulate the cookie jar, here's a solution that includes storing the cookies into a file, readable in a later Ruby run. A straight <code>File.open</code> write and read of the array had some issues I didn't wish to work around (parsing?), but <strong>YAML object serialization</strong> already bundled in Ruby was well suited to the task.</p> <pre><code>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 =&gt; saved_cookie[:domain], :expires =&gt; saved_cookie[:expires], :path =&gt; saved_cookie[:path], :secure =&gt; saved_cookie[:secure] end </code></pre> <p>Still on the lookout for a pre <code>watir-webdriver 0.5.x</code> method, though.</p> <p><strong>CAVEAT</strong></p> <p>Not thoroughly tested yet but it seems I have to <em>first</em> load the URL to which the cookies apply, <em>then</em> load in my cookie jar using the above method, and <em>finally</em> load that URL into <code>$browser</code> object <strong>a second time</strong>. This is only a minor inconvenience and time cost for my case in which I'm staying within the same domain throughout my web session, but I can see this turning into a real thorn for cookie jars affecting several unrelated sites (as had indeed been an expectation for my old programs using other languages &amp; libraries like Mechanize). Curl and Wget and generally other tools I've used for SOAP interfacing always let me control my POST/session/cookie environments prior to site loading. Just a thought.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload