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.
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.
7 8 9 10 |
# File 'lib/akephalos/page.rb', line 7 def initialize(page) @nodes = [] @_page = page end |
Instance Method Details
#==(other) ⇒ true, false
Compare this page with an HtmlUnit page.
88 89 90 |
# File 'lib/akephalos/page.rb', line 88 def ==(other) @_page == other end |
#current_url ⇒ String
Returns the current page’s URL.
63 64 65 |
# File 'lib/akephalos/page.rb', line 63 def current_url current_frame.getWebResponse.getRequestSettings.getUrl.toString end |
#evaluate_script(script) ⇒ Object
Execute JavaScript against the current page and return the results.
80 81 82 |
# File 'lib/akephalos/page.rb', line 80 def evaluate_script(script) current_frame.executeJavaScript(script).getJavaScriptResult end |
#execute_script(script) ⇒ nil
Execute JavaScript against the current page, discarding any return value.
71 72 73 74 |
# File 'lib/akephalos/page.rb', line 71 def execute_script(script) current_frame.executeJavaScript(script) nil end |
#find(selector) ⇒ Array<Node>
Search for nodes which match the given XPath selector.
16 17 18 19 20 |
# File 'lib/akephalos/page.rb', line 16 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.
25 26 27 |
# File 'lib/akephalos/page.rb', line 25 def modified_source current_frame.asXml end |
#response_headers ⇒ Hash{String => String}
Returns the page’s response headers.
37 38 39 40 41 42 |
# File 'lib/akephalos/page.rb', line 37 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.
32 33 34 |
# File 'lib/akephalos/page.rb', line 32 def source current_frame.getWebResponse.getContentAsString end |
#status_code ⇒ Integer
Returns the response’s status code.
45 46 47 |
# File 'lib/akephalos/page.rb', line 45 def status_code current_frame.getWebResponse.getStatusCode end |
#within_frame(frame_id) ⇒ true?
Execute the given block in the context of the frame specified.
54 55 56 57 58 59 60 |
# File 'lib/akephalos/page.rb', line 54 def within_frame(frame_id) return unless @current_frame = find_frame(frame_id) yield true ensure @current_frame = nil end |