Class: Akephalos::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/akephalos/node.rb

Overview

Akephalos::Node wraps HtmlUnit’s DOMNode class, providing a simple API for interacting with an element on the page.

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Node

Returns a new instance of Node.

Parameters:

  • node (HtmlUnit::DOMNode)


7
8
9
10
# File 'lib/akephalos/node.rb', line 7

def initialize(node)
  @nodes = []
  @_node = node
end

Instance Method Details

#[](name) ⇒ String?

Return the value of the node’s attribute.

Parameters:

  • name (String)

    attribute on node

Returns:

  • (String)

    the value of the named attribute

  • (nil)

    when the node does not have the named attribute



27
28
29
# File 'lib/akephalos/node.rb', line 27

def [](name)
  @_node.hasAttribute(name.to_s) ? @_node.getAttribute(name.to_s) : nil
end

#checked?true, false

Returns whether the element is checked.

Returns:

  • (true, false)

    whether the element is checked



13
14
15
# File 'lib/akephalos/node.rb', line 13

def checked?
  @_node.isChecked
end

#clickObject

Click the node and then wait for any triggered JavaScript callbacks to fire.



115
116
117
118
119
# File 'lib/akephalos/node.rb', line 115

def click
  @_node.click
  @_node.getPage.getEnclosingWindow.getJobManager.waitForJobs(1000)
  @_node.getPage.getEnclosingWindow.getJobManager.waitForJobsStartingBefore(1000)
end

#find(selector) ⇒ Array<Node>

Search for child nodes which match the given XPath selector.

Parameters:

  • selector (String)

    an XPath selector

Returns:

  • (Array<Node>)

    the matched nodes



125
126
127
128
129
# File 'lib/akephalos/node.rb', line 125

def find(selector)
  nodes = @_node.getByXPath(selector).map { |node| Node.new(node) }
  @nodes << nodes
  nodes
end

#fire_event(name) ⇒ Object

Fire a JavaScript event on the current node. Note that you should not prefix event names with “on”, so:

link.fire_event('mousedown')

Parameters:

  • JavaScript (String)

    event name



98
99
100
# File 'lib/akephalos/node.rb', line 98

def fire_event(name)
  @_node.fireEvent(name)
end

#multiple_select?true, false

Returns whether the node allows multiple-option selection (if the node is a select).

Returns:

  • (true, false)

    whether the node allows multiple-option selection (if the node is a select).



67
68
69
# File 'lib/akephalos/node.rb', line 67

def multiple_select?
  !self[:multiple].nil?
end

#optionsArray<Node>

Return the option elements for a select box.

Returns:

  • (Array<Node>)

    the options



81
82
83
# File 'lib/akephalos/node.rb', line 81

def options
  @_node.getOptions.map { |node| Node.new(node) }
end

#selected_optionsArray<Node>

Return the selected option elements for a select box.

Returns:

  • (Array<Node>)

    the selected options



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

def selected_options
  @_node.getSelectedOptions.map { |node| Node.new(node) }
end

#tag_nameString

Returns the node’s tag name.

Returns:

  • (String)

    the node’s tag name



103
104
105
# File 'lib/akephalos/node.rb', line 103

def tag_name
  @_node.getNodeName
end

#textString

Returns inner text of the node.

Returns:

  • (String)

    inner text of the node



18
19
20
# File 'lib/akephalos/node.rb', line 18

def text
  @_node.asText
end

#unselecttrue, false

Unselect an option.

Returns:

  • (true, false)

    whether the unselection was successful



74
75
76
# File 'lib/akephalos/node.rb', line 74

def unselect
  @_node.setSelected(false)
end

#valueString+

Return the value of a form element. If the element is a select box and has “multiple” declared as an attribute, then all selected options will be returned as an array.

Returns:

  • (String, Array<String>)

    the node’s value



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/akephalos/node.rb', line 36

def value
  case tag_name
  when "select"
    if self[:multiple]
      selected_options.map { |option| option.value }
    else
      selected_option = @_node.selected_options.first
      selected_option ? Node.new(selected_option).value : nil
    end
  when "option"
    self[:value] || text
  when "textarea"
    @_node.getText
  else
    self[:value]
  end
end

#value=(value) ⇒ Object

Set the value of the form input.

Parameters:

  • value (String)


57
58
59
60
61
62
63
64
# File 'lib/akephalos/node.rb', line 57

def value=(value)
  case tag_name
  when "textarea"
    @_node.setText(value)
  when "input"
    @_node.setValueAttribute(value)
  end
end

#visible?true, false

for CSS.

Returns:

  • (true, false)

    whether the node is visible to the user accounting



109
110
111
# File 'lib/akephalos/node.rb', line 109

def visible?
  @_node.isDisplayed
end