2016-03-07 16:52:19 -08:00
|
|
|
# frozen_string_literal: true
|
2018-01-08 12:23:54 -08:00
|
|
|
|
2016-08-18 13:27:35 -07:00
|
|
|
require 'capybara/selector/selector'
|
2016-04-26 22:44:48 -07:00
|
|
|
Capybara::Selector::FilterSet.add(:_field) do
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:checked, :boolean) { |node, value| !(value ^ node.checked?) }
|
|
|
|
node_filter(:unchecked, :boolean) { |node, value| (value ^ node.checked?) }
|
|
|
|
node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }
|
|
|
|
node_filter(:multiple, :boolean) { |node, value| !(value ^ node.multiple?) }
|
2016-04-26 22:44:48 -07:00
|
|
|
|
2018-01-02 17:46:41 -08:00
|
|
|
expression_filter(:name) { |xpath, val| xpath[XPath.attr(:name) == val] }
|
2018-01-09 14:05:50 -08:00
|
|
|
expression_filter(:placeholder) { |xpath, val| xpath[XPath.attr(:placeholder) == val] }
|
2016-10-05 15:16:00 -07:00
|
|
|
|
2017-11-13 13:04:47 -08:00
|
|
|
describe do |checked: nil, unchecked: nil, disabled: nil, multiple: nil, **_options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc, states = +"", []
|
2017-05-01 18:39:08 -07:00
|
|
|
states << 'checked' if checked || (unchecked == false)
|
|
|
|
states << 'not checked' if unchecked || (checked == false)
|
|
|
|
states << 'disabled' if disabled == true
|
|
|
|
states << 'not disabled' if disabled == false
|
2016-04-26 22:44:48 -07:00
|
|
|
desc << " that is #{states.join(' and ')}" unless states.empty?
|
2017-05-01 18:39:08 -07:00
|
|
|
desc << " with the multiple attribute" if multiple == true
|
|
|
|
desc << " without the multiple attribute" if multiple == false
|
2016-04-26 22:44:48 -07:00
|
|
|
desc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-08 12:23:54 -08:00
|
|
|
# rubocop:disable Metrics/BlockLength
|
|
|
|
|
2016-08-18 13:27:35 -07:00
|
|
|
Capybara.add_selector(:xpath) do
|
|
|
|
xpath { |xpath| xpath }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:css) do
|
|
|
|
css { |css| css }
|
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:id) do
|
|
|
|
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
|
|
|
|
end
|
2016-09-19 14:47:41 -07:00
|
|
|
|
2011-08-26 22:16:15 -04:00
|
|
|
Capybara.add_selector(:field) do
|
2016-11-13 18:32:23 -08:00
|
|
|
xpath do |locator, **options|
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = XPath.descendant(:input, :textarea, :select)[!XPath.attr(:type).one_of('submit', 'image', 'hidden')]
|
2016-10-05 15:16:00 -07:00
|
|
|
locate_field(xpath, locator, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
expression_filter(:type) do |expr, type|
|
|
|
|
type = type.to_s
|
2018-01-09 14:05:50 -08:00
|
|
|
if %w[textarea select].include?(type)
|
2017-05-25 15:55:34 -07:00
|
|
|
expr.self(type.to_sym)
|
2016-10-05 15:16:00 -07:00
|
|
|
else
|
2018-01-02 17:46:41 -08:00
|
|
|
expr[XPath.attr(:type) == type]
|
2016-08-18 13:27:35 -07:00
|
|
|
end
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
2016-04-26 08:51:22 -07:00
|
|
|
|
2016-10-05 15:16:00 -07:00
|
|
|
filter_set(:_field) # checked/unchecked/disabled/multiple/name/placeholder
|
2016-04-26 08:51:22 -07:00
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:readonly, :boolean) { |node, value| !(value ^ node.readonly?) }
|
|
|
|
node_filter(:with) do |node, with|
|
2016-07-28 10:25:24 +02:00
|
|
|
with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s
|
|
|
|
end
|
2017-05-01 18:39:08 -07:00
|
|
|
describe do |type: nil, **options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2018-01-09 14:05:50 -08:00
|
|
|
(expression_filters.keys - [:type]).each { |ef| desc << " with #{ef} #{options[ef]}" if options.key?(ef) }
|
2017-05-01 18:39:08 -07:00
|
|
|
desc << " of type #{type.inspect}" if type
|
2018-01-09 14:05:50 -08:00
|
|
|
desc << " with value #{options[:with].to_s.inspect}" if options.key?(:with)
|
2014-07-02 11:17:53 -07:00
|
|
|
desc
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
|
|
|
|
2011-08-27 17:57:12 -04:00
|
|
|
Capybara.add_selector(:fieldset) do
|
2017-11-13 13:04:47 -08:00
|
|
|
xpath(:legend) do |locator, legend: nil, **_options|
|
2016-03-24 11:18:12 -07:00
|
|
|
xpath = XPath.descendant(:fieldset)
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = xpath[(XPath.attr(:id) == locator.to_s) | XPath.child(:legend)[XPath.string.n.is(locator.to_s)]] unless locator.nil?
|
2016-11-13 18:32:23 -08:00
|
|
|
xpath = xpath[XPath.child(:legend)[XPath.string.n.is(legend)]] if legend
|
2016-03-24 11:18:12 -07:00
|
|
|
xpath
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
2016-09-09 16:44:32 -07:00
|
|
|
|
2011-08-26 22:16:15 -04:00
|
|
|
Capybara.add_selector(:link) do
|
2017-11-13 13:04:47 -08:00
|
|
|
xpath(:title, :alt) do |locator, href: true, enable_aria_label: false, alt: nil, title: nil, **_options|
|
2016-12-15 10:59:01 -08:00
|
|
|
xpath = XPath.descendant(:a)
|
2018-05-08 16:44:57 -07:00
|
|
|
xpath = xpath[
|
|
|
|
case href
|
|
|
|
when nil, false
|
|
|
|
!XPath.attr(:href)
|
|
|
|
when true
|
|
|
|
XPath.attr(:href)
|
|
|
|
when Regexp
|
|
|
|
nil # needs to be handled in filter
|
|
|
|
else
|
|
|
|
XPath.attr(:href) == href.to_s
|
|
|
|
end
|
|
|
|
]
|
|
|
|
|
2016-03-24 11:18:12 -07:00
|
|
|
unless locator.nil?
|
|
|
|
locator = locator.to_s
|
2018-01-11 16:45:50 -08:00
|
|
|
matchers = [XPath.attr(:id) == locator,
|
2018-01-08 12:23:54 -08:00
|
|
|
XPath.string.n.is(locator),
|
|
|
|
XPath.attr(:title).is(locator),
|
2018-05-16 12:47:08 -07:00
|
|
|
XPath.descendant(:img)[XPath.attr(:alt).is(locator)]]
|
|
|
|
matchers << XPath.attr(:'aria-label').is(locator) if enable_aria_label
|
|
|
|
xpath = xpath[matchers.reduce(:|)]
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
2018-05-08 16:44:57 -07:00
|
|
|
|
2017-05-01 18:39:08 -07:00
|
|
|
xpath = xpath[find_by_attr(:title, title)]
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = xpath[XPath.descendant(:img)[XPath.attr(:alt) == alt]] if alt
|
2016-03-24 11:18:12 -07:00
|
|
|
xpath
|
|
|
|
end
|
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:href) do |node, href|
|
2018-05-08 16:44:57 -07:00
|
|
|
# If not a Regexp it's been handled in the main XPath
|
|
|
|
href.is_a?(Regexp) ? node[:href].match(href) : true
|
2012-01-03 09:43:11 +01:00
|
|
|
end
|
2016-03-24 11:18:12 -07:00
|
|
|
|
2017-05-01 18:39:08 -07:00
|
|
|
describe do |**options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2016-12-15 10:59:01 -08:00
|
|
|
desc << " with href #{options[:href].inspect}" if options[:href]
|
|
|
|
desc << " with no href attribute" if options.fetch(:href, true).nil?
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:button) do
|
2018-05-16 12:47:08 -07:00
|
|
|
xpath(:value, :title, :type) do |locator, enable_aria_label: false, **options|
|
2016-03-24 11:18:12 -07:00
|
|
|
input_btn_xpath = XPath.descendant(:input)[XPath.attr(:type).one_of('submit', 'reset', 'image', 'button')]
|
|
|
|
btn_xpath = XPath.descendant(:button)
|
2018-01-02 17:46:41 -08:00
|
|
|
image_btn_xpath = XPath.descendant(:input)[XPath.attr(:type) == 'image']
|
2016-03-24 11:18:12 -07:00
|
|
|
|
|
|
|
unless locator.nil?
|
|
|
|
locator = locator.to_s
|
2018-01-02 17:46:41 -08:00
|
|
|
locator_matches = XPath.attr(:id).equals(locator) | XPath.attr(:value).is(locator) | XPath.attr(:title).is(locator)
|
2018-05-16 12:47:08 -07:00
|
|
|
locator_matches |= XPath.attr(:'aria-label').is(locator) if enable_aria_label
|
2016-08-03 13:16:30 -07:00
|
|
|
|
|
|
|
input_btn_xpath = input_btn_xpath[locator_matches]
|
|
|
|
|
2018-01-02 17:46:41 -08:00
|
|
|
btn_xpath = btn_xpath[locator_matches | XPath.string.n.is(locator) | XPath.descendant(:img)[XPath.attr(:alt).is(locator)]]
|
2016-08-03 13:16:30 -07:00
|
|
|
|
|
|
|
alt_matches = XPath.attr(:alt).is(locator)
|
2018-05-16 12:47:08 -07:00
|
|
|
alt_matches |= XPath.attr(:'aria-label').is(locator) if enable_aria_label
|
2016-08-03 13:16:30 -07:00
|
|
|
image_btn_xpath = image_btn_xpath[alt_matches]
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
|
|
|
|
2017-05-30 15:51:15 -07:00
|
|
|
res_xpath = input_btn_xpath.union(btn_xpath).union(image_btn_xpath)
|
2016-08-18 13:27:35 -07:00
|
|
|
|
2016-10-05 15:16:00 -07:00
|
|
|
res_xpath = expression_filters.keys.inject(res_xpath) { |memo, ef| memo[find_by_attr(ef, options[ef])] }
|
2016-08-18 13:27:35 -07:00
|
|
|
|
|
|
|
res_xpath
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }
|
2016-03-24 11:18:12 -07:00
|
|
|
|
2017-05-01 18:39:08 -07:00
|
|
|
describe do |disabled: nil, **options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2017-05-01 18:39:08 -07:00
|
|
|
desc << " that is disabled" if disabled == true
|
2016-11-21 16:28:45 -08:00
|
|
|
desc << describe_all_expression_filters(options)
|
2016-08-18 13:27:35 -07:00
|
|
|
desc
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
|
|
|
|
2016-03-24 11:18:12 -07:00
|
|
|
Capybara.add_selector(:link_or_button) do
|
|
|
|
label "link or button"
|
2017-05-01 18:39:08 -07:00
|
|
|
xpath do |locator, **options|
|
2018-01-09 14:05:50 -08:00
|
|
|
self.class.all.values_at(:link, :button).map { |selector| selector.xpath.call(locator, options) }.reduce(:union)
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| node.tag_name == "a" || !(value ^ node.disabled?) }
|
2016-03-24 11:18:12 -07:00
|
|
|
|
2017-11-13 13:04:47 -08:00
|
|
|
describe { |disabled: nil, **_options| " that is disabled" if disabled == true }
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
|
|
|
|
2011-08-26 22:16:15 -04:00
|
|
|
Capybara.add_selector(:fillable_field) do
|
2012-06-08 17:07:12 +02:00
|
|
|
label "field"
|
2017-05-01 18:39:08 -07:00
|
|
|
|
|
|
|
xpath do |locator, **options|
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = XPath.descendant(:input, :textarea)[!XPath.attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')]
|
2016-08-18 13:27:35 -07:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
|
|
|
|
2016-10-05 15:16:00 -07:00
|
|
|
expression_filter(:type) do |expr, type|
|
|
|
|
type = type.to_s
|
|
|
|
if ['textarea'].include?(type)
|
2017-05-25 15:55:34 -07:00
|
|
|
expr.self(type.to_sym)
|
2016-10-05 15:16:00 -07:00
|
|
|
else
|
2018-01-02 17:46:41 -08:00
|
|
|
expr[XPath.attr(:type) == type]
|
2016-10-05 15:16:00 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-08 12:23:54 -08:00
|
|
|
filter_set(:_field, %i[disabled multiple name placeholder])
|
2016-08-18 13:27:35 -07:00
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:with) do |node, with|
|
2016-09-19 14:47:41 -07:00
|
|
|
with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s
|
|
|
|
end
|
|
|
|
|
2016-08-18 13:27:35 -07:00
|
|
|
describe do |options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2016-11-21 16:28:45 -08:00
|
|
|
desc << describe_all_expression_filters(options)
|
2018-01-09 14:05:50 -08:00
|
|
|
desc << " with value #{options[:with].to_s.inspect}" if options.key?(:with)
|
2016-08-18 13:27:35 -07:00
|
|
|
desc
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:radio_button) do
|
2012-06-08 17:07:12 +02:00
|
|
|
label "radio button"
|
2017-05-01 18:39:08 -07:00
|
|
|
|
|
|
|
xpath do |locator, **options|
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = XPath.descendant(:input)[XPath.attr(:type) == 'radio']
|
2016-08-18 13:27:35 -07:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
|
|
|
|
2018-01-08 12:23:54 -08:00
|
|
|
filter_set(:_field, %i[checked unchecked disabled name])
|
2016-04-26 08:51:22 -07:00
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:option) { |node, value| node.value == value.to_s }
|
2016-03-24 11:18:12 -07:00
|
|
|
|
2017-09-21 10:28:03 -07:00
|
|
|
describe do |option: nil, **options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2017-05-01 18:39:08 -07:00
|
|
|
desc << " with value #{option.inspect}" if option
|
2016-11-21 16:28:45 -08:00
|
|
|
desc << describe_all_expression_filters(options)
|
2014-07-02 11:17:53 -07:00
|
|
|
desc
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:checkbox) do
|
2017-05-01 18:39:08 -07:00
|
|
|
xpath do |locator, **options|
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = XPath.descendant(:input)[XPath.attr(:type) == 'checkbox']
|
2016-08-18 13:27:35 -07:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
|
|
|
|
2018-01-08 12:23:54 -08:00
|
|
|
filter_set(:_field, %i[checked unchecked disabled name])
|
2016-04-26 08:51:22 -07:00
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:option) { |node, value| node.value == value.to_s }
|
2016-03-24 11:18:12 -07:00
|
|
|
|
2017-05-01 18:39:08 -07:00
|
|
|
describe do |option: nil, **options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2017-05-01 18:39:08 -07:00
|
|
|
desc << " with value #{option.inspect}" if option
|
2016-11-21 16:28:45 -08:00
|
|
|
desc << describe_all_expression_filters(options)
|
2014-07-02 11:17:53 -07:00
|
|
|
desc
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:select) do
|
2012-06-08 17:07:12 +02:00
|
|
|
label "select box"
|
2017-05-01 18:39:08 -07:00
|
|
|
|
|
|
|
xpath do |locator, **options|
|
2016-03-24 11:18:12 -07:00
|
|
|
xpath = XPath.descendant(:select)
|
2016-08-18 13:27:35 -07:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
|
|
|
|
2018-01-08 12:23:54 -08:00
|
|
|
filter_set(:_field, %i[disabled multiple name placeholder])
|
2016-04-26 08:51:22 -07:00
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:options) do |node, options|
|
2018-01-08 12:23:54 -08:00
|
|
|
actual = if node.visible?
|
2018-01-09 14:05:50 -08:00
|
|
|
node.all(:xpath, './/option', wait: false).map(&:text)
|
2015-10-06 14:42:45 -06:00
|
|
|
else
|
2018-01-08 12:23:54 -08:00
|
|
|
node.all(:xpath, './/option', visible: false, wait: false).map { |option| option.text(:all) }
|
2015-10-06 14:42:45 -06:00
|
|
|
end
|
2012-03-16 00:14:47 -03:00
|
|
|
options.sort == actual.sort
|
|
|
|
end
|
2016-10-06 18:04:14 -07:00
|
|
|
|
2018-05-08 16:44:57 -07:00
|
|
|
expression_filter(:with_options) do |expr, options|
|
2018-05-16 12:47:08 -07:00
|
|
|
options.inject(expr) do |xpath, option|
|
|
|
|
xpath[Capybara::Selector.all[:option].call(option)]
|
2018-05-08 16:44:57 -07:00
|
|
|
end
|
2015-10-06 14:42:45 -06:00
|
|
|
end
|
2016-10-06 18:04:14 -07:00
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:selected) do |node, selected|
|
2017-11-14 12:14:24 -08:00
|
|
|
actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }
|
2017-06-04 15:51:29 +02:00
|
|
|
Array(selected).sort == actual.sort
|
|
|
|
end
|
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:with_selected) do |node, selected|
|
2017-11-14 12:14:24 -08:00
|
|
|
actual = node.all(:xpath, './/option', visible: false, wait: false).select(&:selected?).map { |option| option.text(:all) }
|
2017-06-04 15:51:29 +02:00
|
|
|
(Array(selected) - actual).empty?
|
2012-01-02 17:12:21 +01:00
|
|
|
end
|
2016-03-24 11:18:12 -07:00
|
|
|
|
2017-05-01 18:39:08 -07:00
|
|
|
describe do |options: nil, with_options: nil, selected: nil, with_selected: nil, **opts|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2017-05-01 18:39:08 -07:00
|
|
|
desc << " with options #{options.inspect}" if options
|
|
|
|
desc << " with at least options #{with_options.inspect}" if with_options
|
|
|
|
desc << " with #{selected.inspect} selected" if selected
|
|
|
|
desc << " with at least #{with_selected.inspect} selected" if with_selected
|
|
|
|
desc << describe_all_expression_filters(opts)
|
2014-07-02 11:17:53 -07:00
|
|
|
desc
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
|
|
|
|
2018-04-26 03:04:31 -07:00
|
|
|
Capybara.add_selector(:datalist_input) do
|
|
|
|
label "input box with datalist completion"
|
|
|
|
|
|
|
|
xpath do |locator, **options|
|
|
|
|
xpath = XPath.descendant(:input)[XPath.attr(:list)]
|
|
|
|
locate_field(xpath, locator, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
filter_set(:_field, %i[disabled name placeholder])
|
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:options) do |node, options|
|
2018-04-26 03:04:31 -07:00
|
|
|
actual = node.find("//datalist[@id=#{node[:list]}]", visible: :all).all(:datalist_option, wait: false).map(&:value)
|
|
|
|
options.sort == actual.sort
|
|
|
|
end
|
|
|
|
|
2018-05-08 16:44:57 -07:00
|
|
|
expression_filter(:with_options) do |expr, options|
|
2018-05-16 12:47:08 -07:00
|
|
|
options.inject(expr) do |xpath, option|
|
|
|
|
xpath[XPath.attr(:list) == XPath.anywhere(:datalist)[Capybara::Selector.all[:datalist_option].call(option)].attr(:id)]
|
2018-05-08 16:44:57 -07:00
|
|
|
end
|
2018-04-26 03:04:31 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
describe do |options: nil, with_options: nil, **opts|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2018-04-26 03:04:31 -07:00
|
|
|
desc << " with options #{options.inspect}" if options
|
|
|
|
desc << " with at least options #{with_options.inspect}" if with_options
|
|
|
|
desc << describe_all_expression_filters(opts)
|
|
|
|
desc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-26 22:16:15 -04:00
|
|
|
Capybara.add_selector(:option) do
|
2016-03-24 11:18:12 -07:00
|
|
|
xpath do |locator|
|
|
|
|
xpath = XPath.descendant(:option)
|
|
|
|
xpath = xpath[XPath.string.n.is(locator.to_s)] unless locator.nil?
|
|
|
|
xpath
|
|
|
|
end
|
2016-03-24 14:50:42 -07:00
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }
|
|
|
|
node_filter(:selected, :boolean) { |node, value| !(value ^ node.selected?) }
|
2016-03-24 14:50:42 -07:00
|
|
|
|
2017-05-01 18:39:08 -07:00
|
|
|
describe do |**options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2018-01-09 14:05:50 -08:00
|
|
|
desc << " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)
|
|
|
|
desc << " that is#{' not' unless options[:selected]} selected" if options.key?(:selected)
|
2016-03-24 14:50:42 -07:00
|
|
|
desc
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
|
|
|
|
2018-04-26 03:04:31 -07:00
|
|
|
Capybara.add_selector(:datalist_option) do
|
|
|
|
label "datalist option"
|
|
|
|
visible(:all)
|
|
|
|
|
|
|
|
xpath do |locator|
|
|
|
|
xpath = XPath.descendant(:option)
|
|
|
|
xpath = xpath[XPath.string.n.is(locator.to_s) | (XPath.attr(:value) == locator.to_s)] unless locator.nil?
|
|
|
|
xpath
|
|
|
|
end
|
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }
|
2018-04-26 03:04:31 -07:00
|
|
|
|
|
|
|
describe do |**options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2018-04-26 03:04:31 -07:00
|
|
|
desc << " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)
|
|
|
|
desc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-26 22:16:15 -04:00
|
|
|
Capybara.add_selector(:file_field) do
|
2012-06-08 17:07:12 +02:00
|
|
|
label "file field"
|
2016-10-05 15:16:00 -07:00
|
|
|
xpath do |locator, options|
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = XPath.descendant(:input)[XPath.attr(:type) == 'file']
|
2016-08-18 13:27:35 -07:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 11:18:12 -07:00
|
|
|
end
|
|
|
|
|
2018-01-08 12:23:54 -08:00
|
|
|
filter_set(:_field, %i[disabled multiple name])
|
2016-08-18 13:27:35 -07:00
|
|
|
|
2017-05-01 18:39:08 -07:00
|
|
|
describe do |**options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2016-11-21 16:28:45 -08:00
|
|
|
desc << describe_all_expression_filters(options)
|
2016-08-18 13:27:35 -07:00
|
|
|
desc
|
|
|
|
end
|
2011-08-26 22:16:15 -04:00
|
|
|
end
|
2011-08-27 17:54:55 -04:00
|
|
|
|
2016-04-06 10:59:11 -07:00
|
|
|
Capybara.add_selector(:label) do
|
|
|
|
label "label"
|
2016-09-28 11:49:08 -07:00
|
|
|
xpath(:for) do |locator, options|
|
2016-04-06 10:59:11 -07:00
|
|
|
xpath = XPath.descendant(:label)
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = xpath[XPath.string.n.is(locator.to_s) | (XPath.attr(:id) == locator.to_s)] unless locator.nil?
|
2018-01-09 14:05:50 -08:00
|
|
|
if options.key?(:for) && !options[:for].is_a?(Capybara::Node::Element)
|
2018-01-11 16:45:50 -08:00
|
|
|
with_attr = XPath.attr(:for) == options[:for].to_s
|
2018-01-09 14:05:50 -08:00
|
|
|
labelable_elements = %i[button input keygen meter output progress select textarea]
|
2018-01-02 17:46:41 -08:00
|
|
|
wrapped = !XPath.attr(:for) &
|
|
|
|
XPath.descendant(*labelable_elements)[XPath.attr(:id) == options[:for].to_s]
|
|
|
|
xpath = xpath[with_attr | wrapped]
|
2016-09-28 11:49:08 -07:00
|
|
|
end
|
2016-04-06 10:59:11 -07:00
|
|
|
xpath
|
|
|
|
end
|
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:for) do |node, field_or_value|
|
2016-04-06 10:59:11 -07:00
|
|
|
if field_or_value.is_a? Capybara::Node::Element
|
2016-09-28 11:49:08 -07:00
|
|
|
if node[:for]
|
|
|
|
field_or_value[:id] == node[:for]
|
2016-04-06 10:59:11 -07:00
|
|
|
else
|
2016-04-06 14:20:23 -07:00
|
|
|
field_or_value.find_xpath('./ancestor::label[1]').include? node.base
|
2016-04-06 10:59:11 -07:00
|
|
|
end
|
|
|
|
else
|
2018-05-16 12:47:08 -07:00
|
|
|
true # Non element values were handled through the expression filter
|
2016-04-06 10:59:11 -07:00
|
|
|
end
|
|
|
|
end
|
2016-04-21 14:25:14 -07:00
|
|
|
|
2017-05-01 18:39:08 -07:00
|
|
|
describe do |**options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2016-04-21 14:25:14 -07:00
|
|
|
desc << " for #{options[:for]}" if options[:for]
|
2016-04-26 22:44:48 -07:00
|
|
|
desc
|
2016-04-21 14:25:14 -07:00
|
|
|
end
|
2016-04-06 10:59:11 -07:00
|
|
|
end
|
|
|
|
|
2011-08-27 17:54:55 -04:00
|
|
|
Capybara.add_selector(:table) do
|
2018-05-16 12:47:08 -07:00
|
|
|
xpath(:caption) do |locator, caption: nil, **_options|
|
2016-03-24 11:18:12 -07:00
|
|
|
xpath = XPath.descendant(:table)
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = xpath[(XPath.attr(:id) == locator.to_s) | XPath.descendant(:caption).is(locator.to_s)] unless locator.nil?
|
2018-05-16 12:47:08 -07:00
|
|
|
xpath = xpath[XPath.descendant(:caption) == caption] if caption
|
2016-03-24 11:18:12 -07:00
|
|
|
xpath
|
|
|
|
end
|
2016-08-18 13:27:35 -07:00
|
|
|
|
2017-11-13 13:04:47 -08:00
|
|
|
describe do |caption: nil, **_options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2017-05-01 18:39:08 -07:00
|
|
|
desc << " with caption #{caption}" if caption
|
2016-08-18 13:27:35 -07:00
|
|
|
desc
|
|
|
|
end
|
2011-08-27 17:54:55 -04:00
|
|
|
end
|
2016-07-18 13:17:22 -07:00
|
|
|
|
|
|
|
Capybara.add_selector(:frame) do
|
2017-05-01 18:39:08 -07:00
|
|
|
xpath(:name) do |locator, **options|
|
2017-05-30 15:51:15 -07:00
|
|
|
xpath = XPath.descendant(:iframe).union(XPath.descendant(:frame))
|
2018-01-02 17:46:41 -08:00
|
|
|
xpath = xpath[(XPath.attr(:id) == locator.to_s) | (XPath.attr(:name) == locator.to_s)] unless locator.nil?
|
2016-10-05 15:16:00 -07:00
|
|
|
xpath = expression_filters.keys.inject(xpath) { |memo, ef| memo[find_by_attr(ef, options[ef])] }
|
2016-07-18 13:17:22 -07:00
|
|
|
xpath
|
|
|
|
end
|
2016-08-18 13:27:35 -07:00
|
|
|
|
2017-11-13 13:04:47 -08:00
|
|
|
describe do |name: nil, **_options|
|
2018-05-10 13:20:23 -07:00
|
|
|
desc = +""
|
2017-05-01 18:39:08 -07:00
|
|
|
desc << " with name #{name}" if name
|
2016-08-18 13:27:35 -07:00
|
|
|
desc
|
|
|
|
end
|
2016-07-28 10:25:24 +02:00
|
|
|
end
|
2018-01-08 12:23:54 -08:00
|
|
|
|
2018-05-24 15:27:34 -07:00
|
|
|
Capybara.add_selector(:element) do
|
|
|
|
xpath do |locator, **_options|
|
|
|
|
XPath.descendant((locator || '@').to_sym)
|
|
|
|
end
|
|
|
|
|
2018-05-26 10:26:44 -07:00
|
|
|
expression_filter(:attributes, matcher: /.+/) do |xpath, name, val|
|
2018-05-24 15:27:34 -07:00
|
|
|
case val
|
|
|
|
when Regexp
|
|
|
|
xpath
|
|
|
|
when XPath::Expression
|
|
|
|
xpath[XPath.attr(name)[val]]
|
|
|
|
else
|
|
|
|
xpath[XPath.attr(name.to_sym) == val]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-29 14:02:03 -07:00
|
|
|
node_filter(:attributes, matcher: /.+/) do |node, name, val|
|
2018-05-24 15:27:34 -07:00
|
|
|
val.is_a?(Regexp) ? node[name] =~ val : true
|
|
|
|
end
|
|
|
|
|
|
|
|
describe do |**options|
|
|
|
|
desc = +""
|
|
|
|
desc << describe_all_expression_filters(options)
|
|
|
|
desc
|
|
|
|
end
|
|
|
|
end
|
2018-01-08 12:23:54 -08:00
|
|
|
# rubocop:enable Metrics/BlockLength
|