1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/selenium/node.rb

329 lines
9.1 KiB
Ruby
Raw Normal View History

2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
2018-01-08 15:23:54 -05:00
2011-04-11 01:24:00 -04:00
class Capybara::Selenium::Node < Capybara::Driver::Node
2013-02-17 08:58:41 -05:00
def visible_text
native.text
2011-04-11 01:24:00 -04:00
end
2013-02-17 08:58:41 -05:00
def all_text
2017-03-07 19:32:02 -05:00
text = driver.execute_script("return arguments[0].textContent", self)
text.gsub(/[\u200b\u200e\u200f]/, '')
.gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
2018-03-09 13:02:15 -05:00
.gsub(/\A[[:space:]&&[^\u00a0]]+/, "")
.gsub(/[[:space:]&&[^\u00a0]]+\z/, "")
.tr("\u00a0", ' ')
2013-02-17 08:58:41 -05:00
end
2011-04-11 01:24:00 -04:00
def [](name)
native.attribute(name.to_s)
2011-04-11 01:24:00 -04:00
rescue Selenium::WebDriver::Error::WebDriverError
nil
end
def value
2018-05-14 17:30:34 -04:00
if tag_name == "select" && multiple?
native.find_elements(:css, "option:checked").map { |n| n[:value] || n.text }
2011-04-11 01:24:00 -04:00
else
native[:value]
2011-04-11 01:24:00 -04:00
end
end
##
#
# 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)
2018-01-11 19:45:50 -05:00
raise ArgumentError, "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}" if value.is_a?(Array) && !multiple?
case tag_name
when 'input'
2018-01-13 16:06:03 -05:00
case self[:type]
when 'radio'
click
when 'checkbox'
2018-01-11 19:45:50 -05:00
click if value ^ checked?
when 'file'
2017-11-13 16:04:47 -05:00
set_file(value)
when 'date'
set_date(value)
when 'time'
set_time(value)
when 'datetime-local'
set_datetime_local(value)
else
set_text(value, options)
end
when 'textarea'
set_text(value, options)
else
2018-01-09 17:05:50 -05:00
set_content_editable(value) if content_editable?
2011-04-11 01:24:00 -04:00
end
end
def select_option
2016-06-10 20:22:17 -04:00
native.click unless selected? || disabled?
2011-04-11 01:24:00 -04:00
end
def unselect_option
2018-01-09 17:05:50 -05:00
raise Capybara::UnselectNotAllowed, "Cannot unselect option from single select box." unless select_node.multiple?
2012-02-01 08:23:17 -05:00
native.click if selected?
2011-04-11 01:24:00 -04:00
end
2018-05-17 17:45:53 -04:00
def click(keys = [], **options)
if keys.empty? && !coords?(options)
native.click
else
scroll_if_needed do
2018-01-08 15:33:47 -05:00
action_with_modifiers(keys, options) do |a|
2018-05-17 17:45:53 -04:00
coords?(options) ? a.click : a.click(native)
end
end
end
2018-05-14 17:30:34 -04:00
rescue StandardError => e
if e.is_a?(::Selenium::WebDriver::Error::ElementClickInterceptedError) ||
e.message =~ /Other element would receive the click/
2018-05-15 12:17:09 -04:00
scroll_to_center
end
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)
scroll_if_needed do
2018-01-08 15:33:47 -05:00
action_with_modifiers(keys, options) do |a|
2018-05-17 17:45:53 -04:00
coords?(options) ? a.context_click : a.context_click(native)
end
end
end
2015-04-13 12:24:13 -04:00
2018-05-17 17:45:53 -04:00
def double_click(keys = [], **options)
scroll_if_needed do
2018-01-08 15:33:47 -05:00
action_with_modifiers(keys, options) do |a|
2018-05-17 17:45:53 -04:00
coords?(options) ? a.double_click : a.double_click(native)
end
end
end
2015-04-13 12:24:13 -04: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-01-13 16:06:03 -05:00
scroll_if_needed { driver.browser.action.move_to(native).perform }
2013-02-25 13:37:25 -05:00
end
2011-04-11 01:24:00 -04:00
def drag_to(element)
2018-01-13 16:06:03 -05:00
scroll_if_needed { driver.browser.action.drag_and_drop(native, element.native).perform }
2011-04-11 01:24:00 -04:00
end
def 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
alias :checked? :selected?
2011-04-11 01:24:00 -04:00
def disabled?
!native.enabled?
end
def content_editable?
native.attribute('isContentEditable')
end
def find_xpath(locator)
2011-04-11 01:24:00 -04:00
native.find_elements(:xpath, locator).map { |n| self.class.new(driver, n) }
end
def find_css(locator)
native.find_elements(:css, locator).map { |n| self.class.new(driver, n) }
end
def ==(other)
native == other.native
end
def path
2018-01-13 16:06:03 -05:00
path = find_xpath(XPath.ancestor_or_self).reverse
result = []
default_ns = path.last[:namespaceURI]
2017-11-13 16:04:47 -05:00
while (node = path.shift)
parent = path.first
selector = node[:tagName]
if node[:namespaceURI] != default_ns
selector = XPath.child.where((XPath.local_name == selector) & (XPath.namespace_uri == node[:namespaceURI])).to_s
selector
end
if parent
2018-05-08 19:44:57 -04:00
siblings = parent.find_xpath(selector)
2018-01-13 16:06:03 -05:00
selector += "[#{siblings.index(node) + 1}]" unless siblings.size == 1
end
2018-01-13 16:06:03 -05:00
result.push selector
end
2018-01-13 16:06:03 -05:00
'/' + result.reverse.join('/')
end
2011-04-11 01:24:00 -04:00
private
2018-01-09 17:05:50 -05:00
2018-05-17 17:45:53 -04:00
def coords?(options)
2018-05-16 15:47:08 -04:00
options[:x] && options[:y]
end
2018-01-13 16:06:03 -05:00
def boolean_attr(val)
2018-05-14 17:30:34 -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
end
2018-03-16 12:46:35 -04:00
def set_text(value, clear: nil, **_unused)
2017-09-17 16:27:45 -04:00
if value.to_s.empty? && clear.nil?
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
native.send_keys(*(backspaces + [value.to_s]))
elsif clear == :none
native.send_keys(value.to_s)
elsif clear.is_a? Array
native.send_keys(*clear, value.to_s)
else
2018-01-08 15:23:54 -05:00
# Clear field by JavaScript assignment of the value property.
# Script can change a readonly element which user input cannot, so
# don't execute if readonly.
driver.execute_script "arguments[0].value = ''", self
native.send_keys(value.to_s)
end
2011-04-11 01:24:00 -04:00
end
2018-01-08 15:23:54 -05:00
def scroll_if_needed
yield
rescue ::Selenium::WebDriver::Error::MoveTargetOutOfBoundsError
2018-05-15 12:17:09 -04:00
scroll_to_center
yield
end
def scroll_to_center
2018-01-13 16:06:03 -05:00
script = <<-'JS'
try {
arguments[0].scrollIntoView({behavior: 'instant', block: 'center', inline: 'center'});
} catch(e) {
arguments[0].scrollIntoView(true);
}
JS
2018-05-15 12:17:09 -04:00
begin
driver.execute_script(script, self)
rescue StandardError # rubocop:disable Lint/HandleExceptions
# Swallow error if scrollIntoView with options isn't supported
end
end
2017-11-13 16:04:47 -05:00
def set_date(value) # rubocop:disable Naming/AccessorMethodName
return set_text(value) if value.is_a?(String) || !value.respond_to?(:to_date)
# TODO: this would be better if locale can be detected and correct keystrokes sent
update_value_js(value.to_date.strftime('%Y-%m-%d'))
end
def set_time(value) # rubocop:disable Naming/AccessorMethodName
return set_text(value) if value.is_a?(String) || !value.respond_to?(:to_time)
# TODO: this would be better if locale can be detected and correct keystrokes sent
update_value_js(value.to_time.strftime('%H:%M'))
end
def set_datetime_local(value) # rubocop:disable Naming/AccessorMethodName
return set_text(value) if value.is_a?(String) || !value.respond_to?(:to_time)
# TODO: this would be better if locale can be detected and correct keystrokes sent
update_value_js(value.to_time.strftime('%Y-%m-%dT%H:%M'))
end
def update_value_js(value)
driver.execute_script(<<-JS, self, value)
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 }));
}
JS
end
2018-01-09 17:05:50 -05:00
def set_file(value) # rubocop:disable Naming/AccessorMethodName
2017-11-13 16:04:47 -05:00
path_names = value.to_s.empty? ? [] : value
native.send_keys(Array(path_names).join("\n"))
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
script = <<-JS
var range = document.createRange();
var sel = window.getSelection();
arguments[0].focus();
range.selectNodeContents(arguments[0]);
sel.removeAllRanges();
sel.addRange(range);
JS
driver.execute_script script, self
# 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)
driver.browser.action.send_keys(value.to_s).perform
2017-11-13 16:04:47 -05:00
end
2018-01-08 15:33:47 -05:00
def action_with_modifiers(keys, x: nil, y: nil)
actions = driver.browser.action
actions.move_to(native, x, y)
modifiers_down(actions, keys)
yield actions
modifiers_up(actions, keys)
actions.perform
ensure
a = driver.browser.action
a.release_actions if a.respond_to?(:release_actions)
end
def modifiers_down(actions, keys)
keys.each do |key|
key = case key
when :ctrl then :control
when :command, :cmd then :meta
else
key
end
actions.key_down(key)
end
end
def modifiers_up(actions, keys)
keys.each do |key|
key = case key
when :ctrl then :control
when :command, :cmd then :meta
else
key
end
actions.key_up(key)
end
end
2011-04-11 01:24:00 -04:00
end