Class: Capybara::Cuprite::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver, node) ⇒ Node

Returns a new instance of Node.



15
16
17
18
# File 'lib/capybara/cuprite/node.rb', line 15

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



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

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

#[](name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/capybara/cuprite/node.rb', line 69

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



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

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.



224
225
226
227
# File 'lib/capybara/cuprite/node.rb', line 224

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

#attributesObject



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

def attributes
  command(:attributes)
end

#checked?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/capybara/cuprite/node.rb', line 134

def checked?
  self[:checked]
end

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



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

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

#command(name, *args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/capybara/cuprite/node.rb', line 20

def command(name, *args)
  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)


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

def disabled?
  command(:disabled?)
end

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



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

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

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



168
169
170
171
172
# File 'lib/capybara/cuprite/node.rb', line 168

def drag_by(x, y, **options)
  options[:steps] ||= 1

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

#drag_to(other, **options) ⇒ Object



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

def drag_to(other, **options)
  options[:steps] ||= 1

  command(:drag, other.node, options[:steps], options[:delay])
end

#find(method, selector) ⇒ Object



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

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

#find_css(selector) ⇒ Object



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

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

#find_xpath(selector) ⇒ Object



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

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

#hoverObject



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

def hover
  command(:hover)
end

#inspectObject



214
215
216
# File 'lib/capybara/cuprite/node.rb', line 214

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

#parentsObject



33
34
35
36
37
# File 'lib/capybara/cuprite/node.rb', line 33

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

#pathObject



210
211
212
# File 'lib/capybara/cuprite/node.rb', line 210

def path
  command(:path)
end

#property(name) ⇒ Object



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

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

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



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

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

#scroll_by(x, y) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/capybara/cuprite/node.rb', line 189

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



178
179
180
181
182
183
184
185
186
187
# File 'lib/capybara/cuprite/node.rb', line 178

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



117
118
119
# File 'lib/capybara/cuprite/node.rb', line 117

def select_option
  command(:select, true)
end

#selected?Boolean

Returns:

  • (Boolean)


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

def selected?
  !!self[:selected]
end

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



205
206
207
# File 'lib/capybara/cuprite/node.rb', line 205

def send_keys(*keys)
  command(:send_keys, keys)
end

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/capybara/cuprite/node.rb', line 92

def set(value, options = {})
  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}')")
    else
      command(:set, value.to_s)
    end
  elsif tag_name == "textarea"
    command(:set, value.to_s)
  elsif self[:isContentEditable]
    command(:delete_text)
    send_keys(value.to_s)
  end
end

#tag_nameObject



126
127
128
# File 'lib/capybara/cuprite/node.rb', line 126

def tag_name
  @tag_name ||= 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.



219
220
221
# File 'lib/capybara/cuprite/node.rb', line 219

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

#trigger(event) ⇒ Object



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

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

#unselect_optionObject



121
122
123
124
# File 'lib/capybara/cuprite/node.rb', line 121

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

#valueObject



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

def value
  command(:value)
end

#visible?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/capybara/cuprite/node.rb', line 130

def visible?
  command(:visible?)
end

#visible_textObject



57
58
59
60
61
62
63
# File 'lib/capybara/cuprite/node.rb', line 57

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