Class: Akephalos::Client

Inherits:
Object
  • Object
show all
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

Classes: Cookies, Filter

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_6 => HtmlUnit::BrowserVersion::FIREFOX_3_6
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (Hash) (defaults to: {}) —

    the configuration options for this client

Options Hash (options):

  • :browser (Symbol) — default: :firefox_3_6 —

    the browser version ( see BROWSER_VERSIONS)

  • :validate_scripts (true, false) — default: true —

    whether to raise errors on javascript errors



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/akephalos/client.rb', line 74

def initialize(options = {})
  process_options!(options)

  @_client = java.util.concurrent.FutureTask.new do

    if @http_proxy.nil? or @http_proxy_port.nil?
      client = HtmlUnit::WebClient.new(browser_version)
    else
      client = HtmlUnit::WebClient.new(browser_version, @http_proxy, @http_proxy_port)
    end

    client.setThrowExceptionOnFailingStatusCode(false)
    client.setAjaxController(HtmlUnit::NicelyResynchronizingAjaxController.new)
    client.setCssErrorHandler(HtmlUnit::SilentCssErrorHandler.new)
    client.setThrowExceptionOnScriptError(validate_scripts)
    client.setUseInsecureSSL(use_insecure_ssl)
    client.setRefreshHandler(HtmlUnit::WaitingRefreshHandler.new)

    Filter.new(client)
    client
  end
  Thread.new { @_client.run }
end

Instance Attribute Details

#browser_version ⇒ HtmlUnit::BrowserVersion (readonly)

Returns the configured browser version.

Returns:

  • (HtmlUnit::BrowserVersion) —

    the configured browser version



39
40
41
# File 'lib/akephalos/client.rb', line 39

def browser_version
  @browser_version
end

#htmlunit_log_level ⇒ "trace" / "debug" / "info" / "warn" / "error" or "fatal" (readonly)

Returns which points the htmlunit log level.

Returns:

  • ("trace" / "debug" / "info" / "warn" / "error" or "fatal") —

    which points the htmlunit log level



48
49
50
# File 'lib/akephalos/client.rb', line 48

def htmlunit_log_level
  @htmlunit_log_level
end

#page ⇒ Page

Returns the current page.

Returns:

  • (Page) —

    the current page



36
37
38
# File 'lib/akephalos/client.rb', line 36

def page
  @page
end

#use_insecure_ssl ⇒ true/false (readonly)

Returns whether to ignore insecure ssl certificates.

Returns:

  • (true/false) —

    whether to ignore insecure ssl certificates



45
46
47
# File 'lib/akephalos/client.rb', line 45

def use_insecure_ssl
  @use_insecure_ssl
end

#validate_scripts ⇒ true/false (readonly)

Returns whether to raise errors on javascript failures.

Returns:

  • (true/false) —

    whether to raise errors on javascript failures



42
43
44
# File 'lib/akephalos/client.rb', line 42

def validate_scripts
  @validate_scripts
end

Class Method Details

.new(*args) ⇒ Object



29
30
31
# File 'lib/akephalos/client.rb', line 29

def new(*args)
  ExceptionConvertingDelegator.new(new_orig(*args), "NativeException", RuntimeError)
end

.new_orig ⇒ Object



27
# File 'lib/akephalos/client.rb', line 27

alias_method :new_orig, :new

Instance Method Details

#close_all_windows ⇒ Object



186
187
188
# File 'lib/akephalos/client.rb', line 186

def close_all_windows()
  @client.closeAllWindows()
end

#confirm_dialog(confirm = true, &block) ⇒ Object

Confirm or cancel the dialog, returning the text of the dialog



178
179
180
181
182
183
184
# File 'lib/akephalos/client.rb', line 178

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.

Returns:

  • (Cookies) —

    the cookies for this session



108
109
110
# File 'lib/akephalos/client.rb', line 108

def cookies
  @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.

Parameters:

  • options (Hash) —

    the options to process



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/akephalos/client.rb', line 164

def process_options!(options)        
  options = DEFAULT_OPTIONS.merge(options)

  @browser_version  = BROWSER_VERSIONS.fetch(options.delete(:browser))
  @validate_scripts = options.delete(:validate_scripts)
  @use_insecure_ssl = options.delete(:use_insecure_ssl)
  @htmlunit_log_level = options.delete(:htmlunit_log_level)
  @http_proxy = options.delete(:http_proxy)
  @http_proxy_port = options.delete(:http_proxy_port)        

  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.

Returns:

  • (true, false) —

    whether to ignore insecure ssl certificates



155
156
157
# File 'lib/akephalos/client.rb', line 155

def use_insecure_ssl?
  !!use_insecure_ssl
end

#user_agent ⇒ String

Returns the current user agent string.

Returns:

  • (String) —

    the current user agent string



113
114
115
# File 'lib/akephalos/client.rb', line 113

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.

Parameters:

  • user_agent (:default) —

    the default user agent

  • user_agent (String) —

    the user agent string to use



122
123
124
125
126
127
128
129
130
# File 'lib/akephalos/client.rb', line 122

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.

Returns:

  • (true, false) —

    whether javascript errors will raise exceptions



150
151
152
# File 'lib/akephalos/client.rb', line 150

def validate_scripts?
  !!validate_scripts
end

#visit(url) ⇒ Page

Visit the requested URL and return the page.

Parameters:

  • url (String) —

    the URL to load

Returns:

  • (Page) —

    the loaded page



102
103
104
105
# File 'lib/akephalos/client.rb', line 102

def visit(url)
  client.getPage(url)
  page
end