Class: Akephalos::Page

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

Constructor Details

#initialize(page) ⇒ 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_urlString



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_sourceString

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_headersHash{String => String}



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

#sourceString

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_codeInteger



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