Class: Celerity::Browser

Inherits:
Object
  • Object
show all
Includes:
Container
Defined in:
lib/celerity/browser.rb,
lib/celerity/watir_compatibility.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Container

#browser

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Container

#area, #areas, #button, #buttons, #cell, #cells, #check_box, #checkboxes, #container=, #dd, #dds, #div, #divs, #dl, #dls, #dt, #dts, #em, #ems, #file_field, #file_fields, #form, #forms, #frame, #frames, #h1, #h1s, #h2, #h2s, #h3, #h3s, #h4, #h4s, #h5, #h5s, #h6, #h6s, #hidden, #hiddens, #image, #images, #label, #labels, #li, #link, #links, #lis, #map, #maps, #meta, #metas, #ol, #ols, #option, #p, #pre, #pres, #ps, #radio, #radios, #row, #rows, #select_list, #select_lists, #span, #spans, #strong, #strongs, #table, #tables, #tbodies, #tbody, #text_field, #text_fields, #tfoot, #tfoots, #th, #thead, #theads, #ths, #ul, #uls, #update_page

Methods included from ShortInspect

#short_inspect

Constructor Details

#initialize(opts = {}) ⇒ Celerity::Browser

Creates a browser object.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :log_level (Symbol) — default: :warning

    @see log_level=

  • :browser (:internet_explorer, :firefox, :firefox3) — default: :firefox

    Set the BrowserVersion used by HtmlUnit. Defaults to Firefox 2.

  • :css (Boolean) — default: false

    Enable CSS. Disabled by default.

  • :secure_ssl (Boolean) — default: true

    Enable/disable secure SSL. Enabled by default.

  • :javascript_enabled (Boolean) — default: true

    Enable/disable JavaScript evaluation. Enabled by default.

  • :resynchronize (Boolean) — default: false

    Use HtmlUnit::NicelyResynchronizingAjaxController to resynchronize Ajax calls.

  • :javascript_exceptions (Boolean) — default: false

    Raise exceptions on script errors. Disabled by default.

  • :status_code_exceptions (Boolean) — default: false

    Raise exceptions on failing status codes (404 etc.). Disabled by default.

  • :render (:html, :xml) — default: :html

    What DOM representation to send to connected viewers.

  • :charset (String) — default: "UTF-8"

    Specify the charset that webclient will use for requests, and those where texts are getting gibberished, like Browser#html.

  • :proxy (String) — default: nil

    Proxy server to use, in address:port format.

  • :user_agent (String)

    Override the User-Agent set by the :browser option

  • :ignore_pattern (Regexp)

    See Browser#ignore_pattern=

Raises:

  • (ArgumentError)

See Also:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/celerity/browser.rb', line 53

def initialize(opts = {})
  unless opts.is_a?(Hash)
    raise TypeError, "wrong argument type #{opts.class}, expected Hash"
  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

  @render_type   = opts.delete(:render)    || :html
  @charset       = opts.delete(:charset)   || "UTF-8"
  self.log_level = opts.delete(:log_level) || :warning

  @page = nil
  @error_checkers  = []
  @browser         = self # for Container#browser

  setup_webclient(opts)

  raise ArgumentError, "unknown option #{opts.inspect}" unless opts.empty?
  find_viewer
end

Class Attribute Details

.attach_timeoutObject

Added for Watir compatibility - not in use by Celerity



6
7
8
# File 'lib/celerity/watir_compatibility.rb', line 6

def attach_timeout
  @attach_timeout
end

.speedObject

Added for Watir compatibility - not in use by Celerity



6
7
8
# File 'lib/celerity/watir_compatibility.rb', line 6

def speed
  @speed
end

.visibleObject

Added for Watir compatibility - not in use by Celerity



6
7
8
# File 'lib/celerity/watir_compatibility.rb', line 6

def visible
  @visible
end

Instance Attribute Details

#charsetObject

Returns the value of attribute charset.



5
6
7
# File 'lib/celerity/browser.rb', line 5

def charset
  @charset
end

#objectObject

Returns the value of attribute object.



5
6
7
# File 'lib/celerity/browser.rb', line 5

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/celerity/browser.rb', line 6

def options
  @options
end

#pageObject

Returns the value of attribute page.



5
6
7
# File 'lib/celerity/browser.rb', line 5

def page
  @page
end

#viewerObject (readonly)

Returns the value of attribute viewer.



