Class: Capybara::Cuprite::Node

Inherits:
Driver::Node
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/capybara/cuprite/node.rb

Constant Summary collapse

DRAG_MODIFIER_ALIASES =
{ control: :ctrl, command: :meta, cmd: :meta }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver, node) ⇒ Node

Returns a new instance of Node.



17
18
19
20
# File 'lib/capybara/cuprite/node.rb', line 17

def initialize(driver, node)
  super(driver, self)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

Instance Method Details

#==(other) ⇒ Object



227
228
229
# File 'lib/capybara/cuprite/node.rb', line 227

def ==(other)
  node == other.native.node
end

#[](name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/capybara/cuprite/node.rb', line 73

def [](name)
  # Although the attribute matters, the property is consistent. Return that in
  # preference to the attribute for links and images.
  if (tag_name == "img" && name == "src") ||
     (tag_name == "a" && name == "href")
    # if attribute exists get the property
    return command(:attribute, name) && command(:property, name)
  end

  value = property(name)
  value = command(:attribute, name) if value.nil? || value.is_a?(Hash)

  value
end

#all_textObject



57
58
59
# File 'lib/capybara/cuprite/node.rb', line 57

def all_text
  filter_text(command(:all_text))
end

#as_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



270
271
272
273
# File 'lib/capybara/cuprite/node.rb', line 270

def as_json(*)
  # FIXME: Where is this method used and why attr is called id?
  { ELEMENT: { node: node, id: node.node_id } }
end

#attributesObject



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

def attributes
  command(:attributes)
end

#checked?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/capybara/cuprite/node.rb', line 155

def checked?
  self[:checked]
end

#click(keys = [], **options) ⇒ Object



167
168
169
# File 'lib/capybara/cuprite/node.rb', line 167

def click(keys = [], **options)
  prepare_and_click(:left, __method__, keys, options)
end

#command(name, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/capybara/cuprite/node.rb', line 22

def command(name, *args)
  raise ObsoleteNode.new(self, nil) unless node.evaluate("this.isConnected")

  browser.send(name, node, *args)
rescue Ferrum::NodeNotFoundError => e
  raise ObsoleteNode.new(self, e.response)
rescue Ferrum::BrowserError => e
  case e.message
  when "Cuprite.MouseEventFailed"
    raise MouseEventFailed.new(self, e.response)
  else
    raise
  end
end

#disabled?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/capybara/cuprite/node.rb', line 163

def disabled?
  command(:disabled?)
end

#double_click(keys = [], **options) ⇒ Object



175
176
177
# File 'lib/capybara/cuprite/node.rb', line 175

def double_click(keys = [], **options)
  prepare_and_click(:double, __method__, keys, options)
end

#drag_by(x, y, **options) ⇒ Object



191
192
193
194
195
196
# File 'lib/capybara/cuprite/node.rb', line 191

def drag_by(x, y, **options)
  options[:steps] ||= 1
  options[:scroll] = true unless options.key?(:scroll)

  command(:drag_by, x, y, options[:steps], options[:delay], options[:scroll])
end

#drag_to(other, **options) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/capybara/cuprite/node.rb', line 183

def drag_to(other, **options)
  options[:steps] ||= 1
  options[:scroll] = true unless options.key?(:scroll)
  modifiers = Array(options[:drop_modifiers]).map { |m| DRAG_MODIFIER_ALIASES.fetch(m.to_sym, m.to_sym) }

  command(:drag, other.node, modifiers, options.slice(:steps, :delay, :scroll, :html5))
end

#dropObject



198
# File 'lib/capybara/cuprite/node.rb', line 198

def drop(...) = command(:drop, ...)

#find(method, selector) ⇒ Object



51
52
53
54
55
# File 'lib/capybara/cuprite/node.rb', line 51

def find(method, selector)
  command(:find_within, method, selector).map do |node|
    self.class.new(driver, node)
  end
end

#find_css(selector) ⇒ Object



47
48
49
# File 'lib/capybara/cuprite/node.rb', line 47

def find_css(selector)
  find(:css, selector)
end

#find_xpath(selector) ⇒ Object



43
44
45
# File 'lib/capybara/cuprite/node.rb', line 43

def find_xpath(selector)
  find(:xpath, selector)
end

#hoverObject



179
180
181
# File 'lib/capybara/cuprite/node.rb', line 179

def hover
  command(:hover)
end

#inspectObject



260
261
262
# File 'lib/capybara/cuprite/node.rb', line 260

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

#obscured?Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/capybara/cuprite/node.rb', line 243

def obscured?
  command(:obscured?)
end

#parentsObject



37
38
39
40
41
# File 'lib/capybara/cuprite/node.rb', line 37

def parents
  command(:parents).map do |parent|
    self.class.new(driver, parent)
  end
end

#pathObject



239
240
241
# File 'lib/capybara/cuprite/node.rb', line 239

def path
  command(:path)
end

#property(name) ⇒ Object



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

def property(name)
  command(:property, name)
end

#rectObject



254
255
256
257
258
# File 'lib/capybara/cuprite/node.rb', line 254

