2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 15:23:54 -05:00
|
|
|
|
2018-08-17 16:57:12 -04:00
|
|
|
# Selenium specific implementation of the Capybara::Driver::Node API
|
2018-12-17 13:09:53 -05:00
|
|
|
|
2019-01-03 16:00:38 -05:00
|
|
|
require 'capybara/selenium/extensions/find'
|
2018-12-17 13:09:53 -05:00
|
|
|
require 'capybara/selenium/extensions/scroll'
|
|
|
|
|
2011-04-11 01:24:00 -04:00
|
|
|
class Capybara::Selenium::Node < Capybara::Driver::Node
|
2019-01-03 16:00:38 -05:00
|
|
|
include Capybara::Selenium::Find
|
2018-12-17 13:09:53 -05:00
|
|
|
include Capybara::Selenium::Scroll
|
|
|
|
|
2013-02-17 08:58:41 -05:00
|
|
|
def visible_text
|
2018-03-05 17:57:33 -05:00
|
|
|
native.text
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|
|
|
|
|
2013-02-17 08:58:41 -05:00
|
|
|
def all_text
|
2019-01-15 14:05:35 -05:00
|
|
|
text = driver.evaluate_script('arguments[0].textContent', self)
|
2018-03-05 17:57:33 -05:00
|
|
|
text.gsub(/[\u200b\u200e\u200f]/, '')
|
|
|
|
.gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
|
2018-07-10 17:18:39 -04:00
|
|
|
.gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
|
|
|
|
.gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
|
2018-03-05 17:57:33 -05:00
|
|
|
.tr("\u00a0", ' ')
|
2013-02-17 08:58:41 -05:00
|
|
|
end
|
|
|
|
|
2011-04-11 01:24:00 -04:00
|
|
|
def [](name)
|
2012-10-26 09:14:56 -04:00
|
|
|
native.attribute(name.to_s)
|
2011-04-11 01:24:00 -04:00
|
|
|
rescue Selenium::WebDriver::Error::WebDriverError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def value
|
2018-07-10 17:18:39 -04:00
|
|
|
if tag_name == 'select' && multiple?
|
2018-08-20 19:46:31 -04:00
|
|
|
native.find_elements(:css, 'option:checked').map { |el| el[:value] || el.text }
|
2011-04-11 01:24:00 -04:00
|
|
|
else
|
2011-04-28 15:30:01 -04:00
|
|
|
native[:value]
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-19 16:34:54 -04:00
|
|
|
def style(styles)
|
2018-06-20 14:43:21 -04:00
|
|
|
styles.each_with_object({}) do |style, result|
|
|
|
|
result[style] = native.css_value(style)
|
2018-06-19 16:34:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-13 00:25:13 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Set the value of the form element to the given value.
|
|
|
|
#
|
|
|
|
# @param [String] value The new value
|
|
|
|
# @param [Hash{}] options Driver specific options for how to set the value
|
|
|
|
# @option options [Symbol,Array] :clear (nil) The method used to clear the previous value <br/>
|
|
|
|
# nil => clear via javascript <br/>
|
|
|
|
# :none => append the new value to the existing value <br/>
|
|
|
|
# :backspace => send backspace keystrokes to clear the field <br/>
|
|
|
|
# Array => an array of keys to send before the value being set, e.g. [[:command, 'a'], :backspace]
|
2016-08-17 19:14:39 -04:00
|
|
|
def set(value, **options)
|
2019-10-15 21:02:35 -04:00
|
|
|
if value.is_a?(Array) && !multiple?
|
|
|
|
raise ArgumentError, "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
|
|
|
|
end
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2019-04-30 12:49:01 -04:00
|
|
|
tag_name, type = attrs(:tagName, :type).map { |val| val&.downcase }
|
|
|
|
@tag_name ||= tag_name
|
|
|
|
|
|
|
|
case tag_name
|
2017-08-01 16:59:17 -04:00
|
|
|
when 'input'
|
2019-04-30 12:49:01 -04:00
|
|
|
case type
|
2017-08-01 16:59:17 -04:00
|
|
|
when 'radio'
|
|
|
|
click
|
|
|
|
when 'checkbox'
|
2018-01-11 19:45:50 -05:00
|
|
|
click if value ^ checked?
|
2017-08-01 16:59:17 -04:00
|
|
|
when 'file'
|
2017-11-13 16:04:47 -05:00
|
|
|
set_file(value)
|
2018-03-09 17:23:52 -05:00
|
|
|
when 'date'
|
|
|
|
set_date(value)
|
|
|
|
when 'time'
|
|
|
|
set_time(value)
|
|
|
|
when 'datetime-local'
|
|
|
|
set_datetime_local(value)
|
2019-06-05 12:11:31 -04:00
|
|
|
when 'color'
|
|
|
|
set_color(value)
|
2016-10-18 14:09:26 -04:00
|
|
|
else
|
2019-11-28 17:41:13 -05:00
|
|
|
set_text(value, **options)
|
2016-10-18 14:09:26 -04:00
|
|
|
end
|
2017-08-01 16:59:17 -04:00
|
|
|
when 'textarea'
|
2019-11-28 17:41:13 -05:00
|
|
|
set_text(value, **options)
|
2017-08-01 16:59:17 -04:00
|
|
|
else
|
2019-04-30 12:49:01 -04:00
|
|
|
set_content_editable(value)
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def select_option
|
2018-08-24 07:45:19 -04:00
|
|
|
click unless selected? || disabled?
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def unselect_option
|
2018-07-10 17:18:39 -04:00
|
|
|
raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.' unless select_node.multiple?
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2018-08-24 07:45:19 -04:00
|
|
|
click if selected?
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|
|
|
|
|
2018-05-17 17:45:53 -04:00
|
|
|
def click(keys = [], **options)
|
2018-08-17 16:57:12 -04:00
|
|
|
click_options = ClickOptions.new(keys, options)
|
|
|
|
return native.click if click_options.empty?
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2018-09-01 14:20:11 -04:00
|
|
|
click_with_options(click_options)
|
2019-04-04 15:03:01 -04:00
|
|
|
rescue StandardError => e
|
|
|
|
if e.is_a?(::Selenium::WebDriver::Error::ElementClickInterceptedError) ||
|
|
|
|
e.message.match?(/Other element would receive the click/)
|
2018-05-15 12:17:09 -04:00
|
|
|
scroll_to_center
|
2017-10-02 14:55:13 -04:00
|
|
|
end
|
2018-06-06 16:21:33 -04:00
|
|
|
|
2019-04-04 15:03:01 -04:00
|
|
|
raise e
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|
2015-04-13 12:24:13 -04:00
|
|
|
|
2018-05-17 17:45:53 -04:00
|
|
|
def right_click(keys = [], **options)
|
2018-08-17 16:57:12 -04:00
|
|
|
click_options = ClickOptions.new(keys, options)
|
2018-09-01 14:20:11 -04:00
|
|
|
click_with_options(click_options) do |action|
|
|
|
|
click_options.coords? ? action.context_click : action.context_click(native)
|
2017-11-16 21:32:26 -05:00
|
|
|
end
|
2013-05-10 12:55:17 -04:00
|
|
|
end
|
2015-04-13 12:24:13 -04:00
|
|
|
|
2018-05-17 17:45:53 -04:00
|
|
|
def double_click(keys = [], **options)
|
2018-08-17 16:57:12 -04:00
|
|
|
click_options = ClickOptions.new(keys, options)
|
2018-09-01 14:20:11 -04:00
|
|
|
click_with_options(click_options) do |action|
|
|
|
|
click_options.coords? ? action.double_click : action.double_click(native)
|
2017-11-16 21:32:26 -05:00
|
|
|
end
|
2013-05-10 12:55:17 -04:00
|
|
|
end
|
2015-04-13 12:24:13 -04:00
|
|
|
|
2015-01-23 15:23:57 -05:00
|
|
|
def send_keys(*args)
|
|
|
|
native.send_keys(*args)
|
|
|
|
end
|
2011-04-11 01:24:00 -04:00
|
|
|
|
2013-02-25 13:37:25 -05:00
|
|
|
def hover
|
2018-08-17 16:57:12 -04:00
|
|
|
scroll_if_needed { browser_action.move_to(native).perform }
|
2013-02-25 13:37:25 -05:00
|
|
|
end
|
2012-12-14 19:06:50 -05:00
|
|
|
|
2019-06-09 12:12:03 -04:00
|
|
|
def drag_to(element, **)
|
2018-08-31 17:24:30 -04:00
|
|
|
# Due to W3C spec compliance - The Actions API no longer scrolls to elements when necessary
|
|
|
|
# which means Seleniums `drag_and_drop` is now broken - do it manually
|
|
|
|
scroll_if_needed { browser_action.click_and_hold(native).perform }
|
|
|
|
element.scroll_if_needed { browser_action.move_to(element.native).release.perform }
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|
|
|
|
|
2019-05-15 17:25:50 -04:00
|
|
|
def drop(*_)
|
|
|
|
raise NotImplementedError, 'Out of browser drop emulation is not implemented for the current browser'
|
|
|
|
end
|
|
|
|
|
2011-04-11 01:24:00 -04:00
|
|
|
def tag_name
|
2019-04-30 12:49:01 -04:00
|
|
|
@tag_name ||= native.tag_name.downcase
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|
|
|
|
|
2018-01-13 16:06:03 -05:00
|
|
|
def visible?; boolean_attr(native.displayed?); end
|
|
|
|
def readonly?; boolean_attr(self[:readonly]); end
|
|
|
|
def multiple?; boolean_attr(self[:multiple]); end
|
|
|
|
def selected?; boolean_attr(native.selected?); end
|
2016-06-11 15:09:23 -04:00
|
|
|
alias :checked? :selected?
|
2011-04-11 01:24:00 -04:00
|
|
|
|
2013-01-29 05:45:24 -05:00
|
|
|
def disabled?
|
2018-06-27 13:19:47 -04:00
|
|
|
return true unless native.enabled?
|
2019-04-02 15:08:52 -04:00
|
|
|
|
2018-06-27 13:19:47 -04:00
|
|
|
# WebDriver only defines `disabled?` for form controls but fieldset makes sense too
|
2019-04-02 13:10:41 -04:00
|
|
|
find_xpath('self::fieldset/ancestor-or-self::fieldset[@disabled]').any?
|
2013-01-29 05:45:24 -05:00
|
|
|
end
|
|
|
|
|
2017-08-01 16:59:17 -04:00
|
|
|
def content_editable?
|
2019-05-02 21:57:14 -04:00
|
|
|
native.attribute('isContentEditable') == 'true'
|
2017-08-01 16:59:17 -04:00
|
|
|
end
|
|
|
|
|
2012-11-19 21:57:09 -05:00
|
|
|
def ==(other)
|
|
|
|
native == other.native
|
|
|
|
end
|
|
|
|
|
2015-08-09 12:55:56 -04:00
|
|
|
def path
|
2018-12-29 16:13:33 -05:00
|
|
|
driver.evaluate_script GET_XPATH_SCRIPT, self
|
2015-08-09 12:55:56 -04:00
|
|
|
end
|
|
|
|
|
2019-05-10 11:07:57 -04:00
|
|
|
def obscured?(x: nil, y: nil)
|
|
|
|
res = driver.evaluate_script(OBSCURED_OR_OFFSET_SCRIPT, self, x, y)
|
|
|
|
return true if res == true
|
|
|
|
|
|
|
|
driver.frame_obscured_at?(x: res['x'], y: res['y'])
|
|
|
|
end
|
|
|
|
|
2019-08-07 17:14:28 -04:00
|
|
|
def rect
|
|
|
|
native.rect
|
|
|
|
end
|
|
|
|
|
2018-08-31 17:24:30 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def scroll_if_needed
|
|
|
|
yield
|
|
|
|
rescue ::Selenium::WebDriver::Error::MoveTargetOutOfBoundsError
|
|
|
|
scroll_to_center
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
2019-06-26 09:37:26 -04:00
|
|
|
def scroll_to_center
|
|
|
|
script = <<-'JS'
|
|
|
|
try {
|
|
|
|
arguments[0].scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
|
|
|
|
} catch(e) {
|
|
|
|
arguments[0].scrollIntoView(true);
|
|
|
|
}
|
|
|
|
JS
|
|
|
|
begin
|
|
|
|
driver.execute_script(script, self)
|
2019-11-28 15:19:37 -05:00
|
|
|
rescue StandardError # rubocop:disable Lint/SuppressedException
|
2019-06-26 09:37:26 -04:00
|
|
|
# Swallow error if scrollIntoView with options isn't supported
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-11 01:24:00 -04:00
|
|
|
private
|
2018-01-09 17:05:50 -05:00
|
|
|
|
2018-11-02 14:38:58 -04:00
|
|
|
def sibling_index(parent, node, selector)
|
|
|
|
siblings = parent.find_xpath(selector)
|
|
|
|
case siblings.size
|
|
|
|
when 0
|
|
|
|
'[ERROR]' # IE doesn't support full XPath (namespace-uri, etc)
|
|
|
|
when 1
|
|
|
|
'' # index not necessary when only one matching element
|
|
|
|
else
|
|
|
|
idx = siblings.index(node)
|
|
|
|
# Element may not be found in the siblings if it has gone away
|
|
|
|
idx.nil? ? '[ERROR]' : "[#{idx + 1}]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-13 16:06:03 -05:00
|
|
|
def boolean_attr(val)
|
2018-07-10 17:18:39 -04:00
|
|
|
val && (val != 'false')
|
2018-01-13 16:06:03 -05:00
|
|
|
end
|
|
|
|
|
2011-04-11 01:24:00 -04:00
|
|
|
# a reference to the select node if this is an option node
|
|
|
|
def select_node
|
2018-01-13 16:06:03 -05:00
|
|
|
find_xpath(XPath.ancestor(:select)[1]).first
|
2017-08-01 16:59:17 -04:00
|
|
|
end
|
|
|
|
|
2018-03-16 12:46:35 -04:00
|
|
|
def set_text(value, clear: nil, **_unused)
|
2018-08-17 16:57:12 -04:00
|
|
|
value = value.to_s
|
|
|
|
if value.empty? && clear.nil?
|
2017-08-01 16:59:17 -04:00
|
|
|
native.clear
|
2018-01-08 15:23:54 -05:00
|
|
|
elsif clear == :backspace
|
|
|
|
# Clear field by sending the correct number of backspace keys.
|
|
|
|
backspaces = [:backspace] * self.value.to_s.length
|
2018-08-17 16:57:12 -04:00
|
|
|
send_keys(*([:end] + backspaces + [value]))
|
2018-01-08 15:23:54 -05:00
|
|
|
elsif clear.is_a? Array
|
2018-08-17 16:57:12 -04:00
|
|
|
send_keys(*clear, value)
|
2017-08-01 16:59:17 -04:00
|
|
|
else
|
2019-06-10 15:46:08 -04:00
|
|
|
driver.execute_script 'arguments[0].select()', self unless clear == :none
|
2018-08-17 16:57:12 -04:00
|
|
|
send_keys(value)
|
2017-08-01 16:59:17 -04:00
|
|
|
end
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|
2017-11-16 21:32:26 -05:00
|
|
|
|
2018-09-01 14:20:11 -04:00
|
|
|
def click_with_options(click_options)
|
|
|
|
scroll_if_needed do
|
|
|
|
action_with_modifiers(click_options) do |action|
|
|
|
|
if block_given?
|
|
|
|
yield action
|
|
|
|
else
|
|
|
|
click_options.coords? ? action.click : action.click(native)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 15:44:47 -04:00
|
|
|
def set_date(value) # rubocop:disable Naming/AccessorMethodName
|
2018-08-17 16:57:12 -04:00
|
|
|
value = SettableValue.new(value)
|
|
|
|
return set_text(value) unless value.dateable?
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2018-05-09 15:44:47 -04:00
|
|
|
# TODO: this would be better if locale can be detected and correct keystrokes sent
|
2018-08-17 16:57:12 -04:00
|
|
|
update_value_js(value.to_date_str)
|
2018-03-09 17:23:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_time(value) # rubocop:disable Naming/AccessorMethodName
|
2018-08-17 16:57:12 -04:00
|
|
|
value = SettableValue.new(value)
|
|
|
|
return set_text(value) unless value.timeable?
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2018-05-09 15:44:47 -04:00
|
|
|
# TODO: this would be better if locale can be detected and correct keystrokes sent
|
2018-08-17 16:57:12 -04:00
|
|
|
update_value_js(value.to_time_str)
|
2018-03-09 17:23:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_datetime_local(value) # rubocop:disable Naming/AccessorMethodName
|
2018-08-17 16:57:12 -04:00
|
|
|
value = SettableValue.new(value)
|
|
|
|
return set_text(value) unless value.timeable?
|
2018-09-24 12:43:46 -04:00
|
|
|
|
2018-05-09 15:44:47 -04:00
|
|
|
# TODO: this would be better if locale can be detected and correct keystrokes sent
|
2018-08-17 16:57:12 -04:00
|
|
|
update_value_js(value.to_datetime_str)
|
2018-05-09 15:44:47 -04:00
|
|
|
end
|
|
|
|
|
2019-06-05 12:11:31 -04:00
|
|
|
def set_color(value) # rubocop:disable Naming/AccessorMethodName
|
|
|
|
update_value_js(value)
|
|
|
|
end
|
|
|
|
|
2018-05-09 15:44:47 -04:00
|
|
|
def update_value_js(value)
|
|
|
|
driver.execute_script(<<-JS, self, value)
|
2019-04-02 13:10:41 -04:00
|
|
|
if (arguments[0].readOnly) { return };
|
2018-05-09 15:44:47 -04:00
|
|
|
if (document.activeElement !== arguments[0]){
|
|
|
|
arguments[0].focus();
|
|
|
|
}
|
|
|
|
if (arguments[0].value != arguments[1]) {
|
|
|
|
arguments[0].value = arguments[1]
|
|
|
|
arguments[0].dispatchEvent(new InputEvent('input'));
|
|
|
|
arguments[0].dispatchEvent(new Event('change', { bubbles: true }));
|
|
|
|
}
|
2018-06-06 19:11:47 -04:00
|
|
|
JS
|
2018-03-09 17:23:52 -05:00
|
|
|
end
|
|
|
|
|
2018-01-09 17:05:50 -05:00
|
|
|
def set_file(value) # rubocop:disable Naming/AccessorMethodName
|
2019-08-01 15:36:39 -04:00
|
|
|
with_file_detector do
|
|
|
|
path_names = value.to_s.empty? ? [] : value
|
|
|
|
file_names = Array(path_names).map do |pn|
|
|
|
|
Pathname.new(pn).absolute? ? pn : File.expand_path(pn)
|
|
|
|
end.join("\n")
|
|
|
|
native.send_keys(file_names)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_file_detector
|
|
|
|
if driver.options[:browser] == :remote &&
|
|
|
|
bridge.respond_to?(:file_detector) &&
|
|
|
|
bridge.file_detector.nil?
|
|
|
|
begin
|
|
|
|
bridge.file_detector = lambda do |(fn, *)|
|
|
|
|
str = fn.to_s
|
|
|
|
str if File.exist?(str)
|
|
|
|
end
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
bridge.file_detector = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
2017-11-13 16:04:47 -05:00
|
|
|
end
|
|
|
|
|
2018-01-09 17:05:50 -05:00
|
|
|
def set_content_editable(value) # rubocop:disable Naming/AccessorMethodName
|
|
|
|
# Ensure we are focused on the element
|
2017-11-13 16:04:47 -05:00
|
|
|
click
|
|
|
|
|
2019-04-30 12:49:01 -04:00
|
|
|
editable = driver.execute_script <<-JS, self
|
|
|
|
if (arguments[0].isContentEditable) {
|
|
|
|
var range = document.createRange();
|
|
|
|
var sel = window.getSelection();
|
|
|
|
arguments[0].focus();
|
|
|
|
range.selectNodeContents(arguments[0]);
|
|
|
|
sel.removeAllRanges();
|
|
|
|
sel.addRange(range);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-11-13 16:04:47 -05:00
|
|
|
JS
|
|
|
|
|
2018-01-03 19:03:10 -05:00
|
|
|
# The action api has a speed problem but both chrome and firefox 58 raise errors
|
|
|
|
# if we use the faster direct send_keys. For now just send_keys to the element
|
|
|
|
# we've already focused.
|
|
|
|
# native.send_keys(value.to_s)
|
2019-04-30 12:49:01 -04:00
|
|
|
browser_action.send_keys(value.to_s).perform if editable
|
2017-11-13 16:04:47 -05:00
|
|
|
end
|
2017-12-29 15:37:08 -05:00
|
|
|
|
2018-08-17 16:57:12 -04:00
|
|
|
def action_with_modifiers(click_options)
|
2019-06-15 17:17:08 -04:00
|
|
|
actions = browser_action.tap do |acts|
|
|
|
|
if click_options.center_offset? && click_options.coords?
|
|
|
|
acts.move_to(native).move_by(*click_options.coords)
|
|
|
|
else
|
|
|
|
acts.move_to(native, *click_options.coords)
|
|
|
|
end
|
|
|
|
end
|
2018-08-17 16:57:12 -04:00
|
|
|
modifiers_down(actions, click_options.keys)
|
2017-12-29 15:37:08 -05:00
|
|
|
yield actions
|
2018-08-17 16:57:12 -04:00
|
|
|
modifiers_up(actions, click_options.keys)
|
2017-12-29 15:37:08 -05:00
|
|
|
actions.perform
|
|
|
|
ensure
|
2018-08-17 16:57:12 -04:00
|
|
|
act = browser_action
|
2018-08-20 19:46:31 -04:00
|
|
|
act.release_actions if act.respond_to?(:release_actions)
|
2017-12-29 15:37:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def modifiers_down(actions, keys)
|
2018-08-17 16:57:12 -04:00
|
|
|
each_key(keys) { |key| actions.key_down(key) }
|
2017-12-29 15:37:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def modifiers_up(actions, keys)
|
2018-08-17 16:57:12 -04:00
|
|
|
each_key(keys) { |key| actions.key_up(key) }
|
|
|
|
end
|
|
|
|
|
2019-01-03 16:00:38 -05:00
|
|
|
def browser
|
|
|
|
driver.browser
|
|
|
|
end
|
|
|
|
|
2019-08-01 15:36:39 -04:00
|
|
|
def bridge
|
|
|
|
browser.send(:bridge)
|
|
|
|
end
|
|
|
|
|
2018-08-17 16:57:12 -04:00
|
|
|
def browser_action
|
2019-01-03 16:00:38 -05:00
|
|
|
browser.action
|
2018-08-17 16:57:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def each_key(keys)
|
2017-12-29 15:37:08 -05:00
|
|
|
keys.each do |key|
|
|
|
|
key = case key
|
|
|
|
when :ctrl then :control
|
|
|
|
when :command, :cmd then :meta
|
|
|
|
else
|
|
|
|
key
|
|
|
|
end
|
2018-08-17 16:57:12 -04:00
|
|
|
yield key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 16:00:38 -05:00
|
|
|
def find_context
|
|
|
|
native
|
|
|
|
end
|
|
|
|
|
2019-01-08 18:51:05 -05:00
|
|
|
def build_node(native_node, initial_cache = {})
|
|
|
|
self.class.new(driver, native_node, initial_cache)
|
2019-01-03 16:00:38 -05:00
|
|
|
end
|
|
|
|
|
2019-04-02 13:10:41 -04:00
|
|
|
def attrs(*attr_names)
|
|
|
|
return attr_names.map { |name| self[name.to_s] } if ENV['CAPYBARA_THOROUGH']
|
|
|
|
|
|
|
|
driver.evaluate_script <<~'JS', self, attr_names.map(&:to_s)
|
|
|
|
(function(el, names){
|
|
|
|
return names.map(function(name){
|
|
|
|
return el[name]
|
|
|
|
});
|
|
|
|
})(arguments[0], arguments[1]);
|
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
2018-12-29 16:13:33 -05:00
|
|
|
GET_XPATH_SCRIPT = <<~'JS'
|
|
|
|
(function(el, xml){
|
|
|
|
var xpath = '';
|
|
|
|
var pos, tempitem2;
|
|
|
|
|
|
|
|
while(el !== xml.documentElement) {
|
|
|
|
pos = 0;
|
|
|
|
tempitem2 = el;
|
|
|
|
while(tempitem2) {
|
|
|
|
if (tempitem2.nodeType === 1 && tempitem2.nodeName === el.nodeName) { // If it is ELEMENT_NODE of the same name
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
tempitem2 = tempitem2.previousSibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (el.namespaceURI != xml.documentElement.namespaceURI) {
|
|
|
|
xpath = "*[local-name()='"+el.nodeName+"' and namespace-uri()='"+(el.namespaceURI===null?'':el.namespaceURI)+"']["+pos+']'+'/'+xpath;
|
|
|
|
} else {
|
|
|
|
xpath = el.nodeName.toUpperCase()+"["+pos+"]/"+xpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
el = el.parentNode;
|
|
|
|
}
|
|
|
|
xpath = '/'+xml.documentElement.nodeName.toUpperCase()+'/'+xpath;
|
|
|
|
xpath = xpath.replace(/\/$/, '');
|
|
|
|
return xpath;
|
|
|
|
})(arguments[0], document)
|
|
|
|
JS
|
|
|
|
|
2019-05-10 11:07:57 -04:00
|
|
|
OBSCURED_OR_OFFSET_SCRIPT = <<~'JS'
|
|
|
|
(function(el, x, y) {
|
|
|
|
var box = el.getBoundingClientRect();
|
|
|
|
if (x == null) x = box.width/2;
|
|
|
|
if (y == null) y = box.height/2 ;
|
|
|
|
|
|
|
|
var px = box.left + x,
|
|
|
|
py = box.top + y,
|
|
|
|
e = document.elementFromPoint(px, py);
|
|
|
|
|
|
|
|
if (!el.contains(e))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return { x: px, y: py };
|
|
|
|
})(arguments[0], arguments[1], arguments[2])
|
|
|
|
JS
|
|
|
|
|
2018-08-17 16:57:12 -04:00
|
|
|
# SettableValue encapsulates time/date field formatting
|
|
|
|
class SettableValue
|
|
|
|
attr_reader :value
|
|
|
|
|
|
|
|
def initialize(value)
|
|
|
|
@value = value
|
|
|
|
end
|
|
|
|
|
2018-09-21 14:43:15 -04:00
|
|
|
def to_s
|
|
|
|
value.to_s
|
|
|
|
end
|
|
|
|
|
2018-08-17 16:57:12 -04:00
|
|
|
def dateable?
|
|
|
|
!value.is_a?(String) && value.respond_to?(:to_date)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_date_str
|
2019-03-07 16:50:59 -05:00
|
|
|
value.to_date.iso8601
|
2018-08-17 16:57:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def timeable?
|
|
|
|
!value.is_a?(String) && value.respond_to?(:to_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_time_str
|
|
|
|
value.to_time.strftime('%H:%M')
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_datetime_str
|
|
|
|
value.to_time.strftime('%Y-%m-%dT%H:%M')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private_constant :SettableValue
|
|
|
|
|
|
|
|
# ClickOptions encapsulates click option logic
|
|
|
|
class ClickOptions
|
|
|
|
attr_reader :keys, :options
|
|
|
|
|
|
|
|
def initialize(keys, options)
|
|
|
|
@keys = keys
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
def coords?
|
|
|
|
options[:x] && options[:y]
|
|
|
|
end
|
|
|
|
|
|
|
|
def coords
|
|
|
|
[options[:x], options[:y]]
|
|
|
|
end
|
|
|
|
|
2019-06-15 17:17:08 -04:00
|
|
|
def center_offset?
|
|
|
|
options[:offset] == :center
|
|
|
|
end
|
|
|
|
|
2018-08-17 16:57:12 -04:00
|
|
|
def empty?
|
|
|
|
keys.empty? && !coords?
|
2017-12-29 15:37:08 -05:00
|
|
|
end
|
|
|
|
end
|
2018-08-17 16:57:12 -04:00
|
|
|
private_constant :ClickOptions
|
2011-04-11 01:24:00 -04:00
|
|
|
end
|