Class: Capybara::Selenium::ChromeNode

Inherits:
Node show all
Includes:
FileInputClickEmulation, Html5Drag
Defined in:
lib/capybara/selenium/nodes/chrome_node.rb

Instance Attribute Summary

Attributes inherited from Driver::Node

#driver, #initial_cache, #native

Instance Method Summary collapse

Methods inherited from Node

#==, #[], #all_text, #content_editable?, #double_click, #drag_to, #hover, #multiple?, #obscured?, #path, #readonly?, #rect, #right_click, #selected?, #send_keys, #set, #style, #tag_name, #unselect_option, #value, #visible_text

Methods included from Scroll

#scroll_by, #scroll_to

Methods included from Find

#find_css, #find_xpath

Methods inherited from Driver::Node

#==, #[], #all_text, #checked?, #double_click, #drag_to, #hover, #initialize, #inspect, #multiple?, #obscured?, #path, #readonly?, #rect, #right_click, #scroll_by, #scroll_to, #selected?, #send_keys, #set, #style, #tag_name, #trigger, #unselect_option, #value, #visible_text

Constructor Details

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

Instance Method Details

#clickObject


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 39

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.match?(/element click intercepted/)
    raise ::Selenium::WebDriver::Error::ElementClickInterceptedError, e.message
  end

  raise
end

#disabled?Boolean


52
53
54
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 52

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

#drop(*args) ⇒ Object


35
36
37
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 35

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

#select_optionObject


56
57
58
59
60
61
62
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 56

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

#set_file(value) ⇒ Object

rubocop:disable Naming/AccessorMethodName


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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 > 0)){
        arguments[0].value = null;
      }
    JS
  end
  super
rescue *file_errors => e
  if e.message.match?(/File not found : .+\n.+/m)
    raise ArgumentError, "Selenium < 3.14 with remote Chrome doesn't support multiple file upload"
  end

  raise
end

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


10
11
12
13
14
15
# 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

#visible?Boolean


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/capybara/selenium/nodes/chrome_node.rb', line 64

def visible?
  return super unless native_displayed?

  begin
    bridge.send(:execute, :is_element_displayed, id: native.ref)
  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