Class: Akephalos::Node
- Inherits:
-
Object
- Object
- Akephalos::Node
- 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
-
#[](name) ⇒ String?
Return the value of the node’s attribute.
-
#checked? ⇒ true, false
Whether the element is checked.
-
#click ⇒ Object
Click the node and then wait for any triggered JavaScript callbacks to fire.
-
#file_input? ⇒ true, false
Whether the node is a file input.
-
#find(selector) ⇒ Array<Node>
Search for child nodes which match the given XPath selector.
-
#fire_event(name) ⇒ Object
Fire a JavaScript event on the current node.
-
#initialize(node) ⇒ Node
constructor
A new instance of Node.
-
#multiple_select? ⇒ true, false
Whether the node allows multiple-option selection (if the node is a select).
-
#options ⇒ Array<Node>
Return the option elements for a select box.
-
#selected? ⇒ true, false
for CSS.
-
#selected_options ⇒ Array<Node>
Return the selected option elements for a select box.
-
#tag_name ⇒ String
The node’s tag name.
-
#text ⇒ String
Inner text of the node.
-
#type(value) ⇒ Object
Types each character into a text or input field.
-
#unselect ⇒ true, false
Unselect an option.
-
#value ⇒ String+
Return the value of a form element.
-
#value=(value) ⇒ Object
Set the value of the form input.
-
#visible? ⇒ true, false
for CSS.
-
#xpath ⇒ String
The XPath expression for this node.
Constructor Details
#initialize(node) ⇒ Node
Returns a new instance of Node.
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.
31 32 33 |
# File 'lib/akephalos/node.rb', line 31 def [](name) @_node.hasAttribute(name.to_s) ? @_node.getAttribute(name.to_s) : nil end |
#checked? ⇒ true, false
Returns whether the element is checked.
13 14 15 16 17 18 19 |
# File 'lib/akephalos/node.rb', line 13 def checked? if @_node.respond_to?(:isChecked) @_node.isChecked else !! self[:checked] end end |
#click ⇒ Object
Click the node and then wait for any triggered JavaScript callbacks to fire.
150 151 152 153 154 |
# File 'lib/akephalos/node.rb', line 150 def click @_node.click @_node.getPage.getEnclosingWindow.getJobManager.waitForJobs(1000) @_node.getPage.getEnclosingWindow.getJobManager.waitForJobsStartingBefore(1000) end |
#file_input? ⇒ true, false
Returns whether the node is a file input.
91 92 93 |
# File 'lib/akephalos/node.rb', line 91 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.
160 161 162 163 164 |
# File 'lib/akephalos/node.rb', line 160 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')
123 124 125 |
# File 'lib/akephalos/node.rb', line 123 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).
86 87 88 |
# File 'lib/akephalos/node.rb', line 86 def multiple_select? !self[:multiple].nil? end |
#options ⇒ Array<Node>
Return the option elements for a select box.
106 107 108 |
# File 'lib/akephalos/node.rb', line 106 def @_node.getOptions.map { |node| Node.new(node) } end |
#selected? ⇒ true, false
for CSS.
140 141 142 143 144 145 146 |
# File 'lib/akephalos/node.rb', line 140 def selected? if @_node.respond_to?(:isSelected) @_node.isSelected else !! self[:selected] end end |
#selected_options ⇒ Array<Node>
Return the selected option elements for a select box.
113 114 115 |
# File 'lib/akephalos/node.rb', line 113 def @_node.getSelectedOptions.map { |node| Node.new(node) } end |
#tag_name ⇒ String
Returns the node’s tag name.
128 129 130 |
# File 'lib/akephalos/node.rb', line 128 def tag_name @_node.getNodeName end |
#text ⇒ String
Returns inner text of the node.
22 23 24 |
# File 'lib/akephalos/node.rb', line 22 def text @_node.asText end |
#type(value) ⇒ Object
Types each character into a text or input field.
79 80 81 82 83 |
# File 'lib/akephalos/node.rb', line 79 def type(value) value.each_char do |c| @_node.type(c) end end |
#unselect ⇒ true, false
Unselect an option.
99 100 101 |
# File 'lib/akephalos/node.rb', line 99 def unselect @_node.setSelected(false) end |
#value ⇒ String+
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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/akephalos/node.rb', line 40 def value case tag_name when "select" if self[:multiple] .map { |option| option.value } else selected_option = @_node..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.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/akephalos/node.rb', line 61 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.
134 135 136 |
# File 'lib/akephalos/node.rb', line 134 def visible? @_node.isDisplayed end |
#xpath ⇒ String
Returns the XPath expression for this node.
167 168 169 |
# File 'lib/akephalos/node.rb', line 167 def xpath @_node.getCanonicalXPath end |