6
7
8
# File 'lib/celerity/browser.rb', line 6

def viewer
  @viewer
end

#visibleObject

Added for Watir compatibility - not in use by Celerity



22
23
24
# File 'lib/celerity/watir_compatibility.rb', line 22

def visible
  @visible
end

#webclientObject (readonly)

Returns the value of attribute webclient.



6
7
8
# File 'lib/celerity/browser.rb', line 6

def webclient
  @webclient
end

Class Method Details

.attach(*args) ⇒ Object

Not implemented. Use ClickableElement#click_and_attach instead.

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/celerity/browser.rb', line 25

def self.attach(*args)
  raise NotImplementedError, "use ClickableElement#click_and_attach instead"
end

.eachObject

Added for Watir compatibility - not in use by Celerity



12
# File 'lib/celerity/watir_compatibility.rb', line 12

def each; end

.quitObject

Added for Watir compatibility - not in use by Celerity



14
# File 'lib/celerity/watir_compatibility.rb', line 14

def quit; end

.reset_attach_timeoutObject

Added for Watir compatibility - not in use by Celerity



10
# File 'lib/celerity/watir_compatibility.rb', line 10

def reset_attach_timeout; @attach_timeout = 2.0; end

.set_fast_speedObject

Added for Watir compatibility - not in use by Celerity



16
# File 'lib/celerity/watir_compatibility.rb', line 16

def set_fast_speed; @speed = :fast; end

.set_slow_speedObject

Added for Watir compatibility - not in use by Celerity



18
# File 'lib/celerity/watir_compatibility.rb', line 18

def set_slow_speed; @speed = :slow; end

.start(uri) ⇒ Celerity::Browser Also known as: start_window

Initialize a browser and go to the given URL

Parameters:

  • uri (String)

    The URL to go to.

Returns:



15
16
17
18
19
# File 'lib/celerity/browser.rb', line 15

def self.start(uri)
  browser = new
  browser.goto(uri)
  browser
end

Instance Method Details

#add_checker(checker = nil) {|browser| ... } ⇒ Object

Add a ‘checker’ proc that will be run on every page load

Parameters:

  • checker (Proc) (defaults to: nil)

    The proc to be run (can also be given as a block)

Yield Parameters:

Raises:

  • (ArgumentError)

    if no Proc or block was given.



572
573
574
575
576
577
578
579
580
# File 'lib/celerity/browser.rb', line 572

def add_checker(checker = nil, &block)
  if block_given?
    @error_checkers << block
  elsif Proc === checker
    @error_checkers << checker
  else
    raise ArgumentError, "argument must be a Proc or block"
  end
end

Add a cookie with the given parameters (Celerity only)

Parameters:

  • domain (String)
  • name (String)
  • value (String)
  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :path (String) — default: "/"

    A path

  • :max_age (Fixnum) — default: ??

    A max age

  • :secure (Boolean) — default: false

Raises:

  • (ArgumentError)


363
364
365
366
367
368
369
370
371
372
# File 'lib/celerity/browser.rb', line 363

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(ArgumentError, "unknown option: #{opts.inspect}") unless opts.empty?

  cookie = Cookie.new(domain, name, value, path, max_age, secure)
  @webclient.getCookieManager.addCookie(cookie)
end

#add_listener(type, &block) ⇒ Object

Add a listener block for one of the available types. (Celerity only) Types map to HtmlUnit interfaces like this:

:status           => StatusHandler
:alert            => AlertHandler  ( window.alert() )
:web_window_event => WebWindowListener
:html_parser      => HTMLParserListener
:incorrectness    => IncorrectnessListener
:confirm          => ConfirmHandler ( window.confirm() )
:prompt           => PromptHandler ( window.prompt() )

Examples:

browser.add_listener(:status) { |page, message| ... }
browser.add_listener(:alert) { |page, message| ... }
browser.add_listener(:web_window_event) { |web_window_event| ... }
browser.add_listener(:html_parser) { |message, url, line, column, key| ... }
browser.add_listener(:incorrectness) { |message, origin| ... }
browser.add_listener(:confirm) { |page, message| ...; true }
browser.add_listener(:prompt) { |page, message| ... }

Parameters:

  • type (Symbol)

    One of the above symbols.

  • block (Proc)

    A block to be executed for events of this type.



542
543
544
# File 'lib/celerity/browser.rb', line 542

def add_listener(type, &block)
  listener.add_listener(type, &block)
