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.
-
#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.
-
#options ⇒ Array<Node>
Return the option elements for a select box.
-
#select_option(option) ⇒ true, false
Select an option from a select box by its value.
-
#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.
-
#unselect_option(option) ⇒ true, false
Unselect an option from a select box by its value.
-
#value ⇒ String+
Return the value of a form element.
-
#value=(value) ⇒ Object
Set the value of the form input and then wait for any keypress events.
-
#visible? ⇒ true, false
for CSS.
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.
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.
13 14 15 |
# File 'lib/akephalos/node.rb', line 13 def checked? @_node.isChecked end |
#click ⇒ Object
Click the node and then wait for any triggered JavaScript callbacks to fire.
128 129 130 131 |
# File 'lib/akephalos/node.rb', line 128 def click @_node.click wait_for_jobs end |
#find(selector) ⇒ Array<Node>
Search for child nodes which match the given XPath selector.
137 138 139 140 141 |
# File 'lib/akephalos/node.rb', line 137 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')
110 111 112 113 |
# File 'lib/akephalos/node.rb', line 110 def fire_event(name) @_node.fireEvent(name) wait_for_jobs end |
#options ⇒ Array<Node>
Return the option elements for a select box.
93 94 95 |
# File 'lib/akephalos/node.rb', line 93 def @_node.getOptions.map { |node| Node.new(node) } end |
#select_option(option) ⇒ true, false
Select an option from a select box by its value.
75 76 77 78 79 |
# File 'lib/akephalos/node.rb', line 75 def select_option(option) opt = @_node.getOptions.detect { |o| o.asText == option } opt && opt.setSelected(true) end |
#selected_options ⇒ Array<Node>
Return the selected option elements for a select box.
100 101 102 |
# File 'lib/akephalos/node.rb', line 100 def @_node.getSelectedOptions.map { |node| Node.new(node) } end |
#tag_name ⇒ String
Returns the node’s tag name.
116 117 118 |
# File 'lib/akephalos/node.rb', line 116 def tag_name @_node.getNodeName end |
#text ⇒ String
Returns inner text of the node.
18 19 20 |
# File 'lib/akephalos/node.rb', line 18 def text @_node.asText end |
#unselect_option(option) ⇒ true, false
Unselect an option from a select box by its value.
84 85 86 87 88 |
# File 'lib/akephalos/node.rb', line 84 def unselect_option(option) opt = @_node.getOptions.detect { |o| o.asText == option } opt && opt.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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/akephalos/node.rb', line 36 def value case tag_name when "select" if self[:multiple] @_node..map { |option| option.text } else selected_option = @_node..first selected_option ? selected_option.text : nil end when "textarea" @_node.getText else self[:value] end end |
#value=(value) ⇒ Object
Set the value of the form input and then wait for any keypress events.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/akephalos/node.rb', line 55 def value=(value) case tag_name when "textarea" @_node.setText("") when "input" @_node.setValueAttribute("") if @_node.getAttribute("type") != "file" end if @_node.getAttribute("type") != "file" value.each_char do |c| @_node.type(c) end else @_node.setValueAttribute(value) end wait_for_jobs end |
#visible? ⇒ true, false
for CSS.
122 123 124 |
# File 'lib/akephalos/node.rb', line 122 def visible? @_node.isDisplayed end |