Class: Akephalos::Page
- Inherits:
-
Object
- Object
- Akephalos::Page
- Defined in:
- lib/akephalos/page.rb
Overview
Akephalos::Page wraps HtmlUnit’s HtmlPage class, exposing an API for interacting with a page in the browser.
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Compare this page with an HtmlUnit page.
-
#current_url ⇒ String
The current page’s URL.
-
#evaluate_script(script) ⇒ Object
Execute JavaScript against the current page and return the results.
-
#execute_script(script) ⇒ nil
Execute JavaScript against the current page, discarding any return value.
-
#find(selector) ⇒ Array<Node>
Search for nodes which match the given XPath selector.
-
#initialize(page) ⇒ Page
constructor
A new instance of Page.
-
#modified_source ⇒ String
Return the page’s source, including any JavaScript-triggered DOM changes.
-
#response_headers ⇒ Hash{String => String}
The page’s response headers.
-
#source ⇒ String
Return the page’s source as returned by the web server.
-
#status_code ⇒ Integer
The response’s status code.
-
#within_frame(frame_id) ⇒ true?
Execute the given block in the context of the frame specified.
Constructor Details
#initialize(page) ⇒ Page
Returns a new instance of Page.
18 19 20 21 |
# File 'lib/akephalos/page.rb', line 18 def initialize(page) @nodes = [] @_page = page end |
Class Method Details
.new(*args) ⇒ Object
11 12 13 |
# File 'lib/akephalos/page.rb', line 11 def new(*args) ExceptionConvertingDelegator.new(new_orig(*args), "NativeException", RuntimeError) end |
.new_orig ⇒ Object
9 |
# File 'lib/akephalos/page.rb', line 9 alias_method :new_orig, :new |
Instance Method Details
#==(other) ⇒ true, false
Compare this page with an HtmlUnit page.
99 100 101 |
# File 'lib/akephalos/page.rb', line 99 def ==(other) @_page == other end |
#current_url ⇒ String
Returns the current page’s URL.
74 75 76 |
# File 'lib/akephalos/page.rb', line 74 def current_url current_frame.getWebResponse.getWebRequest.getUrl.toString end |
#evaluate_script(script) ⇒ Object
Execute JavaScript against the current page and return the results.
91 92 93 |
# File 'lib/akephalos/page.rb', line 91 def evaluate_script(script) current_frame.executeJavaScript(script).getJavaScriptResult end |
#execute_script(script) ⇒ nil
Execute JavaScript against the current page, discarding any return value.
82 83 84 85 |
# File 'lib/akephalos/page.rb', line 82 def execute_script(script) current_frame.executeJavaScript(script) nil end |
#find(selector) ⇒ Array<Node>
Search for nodes which match the given XPath selector.
27 28 29 30 31 |
# File 'lib/akephalos/page.rb', line 27 def find(selector) nodes = current_frame.getByXPath(selector).map { |node| Node.new(node) } @nodes << nodes nodes end |
#modified_source ⇒ String
Return the page’s source, including any JavaScript-triggered DOM changes.
36 37 38 |
# File 'lib/akephalos/page.rb', line 36 def modified_source current_frame.asXml end |
#response_headers ⇒ Hash{String => String}
Returns the page’s response headers.
48 49 50 51 52 53 |
# File 'lib/akephalos/page.rb', line 48 def response_headers headers = current_frame.getWebResponse.getResponseHeaders.map do |header| [header.getName, header.getValue] end Hash[*headers.flatten] end |
#source ⇒ String
Return the page’s source as returned by the web server.
43 44 45 |
# File 'lib/akephalos/page.rb', line 43 def source current_frame.getWebResponse.getContentAsString end |
#status_code ⇒ Integer
Returns the response’s status code.
56 57 58 |
# File 'lib/akephalos/page.rb', line 56 def status_code current_frame.getWebResponse.getStatusCode end |
#within_frame(frame_id) ⇒ true?
Execute the given block in the context of the frame specified.
65 66 67 68 69 70 71 |
# File 'lib/akephalos/page.rb', line 65 def within_frame(frame_id) return unless @current_frame = find_frame(frame_id) yield true ensure @current_frame = nil end |