class Capybara::Cuprite::Node

Attributes

node[R]

Public Class Methods

new(driver, node) click to toggle source
Calls superclass method
# File lib/capybara/cuprite/node.rb, line 14
def initialize(driver, node)
  super(driver, self)
  @node = node
end

Public Instance Methods

==(other) click to toggle source
# File lib/capybara/cuprite/node.rb, line 200
def ==(other)
  node == other.native.node
end
[](name) click to toggle source
# File lib/capybara/cuprite/node.rb, line 72
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_text() click to toggle source
# File lib/capybara/cuprite/node.rb, line 52
def all_text
  filter_text(command(:all_text))
end
as_json(*) click to toggle source

@api private

# File lib/capybara/cuprite/node.rb, line 223
def as_json(*)
  # FIXME: Where is this method used and why attr is called id?
  { ELEMENT: { node: node, id: node.node_id } }
end
attributes() click to toggle source
# File lib/capybara/cuprite/node.rb, line 87
def attributes
  command(:attributes)
end
checked?() click to toggle source
# File lib/capybara/cuprite/node.rb, line 137
def checked?
  self[:checked]
end
click(keys = [], **options) click to toggle source
# File lib/capybara/cuprite/node.rb, line 149
def click(keys = [], **options)
  prepare_and_click(:left, __method__, keys, options)
end
command(name, *args) click to toggle source
# File lib/capybara/cuprite/node.rb, line 19
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?() click to toggle source
# File lib/capybara/cuprite/node.rb, line 145
def disabled?
  command(:disabled?)
end
double_click(keys = [], **options) click to toggle source
# File lib/capybara/cuprite/node.rb, line 157
def double_click(keys = [], **options)
  prepare_and_click(:double, __method__, keys, options)
end
drag_by(x, y) click to toggle source
# File lib/capybara/cuprite/node.rb, line 169
def drag_by(x, y)
  command(:drag_by, x, y)
end
drag_to(other) click to toggle source
# File lib/capybara/cuprite/node.rb, line 165
def drag_to(other)
  command(:drag, other)
end
find(method, selector) click to toggle source
# File lib/capybara/cuprite/node.rb, line 46
def find(method, selector)
  command(:find_within, method, selector).map do |node|
    self.class.new(driver, node)
  end
end
find_css(selector) click to toggle source
# File lib/capybara/cuprite/node.rb, line 42
def find_css(selector)
  find(:css, selector)
end
find_xpath(selector) click to toggle source
# File lib/capybara/cuprite/node.rb, line 38
def find_xpath(selector)
  find(:xpath, selector)
end
hover() click to toggle source
# File lib/capybara/cuprite/node.rb, line 161
def hover
  command(:hover)
end
inspect() click to toggle source
# File lib/capybara/cuprite/node.rb, line 213
def inspect
  %(#<#{self.class} @node=#{@node.inspect}>)
end
parents() click to toggle source
# File lib/capybara/cuprite/node.rb, line 32
def parents
  command(:parents).map do |parent|
    self.class.new(driver, parent)
  end
end
path() click to toggle source
# File lib/capybara/cuprite/node.rb, line 209
def path
  command(:path)
end
property(name) click to toggle source
# File lib/capybara/cuprite/node.rb, line 68
def property(name)
  command(:property, name)
end
right_click(keys = [], **options) click to toggle source
# File lib/capybara/cuprite/node.rb, line 153
def right_click(keys = [], **options)
  prepare_and_click(:right, __method__, keys, options)
end
scroll_by(x, y) click to toggle source
# File lib/capybara/cuprite/node.rb, line 188
    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) click to toggle source
# File lib/capybara/cuprite/node.rb, line 177
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_option() click to toggle source
# File lib/capybara/cuprite/node.rb, line 120
def select_option
  command(:select, true)
end
selected?() click to toggle source
# File lib/capybara/cuprite/node.rb, line 141
def selected?
  !!self[:selected]
