Permalink
Browse files

Merge pull request #3 from ammar01/master

Matt - pretty please? :-)
  • Loading branch information...
mcrmfc committed Aug 26, 2011
2 parents 34ce688 + ba978ff commit ea01a8c884b618d3b6b63b77db9097da12b730a4
Showing with 109 additions and 0 deletions.
  1. +51 −0 lib/monkey-patches/capybara-mechanize-patches.rb
  2. +58 −0 lib/monkey-patches/capybara-patches.rb
@@ -31,4 +31,55 @@ def process_remote_request(method, url, *options)
@last_request_remote = true
end
end
+
+ def cookies
+ cookies = []
+
+ agent.cookie_jar.jar.each do |domain|
+ domain[1].each do |path|
+ path[1].each do |cookie|
+ cookies.push({
+ :name => cookie[1].name,
+ :value => cookie[1].value,
+ :domain => cookie[1].domain,
+ :secure => cookie[1].secure,
+ :expires => cookie[1].expires,
+ :path => cookie[1].path
+ })
+ end
+ end
+ end
+ cookies
+ end
+
+ def cookie_named(name)
+ cookies.find { |c| c[:name] == name }
+ end
+
+ def delete_cookie(cookie_name)
+ agent.cookie_jar.jar.each do |domain|
+ domain[1].each do |path|
+ path[1].each do |cookie|
+ if cookie[0] == cookie_name
+ agent.cookie_jar.jar[domain[0]][path[0]].delete(cookie[0])
+ end
+ end
+ end
+ end
+ end
+
+ def delete_all_cookies
+ agent.cookie_jar.clear!
+ end
+
+ FakeURI = Struct.new(:host)
+ def add_cookie(attribs)
+ c = Mechanize::Cookie.new(attribs[:name],attribs[:value])
+ # remember: mechanize always removes leading '.' from domains
+ c.domain = attribs[:domain].sub!(/^./, '')
+ c.path = '/'
+ c.expires = attribs[:expires]
+ c.secure = attribs[:secure]
+ agent.cookie_jar.add(FakeURI.new(c.domain),c)
+ end
end
@@ -21,3 +21,61 @@ def style(prop)
end
end
+class Capybara::Driver::Selenium
+ def cookies
+ browser.manage.all_cookies
+ end
+
+ def cookie_named(name)
+ browser.manage.cookie_named(name)
+ end
+
+ def delete_cookie(cookie)
+ browser.manage.delete_cookie(cookie)
+ end
+
+ def delete_all_cookies
+ browser.manage.delete_all_cookies
+ end
+
+ def add_cookie(attribs)
+ browser.manage.add_cookie(attribs)
+ end
+end
+
+module Capybara
+ class Session
+ ##
+ # Get all cookies
+ #
+ # @return [Array<Hash>] list of cookies
+ #
+ def cookies
+ driver.cookies
+ end
+
+ ##
+ # Get the cookie with the given name
+ #
+ # @param [String] name the name of the cookie
+ # @return [Hash, nil] the cookie, or nil if it wasn't found.
+ #
+ def cookie_named(name)
+ driver.cookie_named(name)
+ end
+
+ def delete_cookie(cookie)
+ driver.delete_cookie(cookie)
+ end
+
+ def delete_all_cookies
+ driver.delete_all_cookies
+ end
+
+ def add_cookie(attribs)
+ driver.add_cookie(attribs)
+ end
+ end
+end
+
+

0 comments on commit ea01a8c

Please sign in to comment.