Class: Celerity::Browser
- Inherits:
-
Object
- Object
- Celerity::Browser
- Includes:
- Container, XpathSupport
- Defined in:
- lib/celerity/browser.rb,
lib/celerity/watir_compatibility.rb
Class Attribute Summary collapse
-
.attach_timeout ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
.speed ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
.visible ⇒ Object
Added for Watir compatibility - not in use by Celerity.
Instance Attribute Summary collapse
-
#charset ⇒ Object
Returns the value of attribute charset.
-
#object ⇒ Object
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#page ⇒ Object
Returns the value of attribute page.
-
#viewer ⇒ Object
readonly
Returns the value of attribute viewer.
-
#visible ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
#webclient ⇒ Object
readonly
Returns the value of attribute webclient.
Attributes included from Container
Class Method Summary collapse
-
.attach(*args) ⇒ Object
Not implemented.
-
.each ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
.quit ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
.reset_attach_timeout ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
.set_fast_speed ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
.set_slow_speed ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
.start(uri) ⇒ Celerity::Browser
(also: start_window)
Initialize a browser and go to the given URL.
Instance Method Summary collapse
-
#add_checker(checker = nil) {|browser| ... } ⇒ Object
Add a ‘checker’ proc that will be run on every page load.
-
#add_cookie(domain, name, value, opts = {}) ⇒ Object
Add a cookie with the given parameters (Celerity only).
-
#add_listener(type, &block) ⇒ Object
Add a listener block for one of the available types.
-
#assert_exists ⇒ Object
private
Check that we have a @page object.
-
#asynchronized {|browser| ... } ⇒ Object
Allows you to temporarliy switch to HtmlUnit’s default AjaxController, so ajax calls are performed asynchronously.
-
#back ⇒ String
Goto back one history item.
-
#bring_to_front ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
#clear_cache ⇒ Object
Clears the cache of “compiled JavaScript files and parsed CSS snippets”.
-
#clear_cookies ⇒ Object
Clears all cookies.
-
#close ⇒ Object
Unsets the current page / closes all windows.
-
#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.
-
#contains_text(expected_text) ⇒ Numeric?
Check if the current page contains the given text.
-
#content_type ⇒ String
Content-type as in ‘text/html’.
-
#cookies ⇒ Hash<domain, Hash<name, value>>
Get the cookies for this session.
-
#credentials=(string) ⇒ Object
Set the credentials used for basic HTTP authentication.
- #css ⇒ Object
-
#css=(bool) ⇒ Object
Turn on/off CSS loading.
-
#debug_web_connection(name, &blk) ⇒ Object
Start or stop HtmlUnit’s DebuggingWebConnection.
-
#disable_checker(checker) ⇒ Object
Remove the given checker from the list of checkers.
-
#disable_event_listener ⇒ Object
private
Disable Celerity’s internal WebWindowEventListener.
-
#document ⇒ HtmlUnit::HtmlHtml
The underlying HtmlUnit document.
-
#enable_event_listener ⇒ Object
private
Enable Celerity’s internal WebWindowEventListener.
-
#execute_script(source) ⇒ Object
Execute the given JavaScript on the current page.
-
#exist? ⇒ true, false
(also: #exists?)
Checks if we have a page currently loaded.
-
#focused_element ⇒ Object
Returns the element that currently has the focus (Celerity only).
-
#forward ⇒ String
Go forward one history item.
-
#goto(uri) ⇒ String
Goto the given URL.
-
#html ⇒ String
The HTML content of the current page.
-
#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.
-
#initialize(opts = {}) ⇒ Celerity::Browser
constructor
Creates a browser object.
- #inspect ⇒ Object
-
#io ⇒ IO?
Page contents as an IO, returns nil if no page is loaded.
- #javascript_enabled ⇒ Object
-
#javascript_enabled=(bool) ⇒ Object
Turn on/off JavaScript execution.
- #javascript_exceptions ⇒ Object
-
#javascript_exceptions=(bool) ⇒ Object
Turn on/off javascript exceptions.
-
#log_level ⇒ Symbol
:finest, :finer, :fine, :config, :info, :warning, :severe, or :off, :all.
-
#log_level=(level) ⇒ Object
Set Java log level (default is :warning, can be any of :all, :finest, :finer, :fine, :config, :info, :warning, :severe, :off).
-
#refresh ⇒ Object
Refresh the current page.
- #refresh_handler=(symbol) ⇒ Object
-
#remove_cookie(domain, name) ⇒ Object
Remove the cookie with the given domain and name (Celerity only).
- #remove_listener(type, &block) ⇒ Object
-
#response_headers ⇒ Hash
Response headers as a hash.
-
#resynchronized {|browser| ... } ⇒ Object
Allows you to temporarily switch to HtmlUnit’s NicelyResynchronizingAjaxController to resynchronize ajax calls.
- #secure_ssl ⇒ Object
-
#secure_ssl=(bool) ⇒ Object
Turn on/off secure SSL.
-
#send_keys(keys) ⇒ Object
experimental - should be removed?.
-
#speed=(s) ⇒ Object
Added for Watir compatibility - not in use by Celerity.
-
#status ⇒ String
The value of window.status.
-
#status_code ⇒ Fixnum
Status code of the last request.
- #status_code_exceptions ⇒ Object
-
#status_code_exceptions=(bool) ⇒ Object
Turn on/off status code exceptions.
-
#text ⇒ String
A text representation of the current page.
-
#title ⇒ String
The title of the current page.
-
#url ⇒ String
The URL of the current page.
-
#wait ⇒ Object
Wait for javascript jobs to finish.
-
#wait_until(timeout = 30) {|browser| ... } ⇒ Object
Wait until the given block evaluates to true (Celerity only).
-
#wait_while(timeout = 30) {|browser| ... } ⇒ Object
Wait while the given block evaluates to true (Celerity only).
-
#xml ⇒ String
The XML representation of the DOM.
Methods included from XpathSupport
#element_by_xpath, #element_from_dom_node, #elements_by_xpath
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
Methods included from ShortInspect
Constructor Details
#initialize(opts = {}) ⇒ Celerity::Browser
Creates a browser object.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/celerity/browser.rb', line 56 def initialize(opts = {}) unless opts.is_a?(Hash) raise TypeError, "wrong argument type #{opts.class}, expected Hash" end unless (render_types = [:html, :xml, nil, 'html', 'xml']).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 setup_viewer opts.delete(:viewer) raise ArgumentError, "unknown option #{opts.inspect}" unless opts.empty? end |
Class Attribute Details
.attach_timeout ⇒ Object
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 |
.speed ⇒ Object
Added for Watir compatibility - not in use by Celerity
6 7 8 |
# File 'lib/celerity/watir_compatibility.rb', line 6 def speed @speed end |
.visible ⇒ Object
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
#charset ⇒ Object
Returns the value of attribute charset.
6 7 8 |
# File 'lib/celerity/browser.rb', line 6 def charset @charset end |
#object ⇒ Object
Returns the value of attribute object.
6 7 8 |
# File 'lib/celerity/browser.rb', line 6 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/celerity/browser.rb', line 7 def @options end |
#page ⇒ Object
Returns the value of attribute page.
6 7 8 |
# File 'lib/celerity/browser.rb', line 6 def page @page end |
#viewer ⇒ Object (readonly)
Returns the value of attribute viewer.
7 8 9 |
# File 'lib/celerity/browser.rb', line 7 def viewer @viewer end |
#visible ⇒ Object
Added for Watir compatibility - not in use by Celerity
22 23 24 |
# File 'lib/celerity/watir_compatibility.rb', line 22 def visible @visible end |
#webclient ⇒ Object (readonly)
Returns the value of attribute webclient.
7 8 9 |
# File 'lib/celerity/browser.rb', line 7 def webclient @webclient end |
Class Method Details
.attach(*args) ⇒ Object
Not implemented. Use ClickableElement#click_and_attach instead.
26 27 28 |
# File 'lib/celerity/browser.rb', line 26 def self.attach(*args) raise NotImplementedError, "use ClickableElement#click_and_attach instead" end |
.each ⇒ Object
Added for Watir compatibility - not in use by Celerity
12 |
# File 'lib/celerity/watir_compatibility.rb', line 12 def each; end |
.quit ⇒ Object
Added for Watir compatibility - not in use by Celerity
14 |
# File 'lib/celerity/watir_compatibility.rb', line 14 def quit; end |
.reset_attach_timeout ⇒ Object
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_speed ⇒ Object
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_speed ⇒ Object
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
16 17 18 19 20 |
# File 'lib/celerity/browser.rb', line 16 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
553 554 555 556 557 558 559 560 561 |
# File 'lib/celerity/browser.rb', line 553 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_cookie(domain, name, value, opts = {}) ⇒ Object
Add a cookie with the given parameters (Celerity only)
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/celerity/browser.rb', line 340 def (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.new(domain, name, value, path, max_age, secure) @webclient.getCookieManager.addCookie() 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| ... }
519 520 521 |
# File 'lib/celerity/browser.rb', line 519 def add_listener(type, &block) listener.add_listener(type, &block) end |
#assert_exists ⇒ Object
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.
740 741 742 |
# File 'lib/celerity/browser.rb', line 740 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.
467 468 469 470 471 472 |
# File 'lib/celerity/browser.rb', line 467 def asynchronized(&block) old_controller = @webclient.ajaxController @webclient.setAjaxController(::HtmlUnit::AjaxController.new) yield self @webclient.setAjaxController(old_controller) end |
#back ⇒ String
Goto back one history item
258 259 260 261 262 263 |
# File 'lib/celerity/browser.rb', line 258 def back @webclient.getCurrentWindow.getHistory.back refresh_page_from_window url end |
#bring_to_front ⇒ Object
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_cache ⇒ Object
Clears the cache of “compiled JavaScript files and parsed CSS snippets”
307 308 309 |
# File 'lib/celerity/browser.rb', line 307 def clear_cache @webclient.cache.clear end |
#clear_cookies ⇒ Object
Clears all cookies. (Celerity only)
299 300 301 |
# File 'lib/celerity/browser.rb', line 299 def @webclient.getCookieManager.clearCookies end |
#close ⇒ Object
Unsets the current page / closes all windows
126 127 128 129 130 |
# File 'lib/celerity/browser.rb', line 126 def close @page = nil @webclient.closeAllWindows @viewer.close 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)
537 538 539 540 541 542 543 |
# File 'lib/celerity/browser.rb', line 537 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.
240 241 242 243 |
# File 'lib/celerity/browser.rb', line 240 def contains_text(expected_text) return nil unless exist? super end |
#content_type ⇒ String
Returns content-type as in ‘text/html’.
216 217 218 219 220 |
# File 'lib/celerity/browser.rb', line 216 def content_type return '' unless @page @page.getWebResponse.getContentType end |
#cookies ⇒ Hash<domain, Hash<name, value>>
Get the cookies for this session. (Celerity only)
317 318 319 320 321 322 323 324 325 326 |
# File 'lib/celerity/browser.rb', line 317 def result = Hash.new { |hash, key| hash[key] = {} } = @webclient.getCookieManager.getCookies .each do || result[.getDomain][.getName] = .getValue end result end |
#credentials=(string) ⇒ Object
Set the credentials used for basic HTTP authentication. (Celerity only)
Example:
browser.credentials = "username:password"
115 116 117 118 119 120 |
# File 'lib/celerity/browser.rb', line 115 def credentials=(string) user, pass = string.split(":") dcp = HtmlUnit::DefaultCredentialsProvider.new dcp.addCredentials(user, pass) @webclient.setCredentialsProvider(dcp) end |
#css ⇒ Object
663 664 665 |
# File 'lib/celerity/browser.rb', line 663 def css @webclient.cssEnabled end |
#css=(bool) ⇒ Object
Turn on/off CSS loading
659 660 661 |
# File 'lib/celerity/browser.rb', line 659 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»
482 483 484 485 486 487 488 489 490 |
# File 'lib/celerity/browser.rb', line 482 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
568 569 570 |
# File 'lib/celerity/browser.rb', line 568 def disable_checker(checker) @error_checkers.delete(checker) end |
#disable_event_listener ⇒ Object
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.
Disable Celerity’s internal WebWindowEventListener
772 773 774 775 776 777 778 779 780 781 |
# File 'lib/celerity/browser.rb', line 772 def disable_event_listener listener.remove_listener(:web_window_event, @event_listener) if block_given? result = yield enable_event_listener result end end |
#document ⇒ HtmlUnit::HtmlHtml
Returns the underlying HtmlUnit document.
249 250 251 |
# File 'lib/celerity/browser.rb', line 249 def document @object end |
#enable_event_listener ⇒ Object
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.
Enable Celerity’s internal WebWindowEventListener
758 759 760 761 762 763 764 |
# File 'lib/celerity/browser.rb', line 758 def enable_event_listener @event_listener ||= lambda do |event| self.page = @page ? @page.getEnclosingWindow.getEnclosedPage : event.getNewPage end listener.add_listener(:web_window_event, &@event_listener) end |
#execute_script(source) ⇒ Object
Execute the given JavaScript on the current page.
376 377 378 379 |
# File 'lib/celerity/browser.rb', line 376 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.
620 621 622 |
# File 'lib/celerity/browser.rb', line 620 def exist? !!@page end |
#focused_element ⇒ Object
Returns the element that currently has the focus (Celerity only)
748 749 750 |
# File 'lib/celerity/browser.rb', line 748 def focused_element element_from_dom_node(page.getFocusedElement()) end |
#forward ⇒ String
Go forward one history item
270 271 272 273 274 275 |
# File 'lib/celerity/browser.rb', line 270 def forward @webclient.getCurrentWindow.getHistory.forward refresh_page_from_window url end |
#goto(uri) ⇒ String
Goto the given URL
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/celerity/browser.rb', line 93 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 |
#html ⇒ String
Returns the HTML content of the current page.
161 162 163 |
# File 'lib/celerity/browser.rb', line 161 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).
607 608 609 610 611 612 613 |
# File 'lib/celerity/browser.rb', line 607 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 |
#inspect ⇒ Object
82 83 84 |
# File 'lib/celerity/browser.rb', line 82 def inspect short_inspect :exclude => %w[@webclient @browser @object @options @listener @event_listener] end |
#io ⇒ IO?
Returns page contents as an IO, returns nil if no page is loaded.
226 227 228 229 230 |
# File 'lib/celerity/browser.rb', line 226 def io return nil unless @page @page.getWebResponse.getContentAsStream.to_io end |
#javascript_enabled ⇒ Object
706 707 708 |
# File 'lib/celerity/browser.rb', line 706 def javascript_enabled @webclient.isJavaScriptEnabled end |
#javascript_enabled=(bool) ⇒ Object
Turn on/off JavaScript execution
702 703 704 |
# File 'lib/celerity/browser.rb', line 702 def javascript_enabled=(bool) @webclient.setJavaScriptEnabled(bool) end |
#javascript_exceptions ⇒ Object
635 636 637 |
# File 'lib/celerity/browser.rb', line 635 def javascript_exceptions @webclient.throwExceptionOnScriptError end |
#javascript_exceptions=(bool) ⇒ Object
Turn on/off javascript exceptions
631 632 633 |
# File 'lib/celerity/browser.rb', line 631 def javascript_exceptions=(bool) @webclient.throwExceptionOnScriptError = bool end |
#log_level ⇒ Symbol
:finest, :finer, :fine, :config, :info, :warning, :severe, or :off, :all
578 579 580 |
# File 'lib/celerity/browser.rb', line 578 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)
588 589 590 591 592 593 594 595 596 597 598 |
# File 'lib/celerity/browser.rb', line 588 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 |
#refresh ⇒ Object
Refresh the current page
290 291 292 293 |
# File 'lib/celerity/browser.rb', line 290 def refresh assert_exists @page.refresh end |
#refresh_handler=(symbol) ⇒ Object
667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
# File 'lib/celerity/browser.rb', line 667 def refresh_handler=(symbol) handler = case symbol when :waiting HtmlUnit::WaitingRefreshHandler.new when :threaded HtmlUnit::ThreadedRefreshHandler.new when :immediate HtmlUnit::ImmediateRefreshHandler.new else raise ArgumentError, "expected :waiting, :threaded or :immediate" end @webclient.setRefreshHandler handler end |
#remove_cookie(domain, name) ⇒ Object
Remove the cookie with the given domain and name (Celerity only)
360 361 362 363 364 365 366 367 368 369 |
# File 'lib/celerity/browser.rb', line 360 def (domain, name) cm = @webclient.getCookieManager = cm.getCookies.find { |c| c.getDomain == domain && c.getName == name } if .nil? raise CookieNotFoundError, "no cookie with domain #{domain.inspect} and name #{name.inspect}" end cm.removeCookie() end |
#remove_listener(type, &block) ⇒ Object
523 524 525 |
# File 'lib/celerity/browser.rb', line 523 def remove_listener(type, &block) listener.remove_listener(type, &block) end |
#response_headers ⇒ Hash
Returns response headers as a hash.
198 199 200 201 202 |
# File 'lib/celerity/browser.rb', line 198 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
451 452 453 454 455 456 |
# File 'lib/celerity/browser.rb', line 451 def resynchronized(&block) old_controller = @webclient.ajaxController @webclient.setAjaxController(::HtmlUnit::NicelyResynchronizingAjaxController.new) yield self @webclient.setAjaxController(old_controller) end |
#secure_ssl ⇒ Object
692 693 694 |
# File 'lib/celerity/browser.rb', line 692 def secure_ssl !@webclient.useInsecureSSL end |
#secure_ssl=(bool) ⇒ Object
Turn on/off secure SSL
688 689 690 |
# File 'lib/celerity/browser.rb', line 688 def secure_ssl=(bool) @webclient.useInsecureSSL = !bool end |
#send_keys(keys) ⇒ Object
experimental - should be removed?
382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/celerity/browser.rb', line 382 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 |
#status ⇒ String
Returns the value of window.status.
153 154 155 |
# File 'lib/celerity/browser.rb', line 153 def status execute_script "window.status" # avoid the listener overhead end |
#status_code ⇒ Fixnum
Returns status code of the last request.
208 209 210 |
# File 'lib/celerity/browser.rb', line 208 def status_code @page.getWebResponse.getStatusCode end |
#status_code_exceptions ⇒ Object
649 650 651 |
# File 'lib/celerity/browser.rb', line 649 def status_code_exceptions @webclient.throwExceptionOnFailingStatusCode end |
#status_code_exceptions=(bool) ⇒ Object
Turn on/off status code exceptions
645 646 647 |
# File 'lib/celerity/browser.rb', line 645 def status_code_exceptions=(bool) @webclient.throwExceptionOnFailingStatusCode = bool end |
#text ⇒ String
Returns a text representation of the current page.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/celerity/browser.rb', line 179 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 |
#title ⇒ String
Returns the title of the current page.
145 146 147 |
# File 'lib/celerity/browser.rb', line 145 def title @page ? @page.getTitleText : '' end |
#url ⇒ String
Returns the URL of the current page.
136 137 138 139 |
# File 'lib/celerity/browser.rb', line 136 def url assert_exists @page.getWebResponse.getRequestUrl.toString end |
#wait ⇒ Object
Wait for javascript jobs to finish
281 282 283 284 |
# File 'lib/celerity/browser.rb', line 281 def wait assert_exists @webclient.waitForBackgroundJavaScript(10000); end |
#wait_until(timeout = 30) {|browser| ... } ⇒ Object
Wait until the given block evaluates to true (Celerity only)
405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/celerity/browser.rb', line 405 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)
426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/celerity/browser.rb', line 426 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 |
#xml ⇒ String
Returns the XML representation of the DOM.
169 170 171 172 173 |
# File 'lib/celerity/browser.rb', line 169 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 |