end
send_key(*keys)
Alias for: send_keys
send_keys(*keys) click to toggle source
# File lib/capybara/cuprite/node.rb, line 204
def send_keys(*keys)
  command(:send_keys, keys)
end
Also aliased as: send_key
set(value, options = {}) click to toggle source
# File lib/capybara/cuprite/node.rb, line 95
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_name() click to toggle source
# File lib/capybara/cuprite/node.rb, line 129
def tag_name
  @tag_name ||= description["nodeName"].downcase
end
to_json(*) click to toggle source

@api private

# File lib/capybara/cuprite/node.rb, line 218
def to_json(*)
  JSON.generate(as_json)
end
trigger(event) click to toggle source
# File lib/capybara/cuprite/node.rb, line 173
def trigger(event)
  command(:trigger, event)
end
unselect_option() click to toggle source
# File lib/capybara/cuprite/node.rb, line 124
def unselect_option
  command(:select, false) ||
    raise(Capybara::UnselectNotAllowed, "Cannot unselect option from single select box.")
end
value() click to toggle source
# File lib/capybara/cuprite/node.rb, line 91
def value
  command(:value)
end
visible?() click to toggle source
# File lib/capybara/cuprite/node.rb, line 133
def visible?
  command(:visible?)
end
visible_text() click to toggle source
# File lib/capybara/cuprite/node.rb, line 56
def visible_text
  if Capybara::VERSION.to_f < 3.0
    filter_text(command(:visible_text))
  else
    command(:visible_text).to_s
                          .gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
                          .gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
                          .gsub(/\n+/, "\n")
                          .tr("\u00a0", " ")
  end
end

Private Instance Methods

filter_text(text) click to toggle source
# File lib/capybara/cuprite/node.rb, line 238
def filter_text(text)
  if Capybara::VERSION.to_f < 3
    Capybara::Helpers.normalize_whitespace(text.to_s)
  else
    text.gsub(/[\u200b\u200e\u200f]/, "")
        .gsub(/[\ \n\f\t\v\u2028\u2029]+/, " ")
        .gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
        .gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
        .tr("\u00a0", " ")
  end
end
prepare_and_click(mode, name, keys, options) click to toggle source
# File lib/capybara/cuprite/node.rb, line 230
def prepare_and_click(mode, name, keys, options)
  delay = options[:delay].to_i
  x, y = options.values_at(:x, :y)
  offset = { x: x, y: y, position: options[:offset] || :top }
  command(:before_click, name, keys, offset)
  node.click(mode: mode, keys: keys, offset: offset, delay: delay)
end
scroll_element_to_location(element, location) click to toggle source
# File lib/capybara/cuprite/node.rb, line 250
    def scroll_element_to_location(element, location)
      scroll_opts = case location
      when :top
        'true'
      when :bottom
        'false'
      when :center
        "{behavior: 'instant', block: 'center'}"
      else
        raise ArgumentError, "Invalid scroll_to location: #{location}"
      end
      driver.execute_script <<~JS, element
        arguments[0].scrollIntoView(#{scroll_opts})
      JS
    end
scroll_to_coords(x, y) click to toggle source
# File lib/capybara/cuprite/node.rb, line 284
    def scroll_to_coords(x, y)
      driver.execute_script <<~JS, self, x, y
        if (arguments[0].scrollTo){
          arguments[0].scrollTo(arguments[1], arguments[2]);
        } else {
          arguments[0].scrollTop = arguments[2];
          arguments[0].scrollLeft = arguments[1];
        }
      JS
    end
scroll_to_location(location) click to toggle source
# File lib/capybara/cuprite/node.rb, line 266
    def scroll_to_location(location)
      scroll_y = case location
      when :top
        '0'
      when :bottom
        'arguments[0].scrollHeight'
      when :center
        '(arguments[0].scrollHeight - arguments[0].clientHeight)/2'
      end
      driver.execute_script <<~JS, self
        if (arguments[0].scrollTo){
          arguments[0].scrollTo(0, #{scroll_y});
        } else {
          arguments[0].scrollTop = #{scroll_y};
        }
      JS
    end