Permalink
Browse files

add add_cookie, get_cookie and clear_cookies

  • Loading branch information...
1 parent 94cda5f commit 5c22c3586b431bcdfda0f1a691730274fb2c39e6 @svenfuchs committed Apr 18, 2010
Showing with 39 additions and 0 deletions.
  1. +12 −0 lib/steam/browser/html_unit.rb
  2. +16 −0 lib/steam/browser/html_unit/client.rb
  3. +11 −0 test/browser/html_unit_test.rb
View
12 lib/steam/browser/html_unit.rb
@@ -80,6 +80,18 @@ def remove_handler(type)
@client.remove_handler(type)
end
+ def get_cookie(name)
+ @client.get_cookie(name)
+ end
+
+ def add_cookie(name, value)
+ @client.add_cookie(name, value)
+ end
+
+ def clear_cookies
+ @client.clear_cookies
+ end
+
protected
def respond_to
View
16 lib/steam/browser/html_unit/client.rb
@@ -81,6 +81,22 @@ def remove_handler(type)
@java.send(:"set#{type.to_s.camelize}Handler", nil)
end
+ def get_cookie(name)
+ cookie = @java.getCookieManager.getCookie(name)
+ cookie ? cookie.getValue : nil
+ end
+
+ def add_cookie(name, value) # TODO what about domain, path, expires etc?
+ cookie = Cookie.new
+ cookie.setName(name)
+ cookie.setValue(value)
+ @java.getCookieManager.addCookie(cookie)
+ end
+
+ def clear_cookies
+ @java.getCookieManager.clearCookies
+ end
+
def log_level=(level)
[ 'com.gargoylesoftware.htmlunit',
'com.gargoylesoftware.htmlunit.html',
View
11 test/browser/html_unit_test.rb
@@ -36,6 +36,17 @@ def within(*args, &block)
assert @alerted
end
+ test 'adding a cookie' do
+ @browser.add_cookie('foo', 'bar')
+ assert_equal 'bar', @browser.get_cookie('foo')
+ end
+
+ test 'clearing cookies' do
+ @browser.add_cookie('foo', 'bar')
+ @browser.clear_cookies
+ assert_equal nil, @browser.get_cookie('foo')
+ end
+
test 'locate with node type' do
element = locate(:a)
assert_equal 'a', element.name

0 comments on commit 5c22c35

Please sign in to comment.