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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Page

Returns a new instance of Page.

Parameters:

  • page (HtmlUnit::HtmlPage)


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_origObject



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.

Parameters:

  • other (HtmlUnit::HtmlPage)

    an HtmlUnit page

Returns:

  • (true, false)


99
100
101
# File 'lib/akephalos/page.rb', line 99

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

#current_urlString

Returns the current page’s URL.

Returns:

  • (String)

    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.

Parameters:

  • script (String)

    the JavaScript to be executed

Returns:

  • the result of the JavaScript



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.

Parameters:

  • script (String)

    the JavaScript to be executed

Returns:

  • (nil)


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.

Parameters:

  • selector (String)

    an XPath selector

Returns:

  • (Array<Node>)

    the matched nodes



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_sourceString

Return the page’s source, including any JavaScript-triggered DOM changes.

Returns:

  • (String)

    the page’s modified source



36
37
38
# File 'lib/akephalos/page.rb', line 36

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



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

#sourceString

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

Returns:

  • (String)

    the page’s original source



43
44
45
# File 'lib/akephalos/page.rb', line 43

def source
  current_frame.getWebResponse.getContentAsString
end

#status_codeInteger

Returns the response’s status code.

Returns:

  • (Integer)

    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.

Parameters:

  • frame_id (String)

    the frame’s id

Returns:

  • (true)

    if the frame is found

  • (nil)

    if the frame is not found



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