teamcapybara--capybara/lib/capybara/rack_test/node.rb

290 lines
7.1 KiB
Ruby
Raw Normal View History

2016-03-08 00:52:19 +00:00
# frozen_string_literal: true
2018-01-08 20:23:54 +00:00
require 'capybara/rack_test/errors'
class Capybara::RackTest::Node < Capybara::Driver::Node
BLOCK_ELEMENTS = %w[p h1 h2 h3 h4 h5 h6 ol ul pre address blockquote dl div fieldset form hr noscript table].freeze
def all_text
native.text
.gsub(/[\u200b\u200e\u200f]/, '')
.gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
2018-07-10 21:18:39 +00:00
.gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
.gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
.tr("\u00a0", ' ')
end
def visible_text
displayed_text.gsub(/\ +/, ' ')
2018-03-09 18:02:15 +00:00
.gsub(/[\ \n]*\n[\ \n]*/, "\n")
2018-07-10 21:18:39 +00:00
.gsub(/\A[[:space:]&&[^\u00a0]]+/, '')
.gsub(/[[:space:]&&[^\u00a0]]+\z/, '')
.tr("\u00a0", ' ')
end
def [](name)
string_node[name]
end
def style(_styles)
2018-07-10 21:18:39 +00:00
raise NotImplementedError, 'The rack_test driver does not process CSS'
end
def value
string_node.value
end
def set(value, **options)
2017-11-13 18:43:35 +00:00
return if disabled? || readonly?
warn "Options passed to Node#set but the RackTest driver doesn't support any - ignoring" unless options.empty?
2018-01-09 22:05:50 +00:00
if value.is_a?(Array) && !multiple?
raise TypeError, "Value cannot be an Array when 'multiple' attribute is not present. Not a #{value.class}"
end
2013-01-10 02:19:36 +00:00
2018-01-13 21:06:03 +00:00
if radio? then set_radio(value)
elsif checkbox? then set_checkbox(value)
elsif input_field? then set_input(value)
elsif textarea? then native['_capybara_raw_value'] = value.to_s
end
end
def select_option
return if disabled?
2018-09-24 16:43:46 +00:00
2018-01-13 21:06:03 +00:00
deselect_options unless select_node.multiple?
2018-07-10 21:18:39 +00:00
native['selected'] = 'selected'
end
def unselect_option
2018-07-10 21:18:39 +00:00
raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.' unless select_node.multiple?
2018-09-24 16:43:46 +00:00
native.remove_attribute('selected')
end
2018-05-17 21:45:53 +00:00
def click(keys = [], **offset)
2018-07-10 21:18:39 +00:00
raise ArgumentError, 'The RackTest driver does not support click options' unless keys.empty? && offset.empty?
2018-01-13 21:06:03 +00:00
if link?
2018-01-12 00:45:50 +00:00
follow_link
2018-01-13 21:06:03 +00:00
elsif submits?
associated_form = form
Capybara::RackTest::Form.new(driver, associated_form).submit(self) if associated_form
2018-01-13 21:06:03 +00:00
elsif checkable?
set(!checked?)
2018-01-09 22:05:50 +00:00
elsif tag_name == 'label'
2018-01-12 00:45:50 +00:00
click_label
end
end
def tag_name
native.node_name
end
def visible?
string_node.visible?
end
def checked?
string_node.checked?
end
def selected?
string_node.selected?
end
2013-01-29 09:35:23 +00:00
def disabled?
2018-01-13 21:06:03 +00:00
return true if string_node.disabled?
2018-01-09 22:05:50 +00:00
if %w[option optgroup].include? tag_name
find_xpath(OPTION_OWNER_XPATH)[0].disabled?
else
!find_xpath(DISABLED_BY_FIELDSET_XPATH).empty?
end
2013-01-29 09:35:23 +00:00
end
def path
native.path
end
def find_xpath(locator, **_hints)
native.xpath(locator).map { |el| self.class.new(driver, el) }
end
def find_css(locator, **_hints)
native.css(locator, Capybara::RackTest::CSSHandlers.new).map { |el| self.class.new(driver, el) }
end
2019-02-19 00:40:27 +00:00
public_instance_methods(false).each do |meth_name|
alias_method "unchecked_#{meth_name}", meth_name
2019-04-29 18:11:01 +00:00
private "unchecked_#{meth_name}" # rubocop:disable Style/AccessModifierDeclarations
2019-02-19 00:40:27 +00:00
define_method meth_name do |*args|
stale_check
send("unchecked_#{meth_name}", *args)
end
end
def ==(other)
native == other.native
end
2019-02-19 00:40:27 +00:00
protected
# @api private
def displayed_text(check_ancestor: true)
if !string_node.visible?(check_ancestor)
''
elsif native.text?
native.text
.gsub(/[\u200b\u200e\u200f]/, '')
.gsub(/[\ \n\f\t\v\u2028\u2029]+/, ' ')
elsif native.element?
text = native.children.map do |child|
Capybara::RackTest::Node.new(driver, child).displayed_text(check_ancestor: false)
end.join || ''
text = "\n#{text}\n" if BLOCK_ELEMENTS.include?(tag_name)
text
else
''
end
end
private
def stale_check
raise Capybara::RackTest::Errors::StaleElementReferenceError unless native.document == driver.dom
end
2018-01-13 21:06:03 +00:00
def deselect_options
2018-07-10 21:18:39 +00:00
select_node.find_xpath('.//option[@selected]').each { |node| node.native.remove_attribute('selected') }
2018-01-13 21:06:03 +00:00
end
def string_node
@string_node ||= Capybara::Node::Simple.new(native)
end
# a reference to the select node if this is an option node
def select_node
find_xpath('./ancestor::select[1]').first
end
def type
native[:type]
end
def form
if native[:form]
2018-01-13 21:06:03 +00:00
native.xpath("//form[@id='#{native[:form]}']")
else
2018-01-13 21:06:03 +00:00
native.ancestors('form')
end.first
end
2013-01-10 02:19:36 +00:00
2018-01-09 22:05:50 +00:00
def set_radio(_value) # rubocop:disable Naming/AccessorMethodName
other_radios_xpath = XPath.generate { |xp| xp.anywhere(:input)[xp.attr(:name) == self[:name]] }.to_s
2018-07-10 21:18:39 +00:00
driver.dom.xpath(other_radios_xpath).each { |node| node.remove_attribute('checked') }
2013-01-10 02:19:36 +00:00
native['checked'] = 'checked'
end
2018-01-09 22:05:50 +00:00
def set_checkbox(value) # rubocop:disable Naming/AccessorMethodName
2013-01-10 02:19:36 +00:00
if value && !native['checked']
native['checked'] = 'checked'
elsif !value && native['checked']
native.remove_attribute('checked')
end
end
2018-01-09 22:05:50 +00:00
def set_input(value) # rubocop:disable Naming/AccessorMethodName
2013-01-10 02:19:36 +00:00
if text_or_password? && attribute_is_not_blank?(:maxlength)
# Browser behavior for maxlength="0" is inconsistent, so we stick with
# Firefox, allowing no input
value = value.to_s[0...self[:maxlength].to_i]
2013-01-10 02:19:36 +00:00
end
2018-01-09 22:05:50 +00:00
if value.is_a?(Array) # Assert multiple attribute is present
value.each do |val|
2013-01-10 02:19:36 +00:00
new_native = native.clone
new_native.remove_attribute('value')
native.add_next_sibling(new_native)
new_native['value'] = val.to_s
2013-01-10 02:19:36 +00:00
end
native.remove
else
native['value'] = value.to_s
2013-01-10 02:19:36 +00:00
end
end
def attribute_is_not_blank?(attribute)
self[attribute] && !self[attribute].empty?
end
2018-01-12 00:45:50 +00:00
def follow_link
2018-07-10 21:18:39 +00:00
method = self['data-method'] if driver.options[:respect_data_method]
2018-01-12 00:45:50 +00:00
method ||= :get
driver.follow(method, self[:href].to_s)
end
def click_label
labelled_control = if native[:for]
2018-01-13 21:06:03 +00:00
find_xpath("//input[@id='#{native[:for]}']")
2018-01-12 00:45:50 +00:00
else
2018-07-10 21:18:39 +00:00
find_xpath('.//input')
2018-01-13 21:06:03 +00:00
end.first
2018-01-12 00:45:50 +00:00
labelled_control.set(!labelled_control.checked?) if checkbox_or_radio?(labelled_control)
2018-01-12 00:45:50 +00:00
end
2018-01-13 21:06:03 +00:00
def link?
tag_name == 'a' && !self[:href].nil?
end
def submits?
2018-07-10 21:18:39 +00:00
(tag_name == 'input' && %w[submit image].include?(type)) || (tag_name == 'button' && [nil, 'submit'].include?(type))
2018-01-13 21:06:03 +00:00
end
def checkable?
2018-05-14 21:30:34 +00:00
tag_name == 'input' && %w[checkbox radio].include?(type)
2018-01-13 21:06:03 +00:00
end
protected
def checkbox_or_radio?(field = self)
2018-05-16 19:47:08 +00:00
field&.checkbox? || field&.radio?
end
2013-01-10 02:19:36 +00:00
def checkbox?
input_field? && type == 'checkbox'
end
def radio?
input_field? && type == 'radio'
end
2018-05-16 19:47:08 +00:00
def text_or_password?
input_field? && (type == 'text' || type == 'password')
end
def input_field?
tag_name == 'input'
end
2013-01-10 02:19:36 +00:00
def textarea?
2018-07-10 21:18:39 +00:00
tag_name == 'textarea'
2013-01-10 02:19:36 +00:00
end
OPTION_OWNER_XPATH = XPath.parent(:optgroup, :select, :datalist).to_s.freeze
DISABLED_BY_FIELDSET_XPATH = XPath.generate do |x|
x.parent(:fieldset)[
XPath.attr(:disabled)
] + x.ancestor[
~x.self(:legend) |
x.preceding_sibling(:legend)
][
x.parent(:fieldset)[
x.attr(:disabled)
]
]
end.to_s.freeze
end