Class: Capybara::Selenium::SafariNode

Inherits:
Node show all
Defined in:
lib/capybara/selenium/nodes/safari_node.rb

Overview

require 'capybara/selenium/extensions/html5_drag'

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, #drop, #multiple?, #obscured?, #path, #readonly?, #right_click, #selected?, #set, #style, #tag_name, #value, #visible?

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, #drop, #initialize, #inspect, #multiple?, #obscured?, #path, #readonly?, #right_click, #scroll_by, #scroll_to, #selected?, #set, #style, #tag_name, #trigger, #value, #visible?

Constructor Details

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

Instance Method Details

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

include Html5Drag



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/capybara/selenium/nodes/safari_node.rb', line 8

def click(keys = [], **options)
  # driver.execute_script('arguments[0].scrollIntoViewIfNeeded({block: "center"})', self)
  super
rescue ::Selenium::WebDriver::Error::ElementNotInteractableError
  if tag_name == 'tr'
    warn 'You are attempting to click a table row which has issues in safaridriver - '\
         'Your test should probably be clicking on a table cell like a user would. '\
         'Clicking the first cell in the row instead.'
    return find_css('th:first-child,td:first-child')[0].click(keys, options)
  end
  raise
rescue ::Selenium::WebDriver::Error::WebDriverError
  # Safari doesn't return a specific error here - assume it's an ElementNotInteractableError
  raise ::Selenium::WebDriver::Error::ElementNotInteractableError,
        'Non distinct error raised in #click, translated to ElementNotInteractableError for retry'
end

#disabled?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/capybara/selenium/nodes/safari_node.rb', line 50

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

#hoverObject



83
84
85
86
# File 'lib/capybara/selenium/nodes/safari_node.rb', line 83

def hover
  # Workaround issue where hover would sometimes fail - possibly due to mouse not having moved
  scroll_if_needed { browser_action.move_to(native, 0, 0).move_to(native).perform }
end

#select_optionObject



25
26
27
28
29
30
31
32
# File 'lib/capybara/selenium/nodes/safari_node.rb', line 25

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

#send_keys(*args) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/capybara/selenium/nodes/safari_node.rb', line 60

def send_keys(*args)
  if args.none? { |arg| arg.is_a?(Array) || (arg.is_a?(Symbol) && MODIFIER_KEYS.include?(arg)) }
    return super(*args.map { |arg| arg == :space ? ' ' : arg })
  end

  native.click
  _send_keys(args).perform
end

#set_file(value) ⇒ Object

rubocop:disable Naming/AccessorMethodName



54
55
56
57
58
# File 'lib/capybara/selenium/nodes/safari_node.rb', line 54

def set_file(value) # rubocop:disable Naming/AccessorMethodName
  # By default files are appended so we have to clear here if its multiple and already set
  native.clear if multiple? && driver.evaluate_script('arguments[0].files', self).any?
  super
end

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



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/capybara/selenium/nodes/safari_node.rb', line 69

def set_text(value, clear: nil, **_unused)
  value = value.to_s
  if clear == :backspace
    # Clear field by sending the correct number of backspace keys.
    backspaces = [:backspace] * self.value.to_s.length
    send_keys(*([[:control, 'e']] + backspaces + [value]))
  else
    super.tap do
      # React doesn't see the safaridriver element clear
      send_keys(:space, :backspace) if value.to_s.empty? && clear.nil?
    end
  end
end

#unselect_optionObject



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

def unselect_option
  driver.execute_script("arguments[0].closest('select').scrollIntoView()", self)
  super
end

#visible_textObject



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

def visible_text
  return '' unless visible?

  vis_text = driver.execute_script('return arguments[0].innerText', self)
  vis_text.gsub(/\ +/, ' ')
          .gsub(/[\ \n]*\n[\ \n]*/, "\n")
          .gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
          .gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
          .tr("\u00a0", ' ')
end