Class: Ferrum::Node

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

Constant Summary collapse

MOVING_WAIT =
ENV.fetch("FERRUM_NODE_MOVING_WAIT", 0.01).to_f
MOVING_ATTEMPTS =
ENV.fetch("FERRUM_NODE_MOVING_ATTEMPTS", 50).to_i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frame, target_id, node_id, description) ⇒ Node

Returns a new instance of Node.



10
11
12
13
14
15
# File 'lib/ferrum/node.rb', line 10

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

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#node_idObject (readonly)

Returns the value of attribute node_id.



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

def node_id
  @node_id
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



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

def tag_name
  @tag_name
end

#target_idObject (readonly)

Returns the value of attribute target_id.



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

def target_id
  @target_id
end

Instance Method Details

#==(other) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/ferrum/node.rb', line 114

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



77
78
79
# File 'lib/ferrum/node.rb', line 77

def at_css(selector)
  page.at_css(selector, within: self)
end

#at_xpath(selector) ⇒ Object



73
74
75
# File 'lib/ferrum/node.rb', line 73

def at_xpath(selector)
  page.at_xpath(selector, within: self)
end

#attribute(name) ⇒ Object



106
107
108
# File 'lib/ferrum/node.rb', line 106

def attribute(name)
  evaluate("this.getAttribute('#{name}')")
end

#blurObject



33
34
35
# File 'lib/ferrum/node.rb', line 33

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ferrum/node.rb', line 44

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



85
86
87
# File 'lib/ferrum/node.rb', line 85

def css(selector)
  page.css(selector, within: self)
end

#evaluate(expression) ⇒ Object



110
111
112
# File 'lib/ferrum/node.rb', line 110

def evaluate(expression)
  page.evaluate_on(node: self, expression: expression)
end

#find_position(x: nil, y: nil, position: :top) ⇒ Object



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 126

def find_position(x: nil, y: nil, position: :top)
  prev = get_content_quads

  # FIXME: Case when a few quads returned
  points = Ferrum.with_attempts(errors: NodeIsMovingError, max: MOVING_ATTEMPTS, wait: 0) do
    sleep(MOVING_WAIT)
    current = get_content_quads

    if current != prev
      error = NodeIsMovingError.new(self, prev, current)
      prev = current
      raise(error)
    end

    current
  end.map { |q| to_points(q) }.first

  get_position(points, x, y, position)
rescue Ferrum::BrowserError => e
  return raise unless e.message&.include?("Could not compute content quads")

  find_position_via_js
end

#focusObject



29
30
31
# File 'lib/ferrum/node.rb', line 29

def focus
  tap { page.command("DOM.focus", slowmoable: true, nodeId: node_id) }
end

#frameObject



25
26
27
# File 'lib/ferrum/node.rb', line 25

def frame
  page.frame_by(id: frame_id)
end

#frame_idObject



21
22
23
# File 'lib/ferrum/node.rb', line 21

def frame_id
  description["frameId"]
end

#hoverObject



65
66
67
# File 'lib/ferrum/node.rb', line 65

def hover
  raise NotImplementedError
end

#inner_textObject

FIXME: clear API for text and inner_text



94
95
96
# File 'lib/ferrum/node.rb', line 94

def inner_text
  evaluate("this.innerText")
end

#inspectObject



122
123
124
# File 'lib/ferrum/node.rb', line 122

def inspect
  %(#<#{self.class} @target_id=#{@target_id.inspect} @node_id=#{@node_id} @description=#{@description.inspect}>)
end

#node?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ferrum/node.rb', line 17

def node?
  description["nodeType"] == 1 # nodeType: 3, nodeName: "#text" e.g.
end

#property(name) ⇒ Object



102
103
104
# File 'lib/ferrum/node.rb', line 102

def property(name)
  evaluate("this['#{name}']")
end

#select_file(value) ⇒ Object



69
70
71
# File 'lib/ferrum/node.rb', line 69

def select_file(value)
  page.command("DOM.setFileInputFiles", slowmoable: true, nodeId: node_id, files: Array(value))
end

#textObject



89
90
91
# File 'lib/ferrum/node.rb', line 89

def text
  evaluate("this.textContent")
end

#type(*keys) ⇒ Object



37
38
39
# File 'lib/ferrum/node.rb', line 37

def type(*keys)
  tap { page.keyboard.type(*keys) }
end

#valueObject



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

def value
  evaluate("this.value")
end

#xpath(selector) ⇒ Object



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

def xpath(selector)
  page.xpath(selector, within: self)
end