lib/watir-webdriver/cookies.rb in watir-webdriver-0.6.4 vs lib/watir-webdriver/cookies.rb in watir-webdriver-0.6.5

- old
+ new

@@ -1,7 +1,10 @@ +require 'yaml' + module Watir class Cookies + def initialize(control) @control = control end # @@ -29,27 +32,24 @@ # @param [String] name # @param [String] value # @param [Hash] opts # @option opts [Boolean] :secure # @option opts [String] :path - # @option opts [] :expires TODO what type + # @option opts [Time, DateTime, NilClass] :expires # @option opts [String] :domain # def add(name, value, opts = {}) cookie = { :name => name, :value => value, :secure => opts[:secure], :path => opts[:path], :expires => opts[:expires], + :domain => opts[:domain], } - if opts[:domain] - cookie[:domain] = opts[:domain] - end - @control.add_cookie cookie end # # Deletes cookie by given name. @@ -73,17 +73,45 @@ def clear @control.delete_all_cookies end + # + # Save cookies to file + # + # @example + # browser.cookies.save '.cookies' + # + # @param [String] file + # + + def save(file = '.cookies') + IO.write(file, to_a.to_yaml) + end + + # + # Load cookies from file + # + # @example + # browser.cookies.load '.cookies' + # + # @param [String] file + # + + def load(file = '.cookies') + YAML.load(IO.read(file)).each do |c| + add(c.delete(:name), c.delete(:value), c) + end + end + private def to_time(t) if t.respond_to?(:to_time) t.to_time else ::Time.local t.year, t.month, t.day, t.hour, t.min, t.sec end end - end -end + end # Cookies +end # Watir