2018-06-06 23:11:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Capybara::Selenium::MarionetteNode < Capybara::Selenium::Node
|
|
|
|
def click(keys = [], **options)
|
|
|
|
super
|
|
|
|
rescue ::Selenium::WebDriver::Error::ElementNotInteractableError
|
2018-07-10 21:18:39 +00:00
|
|
|
if tag_name == 'tr'
|
|
|
|
warn 'You are attempting to click a table row which has issues in geckodriver/marionette - see https://github.com/mozilla/geckodriver/issues/1228. ' \
|
|
|
|
'Your test should probably be clicking on a table cell like a user would. Clicking the first cell in the row instead.'
|
2018-09-01 18:20:11 +00:00
|
|
|
return find_css('th:first-child,td:first-child')[0].click(keys, options)
|
2018-06-06 23:11:47 +00:00
|
|
|
end
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
def disabled?
|
2018-06-27 19:53:58 +00:00
|
|
|
# Not sure exactly what version of FF fixed the below issue, but it is definitely fixed in 61+
|
2018-08-10 17:40:58 +00:00
|
|
|
return super unless browser_version < 61.0
|
2018-06-06 23:11:47 +00:00
|
|
|
|
2018-06-27 19:53:58 +00:00
|
|
|
return true if super
|
2018-06-06 23:11:47 +00:00
|
|
|
# workaround for selenium-webdriver/geckodriver reporting elements as enabled when they are nested in disabling elements
|
|
|
|
if %w[option optgroup].include? tag_name
|
2018-07-10 21:18:39 +00:00
|
|
|
find_xpath('parent::*[self::optgroup or self::select]')[0].disabled?
|
2018-06-06 23:11:47 +00:00
|
|
|
else
|
2018-07-10 21:18:39 +00:00
|
|
|
!find_xpath('parent::fieldset[@disabled] | ancestor::*[not(self::legend) or preceding-sibling::legend][parent::fieldset[@disabled]]').empty?
|
2018-06-06 23:11:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_file(value) # rubocop:disable Naming/AccessorMethodName
|
2018-08-31 22:26:01 +00:00
|
|
|
# By default files are appended so we have to clear here if its multiple and already set
|
2018-08-31 17:01:40 +00:00
|
|
|
native.clear if multiple? && driver.evaluate_script('arguments[0].files', self).any?
|
2018-08-10 17:40:58 +00:00
|
|
|
return super if browser_version >= 62.0
|
2018-07-24 21:13:03 +00:00
|
|
|
|
2018-07-26 21:13:30 +00:00
|
|
|
# Workaround lack of support for multiple upload by uploading one at a time
|
2018-06-06 23:11:47 +00:00
|
|
|
path_names = value.to_s.empty? ? [] : value
|
2018-06-25 23:31:31 +00:00
|
|
|
Array(path_names).each do |path|
|
|
|
|
unless driver.browser.respond_to?(:upload)
|
2018-06-26 00:27:36 +00:00
|
|
|
if (fd = bridge.file_detector)
|
2018-06-25 23:31:31 +00:00
|
|
|
local_file = fd.call([path])
|
|
|
|
path = upload(local_file) if local_file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
native.send_keys(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-02 19:56:16 +00:00
|
|
|
def send_keys(*args)
|
|
|
|
# https://github.com/mozilla/geckodriver/issues/846
|
2018-08-20 23:46:31 +00:00
|
|
|
return super(*args.map { |arg| arg == :space ? ' ' : arg }) if args.none? { |arg| arg.is_a? Array }
|
2018-08-02 19:56:16 +00:00
|
|
|
|
|
|
|
native.click
|
2018-08-17 20:57:12 +00:00
|
|
|
args.each_with_object(browser_action) do |keys, actions|
|
2018-08-02 19:56:16 +00:00
|
|
|
_send_keys(keys, actions)
|
2018-08-17 20:57:12 +00:00
|
|
|
end.perform
|
2018-08-02 19:56:16 +00:00
|
|
|
end
|
|
|
|
|
2018-08-10 17:40:58 +00:00
|
|
|
def drag_to(element)
|
|
|
|
return super unless (browser_version >= 62.0) && (self[:draggable] == 'true')
|
|
|
|
|
2018-08-17 20:57:12 +00:00
|
|
|
scroll_if_needed { browser_action.click_and_hold(native).perform }
|
2018-08-10 17:40:58 +00:00
|
|
|
driver.execute_script HTML5_DRAG_DROP_SCRIPT, self, element
|
|
|
|
end
|
|
|
|
|
2018-06-25 23:31:31 +00:00
|
|
|
private
|
2018-06-26 00:27:36 +00:00
|
|
|
|
2018-09-01 18:20:11 +00:00
|
|
|
def click_with_options(click_options)
|
|
|
|
# Firefox/marionette has an issue clicking with offset near viewport edge
|
|
|
|
# scroll element to middle just in case
|
|
|
|
scroll_to_center if click_options.coords?
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2018-08-02 19:56:16 +00:00
|
|
|
def _send_keys(keys, actions, down_keys = nil)
|
|
|
|
case keys
|
|
|
|
when String
|
|
|
|
keys = keys.upcase if down_keys&.include?(:shift) # https://bugzilla.mozilla.org/show_bug.cgi?id=1405370
|
|
|
|
actions.send_keys(keys)
|
|
|
|
when :space
|
|
|
|
actions.send_keys(' ') # https://github.com/mozilla/geckodriver/issues/846
|
|
|
|
when :control, :left_control, :right_control,
|
|
|
|
:alt, :left_alt, :right_alt,
|
|
|
|
:shift, :left_shift, :right_shift,
|
|
|
|
:meta, :left_meta, :right_meta,
|
|
|
|
:command
|
|
|
|
if down_keys.nil?
|
|
|
|
actions.send_keys(keys)
|
|
|
|
else
|
|
|
|
down_keys << keys
|
|
|
|
actions.key_down(keys)
|
|
|
|
end
|
|
|
|
when Symbol
|
|
|
|
actions.send_keys(keys)
|
|
|
|
when Array
|
|
|
|
local_down_keys = []
|
|
|
|
keys.each do |sub_keys|
|
|
|
|
_send_keys(sub_keys, actions, local_down_keys)
|
|
|
|
end
|
|
|
|
local_down_keys.each { |key| actions.key_up(key) }
|
|
|
|
else
|
|
|
|
raise ArgumentError, 'Unknown keys type'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-25 23:31:31 +00:00
|
|
|
def bridge
|
|
|
|
driver.browser.send(:bridge)
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload(local_file)
|
|
|
|
unless File.file?(local_file)
|
2018-07-26 21:13:30 +00:00
|
|
|
raise ArgumentError, "You may only upload files: #{local_file.inspect}"
|
2018-06-25 23:31:31 +00:00
|
|
|
end
|
|
|
|
|
2018-06-26 00:27:36 +00:00
|
|
|
result = bridge.http.call(:post, "session/#{bridge.session_id}/file", file: Selenium::WebDriver::Zipper.zip_file(local_file))
|
2018-06-25 23:31:31 +00:00
|
|
|
result['value']
|
2018-06-06 23:11:47 +00:00
|
|
|
end
|
2018-08-10 17:40:58 +00:00
|
|
|
|
|
|
|
def browser_version
|
|
|
|
driver.browser.capabilities[:browser_version].to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
HTML5_DRAG_DROP_SCRIPT = <<~JS
|
|
|
|
var source = arguments[0];
|
|
|
|
var target = arguments[1];
|
|
|
|
|
|
|
|
var dt = new DataTransfer();
|
|
|
|
var opts = { cancelable: true, bubbles: true, dataTransfer: dt };
|
|
|
|
|
|
|
|
var dragEvent = new DragEvent('dragstart', opts);
|
|
|
|
source.dispatchEvent(dragEvent);
|
|
|
|
target.scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
|
|
|
|
var dragOverEvent = new DragEvent('dragover', opts);
|
|
|
|
target.dispatchEvent(dragOverEvent);
|
|
|
|
var dragLeaveEvent = new DragEvent('dragleave', opts);
|
|
|
|
target.dispatchEvent(dragLeaveEvent);
|
|
|
|
if (dragOverEvent.defaultPrevented) {
|
|
|
|
var dropEvent = new DragEvent('drop', opts);
|
|
|
|
target.dispatchEvent(dropEvent);
|
|
|
|
}
|
|
|
|
var dragEndEvent = new DragEvent('dragend', opts);
|
|
|
|
source.dispatchEvent(dragEndEvent);
|
|
|
|
JS
|
2018-06-06 23:11:47 +00:00
|
|
|
end
|