def rect
  driver.evaluate_script <<~JS, self
    arguments[0].getBoundingClientRect().toJSON()
  JS
end

#right_click(keys = [], **options) ⇒ Object



171
172
173
# File 'lib/capybara/cuprite/node.rb', line 171

def right_click(keys = [], **options)
  prepare_and_click(:right, __method__, keys, options)
end

#scroll_by(x, y) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/capybara/cuprite/node.rb', line 215

def scroll_by(x, y)
  driver.execute_script <<~JS, self, x, y
    var el = arguments[0];
    if (el.scrollBy){
      el.scrollBy(arguments[1], arguments[2]);
    } else {
      el.scrollTop = el.scrollTop + arguments[2];
      el.scrollLeft = el.scrollLeft + arguments[1];
    }
  JS
end

#scroll_to(element, location, position = nil) ⇒ Object



204
205
206
207
208
209
210
211
212
213
# File 'lib/capybara/cuprite/node.rb', line 204

def scroll_to(element, location, position = nil)
  if element.is_a?(Node)
    scroll_element_to_location(element, location)
  elsif location.is_a?(Symbol)
    scroll_to_location(location)
  else
    scroll_to_coords(*position)
  end
  self
end

#select_optionObject



138
139
140
# File 'lib/capybara/cuprite/node.rb', line 138

def select_option
  command(:select, true)
end

#selected?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/capybara/cuprite/node.rb', line 159

def selected?
  !!self[:selected]
end

#send_keys(*keys) ⇒ Object Also known as: send_key



231
232
233
234
235
236
# File 'lib/capybara/cuprite/node.rb', line 231

def send_keys(*keys)
  keys = keys.reject { |key| key.nil? || key == "" }
  return if keys.empty?

  command(:send_keys, keys)
end

#set(value, options = {}) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/capybara/cuprite/node.rb', line 96

def set(value, options = {}) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
  warn "Options passed to Node#set but Cuprite doesn't currently support any - ignoring" unless options.empty?

  if tag_name == "input"
    case self[:type]
    when "radio"
      click
    when "checkbox"
      click if value != checked?
    when "file"
      files = value.respond_to?(:to_ary) ? value.to_ary.map(&:to_s) : value.to_s
      command(:select_file, files)
    when "color"
      node.evaluate("this.setAttribute('value', '#{value}')")
      node.evaluate("this.dispatchEvent(new InputEvent('input'))")
      node.evaluate("this.dispatchEvent(new Event('change', { bubbles: true }))")
    when "date"
      value = value.to_date.iso8601 if !value.is_a?(String) && value.respond_to?(:to_date)
      command(:set, value.to_s)
    when "time"
      value = value.to_time.strftime("%H:%M") if !value.is_a?(String) && value.respond_to?(:to_time)
      command(:set, value.to_s)
    when "month"
      value = value.to_date.strftime("%Y-%m") if !value.is_a?(String) && value.respond_to?(:to_date)
      command(:set, value.to_s)
    when "week"
      value = value.to_date.strftime("%G-W%V") if !value.is_a?(String) && value.respond_to?(:to_date)
      command(:set, value.to_s)
    when "datetime-local"
      value = value.to_time.strftime("%Y-%m-%dT%H:%M") if !value.is_a?(String) && value.respond_to?(:to_time)
      command(:set, value.to_s)
    else
      command(:set, value.to_s)
    end
  elsif tag_name == "textarea"
    command(:set, value.to_s)
  elsif self[:isContentEditable]
    command(:delete_text)
    click.type(value.to_s)
  end
end

#shadow_rootObject



247
248
249
250
251
252
# File 'lib/capybara/cuprite/node.rb', line 247

def shadow_root
  root = driver.evaluate_script <<~JS, self
    arguments[0].shadowRoot
  JS
  root && self.class.new(driver, root.node)
end

#tag_nameObject



147
148
149
# File 'lib/capybara/cuprite/node.rb', line 147

def tag_name
  @tag_name ||= description["shadowRootType"] ? "ShadowRoot" : description["nodeName"].downcase
end

#to_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



265
266
267
# File 'lib/capybara/cuprite/node.rb', line 265

def to_json(*)
  JSON.generate(as_json)
end

#trigger(event) ⇒ Object



200
201
202
# File 'lib/capybara/cuprite/node.rb', line 200

def trigger(event)
  command(:trigger, event)
end

#unselect_optionObject



142
143
144
145
# File 'lib/capybara/cuprite/node.rb', line 142

def unselect_option
  command(:select, false) ||
    raise(Capybara::UnselectNotAllowed, "Cannot unselect option from single select box.")
end

#valueObject



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

def value
  command(:value)
end

#visible?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/capybara/cuprite/node.rb', line 151

def visible?
  command(:visible?)
end

#visible_textObject



61
62
63
64
65
66
67
# File 'lib/capybara/cuprite/node.rb', line 61

def visible_text
  command(:visible_text).to_s
                        .gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
                        .gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
                        .gsub(/\n+/, "\n")
                        .tr("\u00a0", " ")
end