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

Returns a new instance of Page.

Parameters:

  • page (HtmlUnit::HtmlPage)


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.

Parameters:

  • other (HtmlUnit::HtmlPage)

    an HtmlUnit page

Returns:

  • (true, false)


88
89
90
# File 'lib/akephalos/page.rb', line 88

def ==(other)
  @_page == other
end

#current_urlString

Returns the current page’s URL.

Returns:

  • (String)

    the current page’s URL.



63
64
65
# File 'lib/akephalos/page.rb', line 63

def current_url
  current_frame.getWebResponse.getWebRequest.getUrl.toString
end

#evaluate_script(script) ⇒ Object

Execute JavaScript against the current page and return the results.

Parameters:

  • script (String)

    the JavaScript to be executed

Returns:

  • the result of the JavaScript



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.

Parameters:

  • script (String)

    the JavaScript to be executed

Returns:

  • (nil)


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.

Parameters:

  • selector (String)

    an XPath selector

Returns:

  • (Array<Node>)

    the matched nodes



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.

Returns:

  • (String)

    the page’s modified source



25
26
27
# File 'lib/akephalos/page.rb', line 25

def modified_source
  current_frame.asXml
end

#response_headersHash{String => String}

Returns the page’s response headers.

Returns:

  • (Hash{String => String})

    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

#sourceString

Return the page’s source as returned by the web server.

Returns:

  • (String)

    the page’s original source



32
33
34
# File 'lib/akephalos/page.rb', line 32

def source
  current_frame.getWebResponse.getContentAsString
end

#status_codeInteger

Returns the response’s status code.

Returns:

  • (Integer)

    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.

Parameters:

  • frame_id (String)

    the frame’s id

Returns:

  • (true)

    if the frame is found

  • (nil)

    if the frame is not found



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