Class: Ferrum::Node
- Inherits:
-
Object
- Object
- Ferrum::Node
- Defined in:
- lib/ferrum/node.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#node_id ⇒ Object
readonly
Returns the value of attribute node_id.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#tag_name ⇒ Object
readonly
Returns the value of attribute tag_name.
-
#target_id ⇒ Object
readonly
Returns the value of attribute target_id.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #at_css(selector) ⇒ Object
- #at_xpath(selector) ⇒ Object
- #attribute(name) ⇒ Object
- #blur ⇒ Object
-
#click(mode: :left, keys: [], offset: {}, delay: 0) ⇒ Object
mode: (:left | :right | :double) keys: (:alt, (:ctrl | :control), (:meta | :command), :shift) offset: { :x, :y, :position (:top | :center) }.
- #css(selector) ⇒ Object
- #evaluate(expression) ⇒ Object
- #find_position(x: nil, y: nil, position: :top) ⇒ Object
- #focus ⇒ Object
- #frame ⇒ Object
- #frame_id ⇒ Object
- #hover ⇒ Object
-
#initialize(frame, target_id, node_id, description) ⇒ Node
constructor
A new instance of Node.
-
#inner_text ⇒ Object
FIXME: clear API for text and inner_text.
- #inspect ⇒ Object
- #node? ⇒ Boolean
- #property(name) ⇒ Object
- #select_file(value) ⇒ Object
- #text ⇒ Object
- #type(*keys) ⇒ Object
- #value ⇒ Object
- #xpath(selector) ⇒ Object
Constructor Details
#initialize(frame, target_id, node_id, description) ⇒ Node
Returns a new instance of Node.
7 8 9 10 11 12 |
# File 'lib/ferrum/node.rb', line 7 def initialize(frame, target_id, node_id, description) @page = frame.page @target_id = target_id @node_id, @description = node_id, description @tag_name = description["nodeName"].downcase end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
5 6 7 |
# File 'lib/ferrum/node.rb', line 5 def description @description end |
#node_id ⇒ Object (readonly)
Returns the value of attribute node_id.
5 6 7 |
# File 'lib/ferrum/node.rb', line 5 def node_id @node_id end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
5 6 7 |
# File 'lib/ferrum/node.rb', line 5 def page @page end |
#tag_name ⇒ Object (readonly)
Returns the value of attribute tag_name.
5 6 7 |
# File 'lib/ferrum/node.rb', line 5 def tag_name @tag_name end |
#target_id ⇒ Object (readonly)
Returns the value of attribute target_id.
5 6 7 |
# File 'lib/ferrum/node.rb', line 5 def target_id @target_id end |
Instance Method Details
#==(other) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/ferrum/node.rb', line 111 def ==(other) return false unless other.is_a?(Node) # We compare backendNodeId because once nodeId is sent to frontend backend # never returns same nodeId sending 0. In other words frontend is # responsible for keeping track of node ids. target_id == other.target_id && description["backendNodeId"] == other.description["backendNodeId"] end |
#at_css(selector) ⇒ Object
74 75 76 |
# File 'lib/ferrum/node.rb', line 74 def at_css(selector) page.at_css(selector, within: self) end |
#at_xpath(selector) ⇒ Object
70 71 72 |
# File 'lib/ferrum/node.rb', line 70 def at_xpath(selector) page.at_xpath(selector, within: self) end |
#attribute(name) ⇒ Object
103 104 105 |
# File 'lib/ferrum/node.rb', line 103 def attribute(name) evaluate("this.getAttribute('#{name}')") end |
#blur ⇒ Object
30 31 32 |
# File 'lib/ferrum/node.rb', line 30 def blur tap { evaluate("this.blur()") } end |
#click(mode: :left, keys: [], offset: {}, delay: 0) ⇒ Object
mode: (:left | :right | :double) keys: (:alt, (:ctrl | :control), (:meta | :command), :shift) offset: { :x, :y, :position (:top | :center) }
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ferrum/node.rb', line 41 def click(mode: :left, keys: [], offset: {}, delay: 0) x, y = find_position(**offset) modifiers = page.keyboard.modifiers(keys) case mode when :right page.mouse.move(x: x, y: y) page.mouse.down(button: :right, modifiers: modifiers) sleep(delay) page.mouse.up(button: :right, modifiers: modifiers) when :double page.mouse.move(x: x, y: y) page.mouse.down(modifiers: modifiers, count: 2) page.mouse.up(modifiers: modifiers, count: 2) when :left page.mouse.click(x: x, y: y, modifiers: modifiers, delay: delay) end self end |
#css(selector) ⇒ Object
82 83 84 |
# File 'lib/ferrum/node.rb', line 82 def css(selector) page.css(selector, within: self) end |
#evaluate(expression) ⇒ Object
107 108 109 |
# File 'lib/ferrum/node.rb', line 107 def evaluate(expression) page.evaluate_on(node: self, expression: expression) end |
#find_position(x: nil, y: nil, position: :top) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/ferrum/node.rb', line 123 def find_position(x: nil, y: nil, position: :top) offset_x, offset_y = x, y quads = get_content_quads x = y = nil if offset_x && offset_y && position == :top point = quads.first x = point[:x] + offset_x.to_i y = point[:y] + offset_y.to_i else x, y = quads.inject([0, 0]) do |memo, point| [memo[0] + point[:x], memo[1] + point[:y]] end x = x / 4 y = y / 4 end if offset_x && offset_y && position == :center x = x + offset_x.to_i y = y + offset_y.to_i end [x, y] end |
#focus ⇒ Object
26 27 28 |
# File 'lib/ferrum/node.rb', line 26 def focus tap { page.command("DOM.focus", slowmoable: true, nodeId: node_id) } end |
#frame ⇒ Object
22 23 24 |
# File 'lib/ferrum/node.rb', line 22 def frame page.frame_by(id: frame_id) end |
#frame_id ⇒ Object
18 19 20 |
# File 'lib/ferrum/node.rb', line 18 def frame_id description["frameId"] end |
#hover ⇒ Object
62 63 64 |
# File 'lib/ferrum/node.rb', line 62 def hover raise NotImplementedError end |
#inner_text ⇒ Object
FIXME: clear API for text and inner_text
91 92 93 |
# File 'lib/ferrum/node.rb', line 91 def inner_text evaluate("this.innerText") end |
#inspect ⇒ Object
119 120 121 |
# File 'lib/ferrum/node.rb', line 119 def inspect %(#<#{self.class} @target_id=#{@target_id.inspect} @node_id=#{@node_id} @description=#{@description.inspect}>) end |
#node? ⇒ Boolean
14 15 16 |
# File 'lib/ferrum/node.rb', line 14 def node? description["nodeType"] == 1 # nodeType: 3, nodeName: "#text" e.g. end |
#property(name) ⇒ Object
99 100 101 |
# File 'lib/ferrum/node.rb', line 99 def property(name) evaluate("this['#{name}']") end |
#select_file(value) ⇒ Object
66 67 68 |
# File 'lib/ferrum/node.rb', line 66 def select_file(value) page.command("DOM.setFileInputFiles", slowmoable: true, nodeId: node_id, files: Array(value)) end |
#text ⇒ Object
86 87 88 |
# File 'lib/ferrum/node.rb', line 86 def text evaluate("this.textContent") end |
#type(*keys) ⇒ Object
34 35 36 |
# File 'lib/ferrum/node.rb', line 34 def type(*keys) tap { page.keyboard.type(*keys) } end |
#value ⇒ Object
95 96 97 |
# File 'lib/ferrum/node.rb', line 95 def value evaluate("this.value") end |
#xpath(selector) ⇒ Object
78 79 80 |
# File 'lib/ferrum/node.rb', line 78 def xpath(selector) page.xpath(selector, within: self) end |