Class: Ferrum::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Node.



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

def initialize(page, target_id, node_id, description)
  @page, @target_id = page, 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.



5
6
7
# File 'lib/ferrum/node.rb', line 5

def description
  @description
end

#node_idObject (readonly)

Returns the value of attribute node_id.



5
6
7
# File 'lib/ferrum/node.rb', line 5

def node_id
  @node_id
end

#pageObject (readonly)

Returns the value of attribute page.



5
6
7
# File 'lib/ferrum/node.rb', line 5

def page
  @page
end

#tag_nameObject (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_idObject (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



100
101
102
103
104
105
106
# File 'lib/ferrum/node.rb', line 100

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



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

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

#at_xpath(selector) ⇒ Object



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

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

#attribute(name) ⇒ Object



92
93
94
# File 'lib/ferrum/node.rb', line 92

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

#blurObject



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

def blur
  tap { evaluate("this.blur()") }
end

#click(mode: :left, keys: [], offset: {}) ⇒ Object

mode: (:left | :right | :double) keys: (:alt, (:ctrl | :control), (:meta | :command), :shift) offset: { :x, :y }



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ferrum/node.rb', line 32

def click(mode: :left, keys: [], offset: {})
  x, y = page.find_position(self, offset[:x], offset[:y])
  modifiers = page.keyboard.modifiers(keys)

  case mode
  when :right
    page.mouse.move(x: x, y: y)
    page.mouse.down(button: :right, modifiers: modifiers)
    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, timeout: 0.05)
  end

  self
end

#css(selector) ⇒ Object



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

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

#evaluate(expression) ⇒ Object



96
97
98
# File 'lib/ferrum/node.rb', line 96

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

#focusObject



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

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

#hoverObject



52
53
54
# File 'lib/ferrum/node.rb', line 52

def hover
  raise NotImplementedError
end

#inspectObject



108
109
110
# File 'lib/ferrum/node.rb', line 108

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

#node?Boolean

Returns:

  • (Boolean)


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

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

#property(name) ⇒ Object



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

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

#select_file(value) ⇒ Object



60
61
62
# File 'lib/ferrum/node.rb', line 60

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

#textObject



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

def text
  evaluate("this.textContent")
end

#trigger(event) ⇒ Object



56
57
58
# File 'lib/ferrum/node.rb', line 56

def trigger(event)
  raise NotImplementedError
end

#type(*keys) ⇒ Object



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

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

#valueObject



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

def value
  evaluate("this.value")
end

#xpath(selector) ⇒ Object



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

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