end

#assert_existsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check that we have a @page object.

Raises:



743
744
745
# File 'lib/celerity/browser.rb', line 743

def assert_exists
  raise UnknownObjectException, "no page loaded" unless exist?
end

#asynchronized {|browser| ... } ⇒ Object

Allows you to temporarliy switch to HtmlUnit’s default AjaxController, so ajax calls are performed asynchronously. This is useful if you have created the Browser with :resynchronize => true, but want to switch it off temporarily.

Yield Parameters:

See Also:

  • #new


490
491
492
493
494
495
# File 'lib/celerity/browser.rb', line 490

def asynchronized(&block)
  old_controller = @webclient.ajaxController
  @webclient.setAjaxController(::HtmlUnit::AjaxController.new)
  yield self
  @webclient.setAjaxController(old_controller)
end

#backString

Goto back one history item

Returns:

  • (String)

    The url of the resulting page.



281
282
283
284
285
286
# File 'lib/celerity/browser.rb', line 281

def back
  @webclient.getCurrentWindow.getHistory.back
  refresh_page_from_window

  url
end

#bring_to_frontObject

Added for Watir compatibility - not in use by Celerity



25
# File 'lib/celerity/watir_compatibility.rb', line 25

def bring_to_front; true; end

#clear_cacheObject

Clears the cache of “compiled JavaScript files and parsed CSS snippets”



330
331
332
# File 'lib/celerity/browser.rb', line 330

def clear_cache
  @webclient.cache.clear
end

#clear_cookiesObject

Clears all cookies. (Celerity only)



322
323
324
# File 'lib/celerity/browser.rb', line 322

def clear_cookies
  @webclient.getCookieManager.clearCookies
end

#closeObject

Unsets the current page / closes all windows



123
124
125
126
# File 'lib/celerity/browser.rb', line 123

def close
  @page = nil
  @webclient.closeAllWindows
end

#confirm(bool, &block) ⇒ Object

Specify a boolean value to click either ‘OK’ or ‘Cancel’ in any confirm dialogs that might show up during the duration of the given block.

(Celerity only)

Parameters:

  • bool (Boolean)

    true to click ‘OK’, false to click ‘cancel’

  • block (Proc)

    A block that will trigger the confirm() call(s).



556
557
558
559
560
561
562
# File 'lib/celerity/browser.rb', line 556

def confirm(bool, &block)
  blk = lambda { bool }

  listener.add_listener(:confirm, &blk)
  yield
  listener.remove_listener(:confirm, blk)
end

#contains_text(expected_text) ⇒ Numeric?

Check if the current page contains the given text.

Parameters:

  • expected_text (String, Regexp)

    The text to look for.

Returns:

  • (Numeric, nil)

    The index of the matched text, or nil if it isn’t found.

Raises:

  • (TypeError)


236
237
238
239
# File 'lib/celerity/browser.rb', line 236

def contains_text(expected_text)
  return nil unless exist?
  super
end

#content_typeString

Returns content-type as in ‘text/html’.

Returns:

  • (String)

    content-type as in ‘text/html’



212
213
214
215
216
# File 'lib/celerity/browser.rb', line 212

def content_type
  return '' unless @page

  @page.getWebResponse.getContentType
end

#cookiesHash<domain, Hash<name, value>>

Get the cookies for this session. (Celerity only)

Returns:

  • (Hash<domain, Hash<name, value>>)


340
341
342
343
344
345
346
347
348
349
# File 'lib/celerity/browser.rb', line 340

def cookies
  result = Hash.new { |hash, key| hash[key] = {} }

  cookies = @webclient.getCookieManager.getCookies
  cookies.each do |cookie|
    result[cookie.getDomain][cookie.getName] = cookie.getValue
  end

  result
end

#credentials=(string) ⇒ Object

Set the credentials used for basic HTTP authentication. (Celerity only)

Example:

browser.credentials = "username:password"

Parameters:

  • A (String)

    string with username / password, separated by a colon



112
113
114
115
116
117
# File 'lib/celerity/browser.rb', line 112

def credentials=(string)
  user, pass = string.split(":")
  dcp = HtmlUnit::DefaultCredentialsProvider.new
  dcp.addCredentials(user, pass)
  @webclient.setCredentialsProvider(dcp)
end

#cssObject



682
683
684
# File 'lib/celerity/browser.rb', line 682

def css
  @webclient.cssEnabled
end

