Class: Akephalos::Client
- Inherits:
-
Object
- Object
- Akephalos::Client
- Defined in:
- lib/akephalos/client.rb,
lib/akephalos/client/filter.rb,
lib/akephalos/client/cookies.rb
Overview
Akephalos::Client wraps HtmlUnit’s WebClient class. It is the main entry point for all interaction with the browser, exposing its current page and allowing navigation.
Defined Under Namespace
Constant Summary collapse
- DEFAULT_OPTIONS =
The default configuration options for a new Client.
{ :browser => :firefox_3_6, :validate_scripts => true, :use_insecure_ssl => false, :htmlunit_log_level => 'fatal' }
- BROWSER_VERSIONS =
Map of browser version symbols to their HtmlUnit::BrowserVersion instances.
{ :ie6 => HtmlUnit::BrowserVersion::INTERNET_EXPLORER_6, :ie7 => HtmlUnit::BrowserVersion::INTERNET_EXPLORER_7, :ie8 => HtmlUnit::BrowserVersion::INTERNET_EXPLORER_8, :firefox_3 => HtmlUnit::BrowserVersion::FIREFOX_3, :firefox_3_6 => HtmlUnit::BrowserVersion::FIREFOX_3_6 }
Instance Attribute Summary collapse
-
#browser_version ⇒ HtmlUnit::BrowserVersion
readonly
The configured browser version.
-
#htmlunit_log_level ⇒ "trace" / "debug" / "info" / "warn" / "error" or "fatal"
readonly
Which points the htmlunit log level.
-
#page ⇒ Page
The current page.
-
#use_insecure_ssl ⇒ true/false
readonly
Whether to ignore insecure ssl certificates.
-
#validate_scripts ⇒ true/false
readonly
Whether to raise errors on javascript failures.
Instance Method Summary collapse
-
#confirm_dialog(confirm = true, &block) ⇒ Object
Confirm or cancel the dialog, returning the text of the dialog.
-
#cookies ⇒ Cookies
The cookies for this session.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#process_options!(options) ⇒ Object
Merges the DEFAULT_OPTIONS with those provided to initialize the Client state, namely, its browser version, whether it should validate scripts, and htmlunit log level.
-
#use_insecure_ssl? ⇒ true, false
Whether to ignore insecure ssl certificates.
-
#user_agent ⇒ String
The current user agent string.
-
#user_agent=(user_agent) ⇒ Object
Set the User-Agent header for this session.
-
#validate_scripts? ⇒ true, false
Whether javascript errors will raise exceptions.
-
#visit(url) ⇒ Page
Visit the requested URL and return the page.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/akephalos/client.rb', line 64 def initialize( = {}) () @_client = java.util.concurrent.FutureTask.new do client = HtmlUnit::WebClient.new(browser_version) client.setThrowExceptionOnFailingStatusCode(false) client.setAjaxController(HtmlUnit::NicelyResynchronizingAjaxController.new) client.setCssErrorHandler(HtmlUnit::SilentCssErrorHandler.new) client.setThrowExceptionOnScriptError(validate_scripts) client.setUseInsecureSSL(use_insecure_ssl) Filter.new(client) client end Thread.new { @_client.run } end |
Instance Attribute Details
#browser_version ⇒ HtmlUnit::BrowserVersion (readonly)
Returns the configured browser version.
28 29 30 |
# File 'lib/akephalos/client.rb', line 28 def browser_version @browser_version end |
#htmlunit_log_level ⇒ "trace" / "debug" / "info" / "warn" / "error" or "fatal" (readonly)
Returns which points the htmlunit log level.
37 38 39 |
# File 'lib/akephalos/client.rb', line 37 def htmlunit_log_level @htmlunit_log_level end |
#page ⇒ Page
Returns the current page.
25 26 27 |
# File 'lib/akephalos/client.rb', line 25 def page @page end |
#use_insecure_ssl ⇒ true/false (readonly)
Returns whether to ignore insecure ssl certificates.
34 35 36 |
# File 'lib/akephalos/client.rb', line 34 def use_insecure_ssl @use_insecure_ssl end |
#validate_scripts ⇒ true/false (readonly)
Returns whether to raise errors on javascript failures.
31 32 33 |
# File 'lib/akephalos/client.rb', line 31 def validate_scripts @validate_scripts end |
Instance Method Details
#confirm_dialog(confirm = true, &block) ⇒ Object
Confirm or cancel the dialog, returning the text of the dialog
160 161 162 163 164 165 166 |
# File 'lib/akephalos/client.rb', line 160 def confirm_dialog(confirm = true, &block) handler = HtmlUnit::ConfirmHandler.new handler.handleConfirmValue = confirm client.setConfirmHandler(handler) yield if block_given? return handler.text end |
#cookies ⇒ Cookies
Returns the cookies for this session.
92 93 94 |
# File 'lib/akephalos/client.rb', line 92 def @cookies ||= Cookies.new(client.getCookieManager) end |
#process_options!(options) ⇒ Object
Merges the DEFAULT_OPTIONS with those provided to initialize the Client state, namely, its browser version, whether it should validate scripts, and htmlunit log level.
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/akephalos/client.rb', line 148 def () = DEFAULT_OPTIONS.merge() @browser_version = BROWSER_VERSIONS.fetch(.delete(:browser)) @validate_scripts = .delete(:validate_scripts) @use_insecure_ssl = .delete(:use_insecure_ssl) @htmlunit_log_level = .delete(:htmlunit_log_level) java.lang.System.setProperty("org.apache.commons.logging.simplelog.defaultlog", @htmlunit_log_level) end |
#use_insecure_ssl? ⇒ true, false
Returns whether to ignore insecure ssl certificates.
139 140 141 |
# File 'lib/akephalos/client.rb', line 139 def use_insecure_ssl? !!use_insecure_ssl end |
#user_agent ⇒ String
Returns the current user agent string.
97 98 99 |
# File 'lib/akephalos/client.rb', line 97 def user_agent @user_agent || client.getBrowserVersion.getUserAgent end |
#user_agent=(user_agent) ⇒ Object
Set the User-Agent header for this session. If :default is given, the User-Agent header will be reset to the default browser’s user agent.
106 107 108 109 110 111 112 113 114 |
# File 'lib/akephalos/client.rb', line 106 def user_agent=(user_agent) if user_agent == :default @user_agent = nil client.removeRequestHeader("User-Agent") else @user_agent = user_agent client.addRequestHeader("User-Agent", user_agent) end end |
#validate_scripts? ⇒ true, false
Returns whether javascript errors will raise exceptions.
134 135 136 |
# File 'lib/akephalos/client.rb', line 134 def validate_scripts? !!validate_scripts end |
#visit(url) ⇒ Page
Visit the requested URL and return the page.
86 87 88 89 |
# File 'lib/akephalos/client.rb', line 86 def visit(url) client.getPage(url) page end |