123456789_123456789_123456789_123456789_123456789_

Class: Capybara::Selenium::ChromeNode

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, FileInputClickEmulation, Html5Drag, Node, Scroll, Find, Node::WhitespaceNormalizer, Driver::Node
Inherits: Capybara::Selenium::Node
Defined in: lib/capybara/selenium/nodes/chrome_node.rb

Constant Summary

Node::WhitespaceNormalizer - Included

BREAKING_SPACES, EMPTY_LINES, LEADING_SPACES, LEFT_TO_RIGHT_MARK, LINE_SEPERATOR, NON_BREAKING_SPACE, PARAGRAPH_SEPERATOR, REMOVED_CHARACTERS, RIGHT_TO_LEFT_MARK, SQUEEZED_SPACES, TRAILING_SPACES, ZERO_WIDTH_SPACE

Scroll - Included

SCROLL_POSITIONS

Node - Inherited

GET_XPATH_SCRIPT, OBSCURED_OR_OFFSET_SCRIPT, RAPID_APPEND_TEXT

Class Method Summary

Driver::Node - Inherited

Instance Attribute Summary

Instance Method Summary

Node - Inherited

Scroll - Included

Find - Included

Node::WhitespaceNormalizer - Included

#normalize_spacing

Normalizes the spacing of a node’s text to be similar to what matchers might expect.

#normalize_visible_spacing

Variant on Capybara::Node::Normalizer#normalize_spacing that targets the whitespace of visible elements only.

Driver::Node - Inherited

Constructor Details

This class inherits a constructor from Capybara::Driver::Node

Instance Attribute Details

#chromedriver_fixed_actions_key_state?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 108

def chromedriver_fixed_actions_key_state?
  Gem::Requirement.new('>= 76.0.3809.68').satisfied_by?(chromedriver_version)
end

#chromedriver_supports_displayed_endpoint?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 112

def chromedriver_supports_displayed_endpoint?
  Gem::Requirement.new('>= 76.0.3809.25').satisfied_by?(chromedriver_version)
end

#disabled?Boolean (readonly)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 46

def disabled?
  driver.evaluate_script("arguments[0].matches(':disabled, select:disabled *')", self)
end

#native_displayed?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 120

def native_displayed?
  (driver.options[:native_displayed] != false) &&
    chromedriver_supports_displayed_endpoint? &&
    (!ENV['DISABLE_CAPYBARA_SELENIUM_OPTIMIZATIONS'])
end

#visible?Boolean (readonly)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 58

def visible?
  return super unless native_displayed?

  begin
    bridge.send(:execute, :is_element_displayed, id: native_id)
  rescue Selenium::WebDriver::Error::UnknownCommandError
    # If the is_element_displayed command is unknown, no point in trying again
    driver.options[:native_displayed] = false
    super
  end
end

Instance Method Details

#browser_version(to_float: true) (private)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 101

def browser_version(to_float: true)
  caps = capabilities
  ver = caps[:browser_version] || caps[:version]
  ver = ver.to_f if to_float
  ver
end

#chromedriver_version (private)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 116

def chromedriver_version
  Gem::Version.new(capabilities['chrome']['chromedriverVersion'].split(' ')[0]) # rubocop:disable Style/RedundantArgument
end

#click

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 33

def click(*, **)
  super
rescue ::Selenium::WebDriver::Error::ElementClickInterceptedError
  raise
rescue ::Selenium::WebDriver::Error::WebDriverError => e
  # chromedriver 74 (at least on mac) raises the wrong error for this
  if e.message.include?('element click intercepted')
    raise ::Selenium::WebDriver::Error::ElementClickInterceptedError, e.message
  end

  raise
end

#drop(*args)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 29

def drop(*args)
  html5_drop(*args)
end

#perform_legacy_drag(element, drop_modifiers) (private)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 90

def perform_legacy_drag(element, drop_modifiers)
  return super if chromedriver_fixed_actions_key_state? || element.obscured?

  raise ArgumentError, 'Modifier keys are not supported while dragging in this version of Chrome.' unless drop_modifiers.empty?

  # W3C Chrome/chromedriver < 77 doesn't maintain mouse button state across actions API performs
  # https://bugs.chromium.org/p/chromedriver/issues/detail?id=2981
  browser_action.release.perform
  browser_action.click_and_hold(native).move_to(element.native).release.perform
end

#select_option

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 50

def select_option
  # To optimize to only one check and then click
  selected_or_disabled = driver.evaluate_script(<<~JS, self)
    arguments[0].matches(':disabled, select:disabled *, :checked')
  JS
  click unless selected_or_disabled
end

#send_keys(*args)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 70

def send_keys(*args)
  args.chunk { |inp| inp.is_a?(String) && inp.match?(/\p{Emoji Presentation}/) }
      .each do |contains_emoji, inputs|
    if contains_emoji
      inputs.join.grapheme_clusters.chunk { |gc| gc.match?(/\p{Emoji Presentation}/) }
            .each do |emoji, clusters|
        if emoji
          driver.send(:execute_cdp, 'Input.insertText', text: clusters.join)
        else
          super(clusters.join)
        end
      end
    else
      super(*inputs)
    end
  end
end

#set_file(value)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 17

def set_file(value) # rubocop:disable Naming/AccessorMethodName
  # In Chrome 75+ files are appended (due to WebDriver spec - why?) so we have to clear here if its multiple and already set
  if browser_version >= 75.0
    driver.execute_script(<<~JS, self)
      if (arguments[0].multiple && arguments[0].files.length){
        arguments[0].value = null;
      }
    JS
  end
  super
end

#set_text(value, clear: nil, **_unused)

[ GitHub ]

  
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 10

def set_text(value, clear: nil, **_unused)
  super.tap do
    # React doesn't see the chromedriver element clear
    send_keys(:space, :backspace) if value.to_s.empty? && clear.nil?
  end
end