#css=(bool) ⇒ Object

Turn on/off CSS loading

Parameters:

  • (Bool)


678
679
680
# File 'lib/celerity/browser.rb', line 678

def css=(bool)
  @webclient.cssEnabled = bool
end

#debug_web_connection(name, &blk) ⇒ Object

Start or stop HtmlUnit’s DebuggingWebConnection. (Celerity only) The output will go to /tmp/«name»

Parameters:

  • name (String)

    directory name

  • blk (block)

    block to execute



505
506
507
508
509
510
511
512
513
# File 'lib/celerity/browser.rb', line 505

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

#disable_checker(checker) ⇒ Object

Remove the given checker from the list of checkers

Parameters:

  • checker (Proc)

    The Proc to disable.



587
588
589
# File 'lib/celerity/browser.rb', line 587

def disable_checker(checker)
  @error_checkers.delete(checker)
end

#documentHtmlUnit::HtmlHtml

Returns the underlying HtmlUnit document.

Returns:

  • (HtmlUnit::HtmlHtml)

    the underlying HtmlUnit document.



272
273
274
# File 'lib/celerity/browser.rb', line 272

def document
  @object
end

#element_by_xpath(xpath) ⇒ Celerity::Element

Get the first element found matching the given XPath.

Parameters:

  • xpath (String)

Returns:



248
249
250
251
252
# File 'lib/celerity/browser.rb', line 248

def element_by_xpath(xpath)
  assert_exists
  obj = @page.getFirstByXPath(xpath)
  element_from_dom_node(obj)
end

#elements_by_xpath(xpath) ⇒ Array<Celerity::Element>

Get all the elements matching the given XPath.

Parameters:

  • xpath (String)

Returns:



261
262
263
264
265
266
# File 'lib/celerity/browser.rb', line 261

def elements_by_xpath(xpath)
  assert_exists
  objects = @page.getByXPath(xpath)
  # should use an ElementCollection here?
  objects.map { |o| element_from_dom_node(o) }.compact
end

#execute_script(source) ⇒ Object

Execute the given JavaScript on the current page.

Returns:

  • (Object)

    The resulting Object



399
400
401
402
# File 'lib/celerity/browser.rb', line 399

def execute_script(source)
  assert_exists
  @page.executeJavaScript(source.to_s).getJavaScriptResult
end

#exist?true, false Also known as: exists?

Checks if we have a page currently loaded.

Returns:

  • (true, false)


639
640
641
# File 'lib/celerity/browser.rb', line 639

def exist?
  !!@page
end

#focused_elementObject

Returns the element that currently has the focus (Celerity only)



751
752
753
# File 'lib/celerity/browser.rb', line 751

def focused_element
  element_from_dom_node(page.getFocusedElement())
end

#forwardString

Go forward one history item

Returns:

  • (String)

    The url of the resulting page.



293
294
295
296
297
298
# File 'lib/celerity/browser.rb', line 293

def forward
  @webclient.getCurrentWindow.getHistory.forward
  refresh_page_from_window

  url
end

#goto(uri) ⇒ String

Goto the given URL

Parameters:

  • uri (String)

    The url.

Returns:

  • (String)

    The url.



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/celerity/browser.rb', line 90

