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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Node

Returns a new instance of Node.

Parameters:

  • node (HtmlUnit::DOMNode)


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

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

Class Method Details

.new(*args) ⇒ Object



11
12
13
# File 'lib/akephalos/node.rb', line 11

def new(*args)
  ExceptionConvertingDelegator.new(new_orig(*args), "NativeException", RuntimeError)
end

.new_origObject



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

alias_method :new_orig, :new

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



58
59
60
# File 'lib/akephalos/node.rb', line 58

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



24
25
26
27
28
29
30
# File 'lib/akephalos/node.rb', line 24

def checked?
  if @_node.respond_to?(:isChecked)
    @_node.isChecked
  else
    !! self[:checked]
  end
end

#clickObject

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



177
178
179
180
# File 'lib/akephalos/node.rb', line 177

def click
  @_node.click
  @_node.getPage.getWebClient.waitForBackgroundJavaScriptStartingBefore(3000)
end

#file_input?true, false

Returns whether the node is a file input.

Returns:

  • (true, false)

    whether the node is a file input



118
119
120
# File 'lib/akephalos/node.rb', line 118

def file_input?
  tag_name == "input" && @_node.getAttribute("type") == "file"
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



186
187
188
189
190
# File 'lib/akephalos/node.rb', line 186

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



150
151
152
# File 'lib/akephalos/node.rb', line 150

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).



113
114
115
# File 'lib/akephalos/node.rb', line 113

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

#optionsArray<Node>

Return the option elements for a select box.

Returns:

  • (Array<Node>)

    the options



133
134
135
# File 'lib/akephalos/node.rb', line 133

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

#selected?true, false

for CSS.

Returns:

  • (true, false)

    whether the node is selected to the user accounting



167
168
169
170
171
172
173
# File 'lib/akephalos/node.rb', line 167

def selected?
  if @_node.respond_to?(:isSelected)
    @_node.isSelected
  else
    !! self[:selected]
  end
end

#selected_optionsArray<Node>

Return the selected option elements for a select box.

Returns:

  • (Array<Node>)

    the selected options



140
141
142
# File 'lib/akephalos/node.rb', line 140

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



155
156
157
# File 'lib/akephalos/node.rb', line 155

def tag_name
  @_node.getNodeName
end

#textString

Returns a textual representation of this element that represents what would be visible to the user if this page was shown in a web browser. For example, a single-selection select element would return the currently selected value as text. Note: This will cleanup/reduce whitespace

Returns:

  • (String)

    inner text of the node



38
39
40
# File 'lib/akephalos/node.rb', line 38

def text
  @_node.asText
end

#text_contentObject

Returns the raw text content of this node and its descendants…



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

def text_content
  @_node.getTextContent
end

#type(value) ⇒ Object

Types each character into a text or input field.

Parameters:

  • value (String)

    the string to type



106
107
108
109
110
# File 'lib/akephalos/node.rb', line 106

def type(value)
  value.each_char do |c|
    @_node.type(c)
  end
end

#unselecttrue, false

Unselect an option.

Returns:

  • (true, false)

    whether the unselection was successful



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

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/akephalos/node.rb', line 67

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)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/akephalos/node.rb', line 88

def value=(value)
  case tag_name
  when "textarea"
    @_node.setText("")
    type(value)
  when "input"
    if file_input?
      @_node.setValueAttribute(value)
    else
      @_node.setValueAttribute("")
      type(value)
    end
  end
end

#visible?true, false

for CSS.

Returns:

  • (true, false)

    whether the node is visible to the user accounting



161
162
163
# File 'lib/akephalos/node.rb', line 161

def visible?
  @_node.isDisplayed
end

#xmlObject

Returns a string representation of the XML document from this element and all it’s children (recursively). The charset used is the current page encoding.



49
50
51
# File 'lib/akephalos/node.rb', line 49

def xml
  @_node.asXml
end

#xpathString

Returns the XPath expression for this node.

Returns:

  • (String)

    the XPath expression for this node



193
194
195
# File 'lib/akephalos/node.rb', line 193

def xpath
  @_node.getCanonicalXPath
end