lib/celerity/browser.rb in walidhalabi-celerity-0.0.6.12 vs lib/celerity/browser.rb in walidhalabi-celerity-0.0.6.13
- old
+ new
@@ -30,11 +30,11 @@
# Creates a browser object.
#
# @see Celerity::Container for an introduction to the main API.
#
# @option opts :log_level [Symbol] (:warning) @see log_level=
- # @option opts :browser [:firefox, :internet_explorer] (:internet_explorer) Set the BrowserVersion used by HtmlUnit. Defaults to Internet Explorer.
+ # @option opts :browser [:internet_explorer, :firefox, :firefox3] (:firefox) Set the BrowserVersion used by HtmlUnit. Defaults to Firefox 2.
# @option opts :css [Boolean] (false) Enable CSS. Disabled by default.
# @option opts :secure_ssl [Boolean] (true) Disable secure SSL. Enabled by default.
# @option opts :resynchronize [Boolean] (false) Use HtmlUnit::NicelyResynchronizingAjaxController to resynchronize Ajax calls.
# @option opts :javascript_exceptions [Boolean] (false) Raise exceptions on script errors. Disabled by default.
# @option opts :status_code_exceptions [Boolean] (false) Raise exceptions on failing status codes (404 etc.). Disabled by default.
@@ -54,13 +54,14 @@
end
unless (render_types = [:html, :xml, nil]).include?(opts[:render])
raise ArgumentError, "expected one of #{render_types.inspect} for key :render"
end
+
+ opts = opts.dup # we'll delete from opts, so dup to avoid side effects
+ @options = opts.dup # keep the unmodified version around as well
- @options = opts.dup # for ClickableElement#click_and_attach
-
@render_type = opts.delete(:render) || :html
@charset = opts.delete(:charset) || "UTF-8"
self.log_level = opts.delete(:log_level) || :warning
@last_url, @page = nil
@@ -346,29 +347,31 @@
def add_cookie(domain, name, value, opts = {})
path = opts.delete(:path) || "/"
max_age = opts.delete(:max_age) || (Time.now + 60*60*24) # not sure if this is correct
secure = opts.delete(:secure) || false
- raise "unknown option: #{opts.inspect}" unless opts.empty?
+ raise(ArgumentError, "unknown option: #{opts.inspect}") unless opts.empty?
cookie = Cookie.new(domain, name, value, path, max_age, secure)
@webclient.getCookieManager.addCookie(cookie)
end
#
# Remove the cookie with the given domain and name (Celerity only)
#
# @param [String] domain
# @param [String] name
+ #
+ # @raise [CookieNotFoundError] if the cookie doesn't exist
#
def remove_cookie(domain, name)
cm = @webclient.getCookieManager
cookie = cm.getCookies.find { |c| c.getDomain == domain && c.getName == name }
if cookie.nil?
- raise "no cookie with domain #{domain.inspect} and name #{name.inspect}"
+ raise CookieNotFoundError, "no cookie with domain #{domain.inspect} and name #{name.inspect}"
end
cm.removeCookie(cookie)
end
@@ -405,16 +408,20 @@
# @yieldparam [Celerity::Browser] browser The browser instance.
# @see Celerity::Browser#resynchronized
#
def wait_until(timeout = 30, &block)
+ returned = nil
+
Timeout.timeout(timeout) do
- until yield(self)
+ until returned = yield(self)
refresh_page_from_window
sleep 0.1
end
end
+
+ returned
end
#
# Wait while the given block evaluates to true (Celerity only)
#
@@ -422,16 +429,20 @@
# @yieldparam [Celerity::Browser] browser The browser instance.
# @see Celerity::Browser#resynchronized
#
def wait_while(timeout = 30, &block)
+ returned = nil
+
Timeout.timeout(timeout) do
- while yield(self)
+ while returned = yield(self)
refresh_page_from_window
sleep 0.1
end
end
+
+ returned
end
#
# Allows you to temporarily switch to HtmlUnit's NicelyResynchronizingAjaxController
# to resynchronize ajax calls.
@@ -469,25 +480,22 @@
#
# Start or stop HtmlUnit's DebuggingWebConnection. (Celerity only)
# The output will go to /tmp/«name»
#
- # @param [Boolean] bool start or stop
- # @param [String] name required if bool is true
+ # @param [String] name directory name
+ # @param [block] blk block to execute
#
- def debug_web_connection(bool, name = nil)
- if bool
- raise "no name given" unless name
- @old_webconnection = @webclient.getWebConnection
- dwc = HtmlUnit::Util::DebuggingWebConnection.new(@old_webconnection, name)
- @webclient.setWebConnection(dwc)
- $stderr.puts "debug-webconnection on"
- else
- @webclient.setWebConnection(@old_webconnection) if @old_webconnection
- $stderr.puts "debug-webconnection off"
- end
+ def debug_web_connection(name, &blk)
+ old_wc = @webclient.getWebConnection
+
+ @webclient.setWebConnection HtmlUnit::Util::DebuggingWebConnection.new(old_wc, name)
+ res = yield
+ @webclient.setWebConnection old_wc
+
+ res
end
#
# Add a listener block for one of the available types. (Celerity only)
# Types map to HtmlUnit interfaces like this:
@@ -682,11 +690,11 @@
end
#
# Check that we have a @page object.
#
- # @raise [Celerity::Exception::UnknownObjectException] if no page is loaded.
+ # @raise [UnknownObjectException] if no page is loaded.
# @api private
#
def assert_exists
raise UnknownObjectException, "no page loaded" unless exist?
@@ -720,11 +728,13 @@
def setup_webclient(opts)
browser = (opts.delete(:browser) || :firefox).to_sym
case browser
- when :firefox, :ff
+ when :firefox, :ff, :ff2
browser_version = ::HtmlUnit::BrowserVersion::FIREFOX_2
+ when :firefox3, :ff3
+ browser_version = ::HtmlUnit::BrowserVersion::FIREFOX_3
when :internet_explorer, :ie
browser_version = ::HtmlUnit::BrowserVersion::INTERNET_EXPLORER_7
else
raise ArgumentError, "unknown browser: #{browser.inspect}"
end