rubywatirtyphoeus

How can I generate a cookie file from Watir cookies


I am trying to download a file from Watir but I don't want to use the loop sleep approach.

I would rather at the last moment of the interaction, recreate the session Watir has on the webpage and use another library, for example Typhoeus.

Typhoeus uses curl and can use cookies from a file, nonetheless, Watir generates a Hash Array, and if I ask to save it, it saves it as a YAML file.

Is there a faster way to convert it?


Solution

  • Another article in StackOverflow said that curl uses Mozilla style cookie files.

    So, if your Watir instance is browser and the file to which you are going to write to is file you can do

    browser.cookies.to_a.each do |ch| 
      terms = []
      terms << ch[:domain]
      terms << ch[:same_site].nil? ? 'FALSE' : 'TRUE'
      terms << ch[:path]
      terms << ch[:secure] ? 'TRUE' : 'FALSE'
      terms << ch[:expires].to_i.to_s
      terms << ch[:name]
      terms << ch[:value]
      file.puts terms.join("\t")
    end
    

    Then you can tell Typhoeus to use the contents of file to keep navigating using the same cookies.