Browse files

add support for Cookies API

  • Loading branch information...
1 parent 3e16bff commit 0cc8311061811417b33226e386a90754ad387aa9 @jarmo jarmo committed Mar 4, 2012
Showing with 50 additions and 0 deletions.
  1. +45 −0 watir/lib/watir/cookies.rb
  2. +1 −0 watir/lib/watir/core.rb
  3. +4 −0 watir/lib/watir/ie-class.rb
View
45 watir/lib/watir/cookies.rb
@@ -0,0 +1,45 @@
+require "uri"
+
+module Watir
+ class Cookies
+ include Enumerable
+
+ def initialize(page_container)
+ @page_container = page_container
+ end
+
+ def each
+ @page_container.document.cookie.split("; ").each do |cookie|
+ name, value = cookie.split("=")
+ yield({:name => name, :value => value})
+ end
+ end
+
+ def add name, value, options={}
+ options = options.map do |option|
+ k, v = option
+ case k
+ when :expires
+ "#{k}=#{v.gmtime.strftime("%a, %-d %b %Y %H:%M:%S UTC")}"
+ when :secure
+ "secure"
+ else
+ "#{k}=#{v}"
+ end
+ end.join("; ")
+
+ options = "; #{options}" unless options.empty?
+ @page_container.document.cookie = "#{name}=#{value}#{options}"
+ end
+
+ def delete name, options={}
+ add name, nil, options.merge(:expires => Time.now - 1000)
+ end
+
+ def clear options={}
+ each do |cookie|
+ delete cookie[:name], options
+ end
+ end
+ end
+end
View
1 watir/lib/watir/core.rb
@@ -28,6 +28,7 @@
require 'watir/image'
require 'watir/link'
require 'watir/window'
+require 'watir/cookies'
require 'watir/win32'
require 'watir/modal_dialog'
View
4 watir/lib/watir/ie-class.rb
@@ -501,6 +501,10 @@ def windows(specifiers={}, &blk)
self.class._find_all(specifiers.keys.first, specifiers.values.first).map {|ie| Window.new(self, specifiers, IE.bind(ie), &blk)}
end
+ def cookies
+ Cookies.new(self)
+ end
+
#
# Synchronization
#

0 comments on commit 0cc8311

Please sign in to comment.