def goto(uri)
  uri = "http://#{uri}" unless uri =~ %r{://}

  request = HtmlUnit::WebRequestSettings.new(::Java::JavaNet::URL.new(uri))
  request.setCharset(@charset)

  rescue_status_code_exception do
    self.page = @webclient.getPage(request)
  end

  url()
end

#htmlString

Returns the HTML content of the current page.

Returns:

  • (String)

    the HTML content of the current page



157
158
159
# File 'lib/celerity/browser.rb', line 157

def html
  @page ? @page.getWebResponse.getContentAsString(@charset) : ''
end

#ignore_pattern=(regexp) ⇒ Object

If a request is made to an URL that matches the pattern set here, Celerity will ignore the request and return an empty page with content type “text/html” instead.

This is useful to block unwanted requests (like ads/banners).



626
627
628
629
630
631
632
# File 'lib/celerity/browser.rb', line 626

def ignore_pattern=(regexp)
  unless regexp.kind_of?(Regexp)
    raise TypeError, "expected Regexp, got #{regexp.inspect}:#{regexp.class}"
  end

  Celerity::IgnoringWebConnection.new(@webclient, regexp)
end

#inspectObject



79
80
81
# File 'lib/celerity/browser.rb', line 79

def inspect
  short_inspect :exclude => %w[@webclient @browser @object @options]
end

#ioIO?

Returns page contents as an IO, returns nil if no page is loaded.

Returns:

  • (IO, nil)

    page contents as an IO, returns nil if no page is loaded.



222
223
224
225
226
# File 'lib/celerity/browser.rb', line 222

def io
  return nil unless @page

  @page.getWebResponse.getContentAsStream.to_io
end

#javascript_enabledObject



710
711
712
# File 'lib/celerity/browser.rb', line 710

def javascript_enabled
  @webclient.isJavaScriptEnabled
end

#javascript_enabled=(bool) ⇒ Object

Turn on/off JavaScript execution

Parameters:

  • (Bool)


706
707
708
# File 'lib/celerity/browser.rb', line 706

def javascript_enabled=(bool)
  @webclient.setJavaScriptEnabled(bool)
end

#javascript_exceptionsObject



654
655
656
# File 'lib/celerity/browser.rb', line 654

def javascript_exceptions
  @webclient.throwExceptionOnScriptError
end

#javascript_exceptions=(bool) ⇒ Object

Turn on/off javascript exceptions

Parameters:

  • (Bool)


650
651
652
# File 'lib/celerity/browser.rb', line 650

def javascript_exceptions=(bool)
  @webclient.throwExceptionOnScriptError = bool
end

#log_levelSymbol

:finest, :finer, :fine, :config, :info, :warning, :severe, or :off, :all

Returns:

  • (Symbol)

    the current log level



597
598
599
# File 'lib/celerity/browser.rb', line 597

def log_level
  Celerity::Util.logger_for('com.gargoylesoftware.htmlunit').level.to_s.downcase.to_sym
end

#log_level=(level) ⇒ Object

Set Java log level (default is :warning, can be any of :all, :finest, :finer, :fine, :config, :info, :warning, :severe, :off)

Parameters:

  • level (Symbol)

    The new log level.



607
608
609
610
611
612
613
614
615
616
617
# File 'lib/celerity/browser.rb', line 607

def log_level=(level)
  log_level = java.util.logging.Level.const_get(level.to_s.upcase)

  [ 'com.gargoylesoftware.htmlunit',
    'com.gargoylesoftware.htmlunit.html',
    'com.gargoylesoftware.htmlunit.javascript',
    'org.apache.commons.httpclient'
  ].each { |package| Celerity::Util.logger_for(package).level = log_level }

  level
end

#refreshObject

Refresh the current page



313
314
315
316
# File 'lib/celerity/browser.rb', line 313

def refresh
  assert_exists
  self.page = @page.refresh
end

Remove the cookie with the given domain and name (Celerity only)

Parameters:

  • domain (String)
  • name (String)

Raises:



383
384
385
386
387
388
389
390
391
392
# File 'lib/celerity/browser.rb', line 383

def remove_cookie(domain, name)
  cm = @webclient.getCookieManager
  cookie = cm.getCookies.find { |c| c.getDomain == domain && c.getName == name }

  if cookie.nil?
    raise CookieNotFoundError, "no cookie with domain #{domain.inspect} and name #{name.inspect}"
  end

  cm.removeCookie(cookie)
end

#response_headersHash

Returns response headers as a hash.

Returns:

  • (Hash)

    response headers as a hash



194
195
196
197
198
# File 'lib/celerity/browser.rb', line 194

def response_headers
  return {} unless @page

  Hash[*@page.getWebResponse.getResponseHeaders.map { |obj| [obj.name, obj.value] }.flatten]
end

#resynchronized {|browser| ... } ⇒ Object

Allows you to temporarily switch to HtmlUnit’s NicelyResynchronizingAjaxController to resynchronize ajax calls.

@browser.resynchronized do |b|
  b.link(:id, 'trigger_ajax_call').click
end

Yield Parameters:

See Also:

  • for how to configure the browser to always use this.


474
475
476
477
478
479
# File 'lib/celerity/browser.rb', line 474

def resynchronized(&block)
  old_controller = @webclient.ajaxController
  @webclient.setAjaxController(::HtmlUnit::NicelyResynchronizingAjaxController.new)
  yield self
  @webclient.setAjaxController(old_controller)
end

#secure_sslObject



696
697
698
# File 'lib/celerity/browser.rb', line 696

def secure_ssl
  !@webclient.useInsecureSSL
end

#secure_ssl=(bool) ⇒ Object

Turn on/off secure SSL

Parameters:

  • (Bool)


692
693
694
# File 'lib/celerity/browser.rb', line 692

def secure_ssl=(bool)
  @webclient.useInsecureSSL = !bool
end

#send_keys(keys) ⇒ Object

experimental - should be removed?



405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/celerity/browser.rb', line 405

def send_keys(keys)
  keys = keys.gsub(/\s*/, '').scan(/((?:\{[A-Z]+?\})|.)/u).flatten
  keys.each do |key|
    element = @page.getFocusedElement
    case key
    when "{TAB}"
      @page.tabToNextElement
    when /\w/
      element.type(key)
    else
      raise NotImplementedError
    end
  end
end

#speed=(s) ⇒ Object

Added for Watir compatibility - not in use by Celerity



27
# File 'lib/celerity/watir_compatibility.rb', line 27

def speed=(s); s end

#statusString

Returns the value of window.status.

Returns:

  • (String)

    the value of window.status



149
150
151
# File 'lib/celerity/browser.rb', line 149

def status
  execute_script "window.status" # avoid the listener overhead
end

#status_codeFixnum

Returns status code of the last request.

Returns:

  • (Fixnum)

    status code of the last request



204
205
206
# File 'lib/celerity/browser.rb', line 204

def status_code
  @page.getWebResponse.getStatusCode
end

#status_code_exceptionsObject



668
669
670
# File 'lib/celerity/browser.rb', line 668

def status_code_exceptions
  @webclient.throwExceptionOnFailingStatusCode
end

#status_code_exceptions=(bool) ⇒ Object

Turn on/off status code exceptions

Parameters:

  • (Bool)


664
665
666
# File 'lib/celerity/browser.rb', line 664

def status_code_exceptions=(bool)
  @webclient.throwExceptionOnFailingStatusCode = bool
end

#textString

Returns a text representation of the current page.

Returns:

  • (String)

    a text representation of the current page



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/celerity/browser.rb', line 175

def text
  return '' unless @page

  if @page.respond_to?("getContent")
    string = @page.getContent.strip
  elsif @page.documentElement
    string = @page.documentElement.asText.strip
  else
    string = ''
  end

  # Celerity::Util.normalize_text(string)
  string
end

#titleString

Returns the title of the current page.

Returns:

  • (String)

    the title of the current page



141
142
143
# File 'lib/celerity/browser.rb', line 141

def title
  @page ? @page.getTitleText : ''
end

#urlString

Returns the URL of the current page.

Returns:

  • (String)

    the URL of the current page



132
133
134
135
# File 'lib/celerity/browser.rb', line 132

def url
  assert_exists
  @page.getWebResponse.getRequestUrl.toString
end

#waitObject

Wait for javascript jobs to finish



304
305
306
307
# File 'lib/celerity/browser.rb', line 304

def wait
  assert_exists
  @webclient.waitForBackgroundJavaScript(10000);
end

#wait_until(timeout = 30) {|browser| ... } ⇒ Object

Wait until the given block evaluates to true (Celerity only)

Parameters:

  • timeout (Fixnum) (defaults to: 30)

    Number of seconds to wait before timing out (default: 30).

Yield Parameters:

See Also:



428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/celerity/browser.rb', line 428

def wait_until(timeout = 30, &block)
  returned = nil

  Timeout.timeout(timeout) do
    until returned = yield(self)
      refresh_page_from_window
      sleep 0.1
    end
  end

  returned
end

#wait_while(timeout = 30) {|browser| ... } ⇒ Object

Wait while the given block evaluates to true (Celerity only)

Parameters:

  • timeout (Fixnum) (defaults to: 30)

    Number of seconds to wait before timing out (default: 30).

Yield Parameters:

See Also:



449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/celerity/browser.rb', line 449

def wait_while(timeout = 30, &block)
  returned = nil

  Timeout.timeout(timeout) do
    while returned = yield(self)
      refresh_page_from_window
      sleep 0.1
    end
  end

  returned
end

#xmlString

Returns the XML representation of the DOM.

Returns:

  • (String)

    the XML representation of the DOM



165
166
167
168
169
# File 'lib/celerity/browser.rb', line 165

def xml
  return '' unless @page
  return @page.asXml if @page.respond_to?(:asXml)
  return text # fallback to text (for exampel for "plain/text" pages)
end