Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add confirm and alert handlers

  • Loading branch information...
commit 94cda5f49819fec82d982ea99dc125a6afa82e44 1 parent d0f29a2
@svenfuchs authored
View
11 lib/steam/browser/html_unit.rb
@@ -10,9 +10,10 @@ module Browser
class HtmlUnit
autoload :Actions, 'steam/browser/html_unit/actions'
autoload :Client, 'steam/browser/html_unit/client'
+ autoload :Connection, 'steam/browser/html_unit/connection'
autoload :Drb, 'steam/browser/html_unit/drb'
autoload :Forker, 'steam/browser/html_unit/forker'
- autoload :Connection, 'steam/browser/html_unit/connection'
+ autoload :Handler, 'steam/browser/html_unit/handler'
autoload :Matchers, 'steam/browser/html_unit/matchers'
autoload :Page, 'steam/browser/html_unit/page'
autoload :WebResponse, 'steam/browser/html_unit/web_response'
@@ -71,6 +72,14 @@ def within(*args, &block)
Locator.within(response.body, *args, &block)
end
+ def set_handler(type, &block) # TODO use delegate
+ @client.set_handler(type, &block)
+ end
+
+ def remove_handler(type)
+ @client.remove_handler(type)
+ end
+
protected
def respond_to
View
18 lib/steam/browser/html_unit/client.rb
@@ -13,6 +13,8 @@ class Client
Java.import 'com.gargoylesoftware.htmlunit.BrowserVersion'
Java.import 'com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController'
Java.import 'com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException'
+ Java.import 'org.apache.commons.httpclient.Cookie', 'Java::Com::Gargoylesoftware::Htmlunit::Cookie'
+ # Java.import 'com.gargoylesoftware.htmlunit.util.Cookie'
include Java::Com::Gargoylesoftware::Htmlunit
@@ -20,10 +22,11 @@ class SilencingListener
def notify(message, origin); end
end
- attr_reader :connection
+ attr_reader :connection, :handlers
def initialize(connection = nil, options = {})
@connection = connection
+ @handlers = {}
options = Steam.config[:html_unit].merge(options)
@java = WebClient.new(BrowserVersion.send(options[:browser_version]))
@@ -53,11 +56,11 @@ def initialize(connection = nil, options = {})
def get(request)
perform(self.request_settings(request))
end
-
+
def perform(request_settings)
@java._invoke('getPage', 'Lcom.gargoylesoftware.htmlunit.WebRequestSettings;', request_settings)
end
-
+
def request_settings(request)
url = Java::Net::Url.new(request.url)
settings = WebRequestSettings.new(url)
@@ -70,7 +73,14 @@ def wait_for_javascript(timeout)
yield if block_given?
end
- # FIXME setLevel throws "Fail: unknown method name `setLevel'". weird.
+ def set_handler(type, &block)
+ @java.send(:"set#{type.to_s.camelize}Handler", Handler.create(type, block))
+ end
+
+ def remove_handler(type)
+ @java.send(:"set#{type.to_s.camelize}Handler", nil)
+ end
+
def log_level=(level)
[ 'com.gargoylesoftware.htmlunit',
'com.gargoylesoftware.htmlunit.html',
View
36 lib/steam/browser/html_unit/handler.rb
@@ -0,0 +1,36 @@
+module Steam
+ module Browser
+ class HtmlUnit
+ module Handler
+ class << self
+ def create(type, proc)
+ type = type.to_s.camelize
+ handler = self.const_get(type).new(proc)
+ Rjb::bind(handler, "com.gargoylesoftware.htmlunit.#{type}Handler")
+ end
+ end
+
+ class Base
+ attr_reader :proc
+
+ def initialize(proc)
+ @proc = proc
+ end
+ end
+
+ class Alert < Base
+ def handleAlert(page, message)
+ proc.call(page, message)
+ end
+ end
+
+ class Confirm < Base
+ def handleConfirm(page, message)
+ result = proc.call(page, message)
+ result.nil? || !!result
+ end
+ end
+ end
+ end
+ end
+end
View
17 test/browser/html_unit_test.rb
@@ -19,6 +19,23 @@ def within(*args, &block)
@browser.within(*args, &block)
end
+ test 'using an alert handler' do
+ @browser.set_handler(:alert) { |page, message| @alerted = message == 'FOO!' }
+ @browser.execute('alert("FOO!");')
+ assert @alerted
+ end
+
+ test 'using an confirm handler' do
+ @browser.set_handler(:alert) { |page, message| @alerted = true }
+ @browser.set_handler(:confirm) { |page, message| message == 'bar?' }
+
+ @browser.execute('if(confirm("foo?")) alert("");')
+ assert !@alerted
+
+ @browser.execute('if(confirm("bar?")) alert("");')
+ assert @alerted
+ end
+
test 'locate with node type' do
element = locate(:a)
assert_equal 'a', element.name
Please sign in to comment.
Something went wrong with that request. Please try again.