2016-03-08 00:52:19 +00:00
|
|
|
# frozen_string_literal: true
|
2016-08-18 20:27:35 +00:00
|
|
|
require 'capybara/selector/selector'
|
2011-08-27 02:16:15 +00:00
|
|
|
|
2016-04-27 05:44:48 +00:00
|
|
|
Capybara::Selector::FilterSet.add(:_field) do
|
|
|
|
filter(:checked, boolean: true) { |node, value| not(value ^ node.checked?) }
|
|
|
|
filter(:unchecked, boolean: true) { |node, value| (value ^ node.checked?) }
|
|
|
|
filter(:disabled, default: false, boolean: true, skip_if: :all) { |node, value| not(value ^ node.disabled?) }
|
2016-06-11 19:09:23 +00:00
|
|
|
filter(:multiple, boolean: true) { |node, value| !(value ^ node.multiple?) }
|
2016-04-27 05:44:48 +00:00
|
|
|
|
|
|
|
describe do |options|
|
|
|
|
desc, states = String.new, []
|
|
|
|
states << 'checked' if options[:checked] || (options[:unchecked] === false)
|
|
|
|
states << 'not checked' if options[:unchecked] || (options[:checked] === false)
|
|
|
|
states << 'disabled' if options[:disabled] == true
|
|
|
|
desc << " that is #{states.join(' and ')}" unless states.empty?
|
|
|
|
desc << " with the multiple attribute" if options[:multiple] == true
|
|
|
|
desc << " without the multiple attribute" if options[:multiple] === false
|
|
|
|
desc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-18 20:27:35 +00: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
|
|
|
|
|
2011-08-27 02:16:15 +00:00
|
|
|
Capybara.add_selector(:field) do
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :name, :placeholder, :type) do |locator, options|
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath = XPath.descendant(:input, :textarea, :select)[~XPath.attr(:type).one_of('submit', 'image', 'hidden')]
|
2016-08-18 20:27:35 +00:00
|
|
|
if options[:type]
|
|
|
|
type=options[:type].to_s
|
|
|
|
if ['textarea', 'select'].include?(type)
|
|
|
|
xpath = XPath.descendant(type.to_sym)
|
|
|
|
else
|
|
|
|
xpath = xpath[XPath.attr(:type).equals(type)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
2016-04-26 15:51:22 +00:00
|
|
|
|
2016-08-18 20:27:35 +00:00
|
|
|
filter_set(:_field) # checked/unchecked/disabled/multiple
|
2016-04-26 15:51:22 +00:00
|
|
|
|
2016-06-11 19:09:23 +00:00
|
|
|
filter(:readonly, boolean: true) { |node, value| not(value ^ node.readonly?) }
|
2016-07-28 08:25:24 +00:00
|
|
|
filter(:with) do |node, with|
|
|
|
|
with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s
|
|
|
|
end
|
2014-07-02 18:17:53 +00:00
|
|
|
describe do |options|
|
2016-08-18 20:27:35 +00:00
|
|
|
desc = String.new
|
|
|
|
(expression_filters - [:type]).each { |ef| desc << " with #{ef.to_s} #{options[ef]}" if options.has_key?(ef) }
|
2014-07-02 18:17:53 +00:00
|
|
|
desc << " of type #{options[:type].inspect}" if options[:type]
|
|
|
|
desc << " with value #{options[:with].to_s.inspect}" if options.has_key?(:with)
|
|
|
|
desc
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
2011-08-27 21:57:12 +00:00
|
|
|
Capybara.add_selector(:fieldset) do
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :legend) do |locator, options|
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath = XPath.descendant(:fieldset)
|
|
|
|
xpath = xpath[XPath.attr(:id).equals(locator.to_s) | XPath.child(:legend)[XPath.string.n.is(locator.to_s)]] unless locator.nil?
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath = xpath[XPath.attr(:id).equals(options[:id])] if options[:id]
|
|
|
|
xpath = xpath[XPath.child(:legend)[XPath.string.n.is(options[:legend])]] if options[:legend]
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:link) do
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :title, :alt) do |locator, options={}|
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath = XPath.descendant(:a)[XPath.attr(:href)]
|
|
|
|
unless locator.nil?
|
|
|
|
locator = locator.to_s
|
2016-08-03 20:16:30 +00:00
|
|
|
matchers = XPath.attr(:id).equals(locator) |
|
|
|
|
XPath.string.n.is(locator) |
|
|
|
|
XPath.attr(:title).is(locator) |
|
|
|
|
XPath.descendant(:img)[XPath.attr(:alt).is(locator)]
|
|
|
|
matchers |= XPath.attr(:'aria-label').is(locator) if Capybara.enable_aria_label
|
|
|
|
xpath = xpath[matchers]
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath = xpath[XPath.attr(:id).equals(options[:id])] if options[:id]
|
|
|
|
xpath = xpath[XPath.attr(:title).equals(options[:title])] if options[:title]
|
|
|
|
xpath = xpath[XPath.descendant(:img)[XPath.attr(:alt).equals(options[:alt])]] if options[:alt]
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath
|
|
|
|
end
|
|
|
|
|
2015-08-25 21:25:42 +00:00
|
|
|
filter(:href) do |node, href|
|
2015-04-28 00:33:39 +00:00
|
|
|
if href.is_a? Regexp
|
|
|
|
node[:href].match href
|
|
|
|
else
|
|
|
|
node.first(:xpath, XPath.axis(:self)[XPath.attr(:href).equals(href.to_s)], minimum: 0)
|
|
|
|
end
|
2012-01-03 08:43:11 +00:00
|
|
|
end
|
2016-03-24 18:18:12 +00:00
|
|
|
|
2014-07-02 18:17:53 +00:00
|
|
|
describe { |options| " with href #{options[:href].inspect}" if options[:href] }
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:button) do
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :value, :title) do |locator, options={}|
|
2016-03-24 18:18:12 +00:00
|
|
|
input_btn_xpath = XPath.descendant(:input)[XPath.attr(:type).one_of('submit', 'reset', 'image', 'button')]
|
|
|
|
btn_xpath = XPath.descendant(:button)
|
|
|
|
image_btn_xpath = XPath.descendant(:input)[XPath.attr(:type).equals('image')]
|
|
|
|
|
|
|
|
unless locator.nil?
|
|
|
|
locator = locator.to_s
|
2016-08-03 20:16:30 +00:00
|
|
|
locator_matches = XPath.attr(:id).equals(locator) | XPath.attr(:value).is(locator) | XPath.attr(:title).is(locator)
|
|
|
|
locator_matches |= XPath.attr(:'aria-label').is(locator) if Capybara.enable_aria_label
|
|
|
|
|
|
|
|
input_btn_xpath = input_btn_xpath[locator_matches]
|
|
|
|
|
|
|
|
btn_xpath = btn_xpath[locator_matches | XPath.string.n.is(locator)]
|
|
|
|
|
|
|
|
alt_matches = XPath.attr(:alt).is(locator)
|
|
|
|
alt_matches |= XPath.attr(:'aria-label').is(locator) if Capybara.enable_aria_label
|
|
|
|
image_btn_xpath = image_btn_xpath[alt_matches]
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
|
|
|
|
2016-08-18 20:27:35 +00:00
|
|
|
res_xpath = input_btn_xpath + btn_xpath + image_btn_xpath
|
|
|
|
|
|
|
|
(expression_filters & options.keys).inject(res_xpath) { |xpath, ef| xpath[XPath.attr(ef).equals(options[ef])] }
|
|
|
|
|
|
|
|
res_xpath
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
|
|
|
|
2015-09-19 18:42:45 +00:00
|
|
|
filter(:disabled, default: false, boolean: true, skip_if: :all) { |node, value| not(value ^ node.disabled?) }
|
2016-03-24 18:18:12 +00:00
|
|
|
|
2016-08-18 20:27:35 +00:00
|
|
|
describe do |options|
|
|
|
|
desc = String.new
|
|
|
|
desc << " that is disabled" if options[:disabled] == true
|
|
|
|
expression_filters.each { |ef| desc << " with #{ef.to_s} #{options[ef]}" if options.has_key?(ef) }
|
|
|
|
desc
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
2016-03-24 18:18:12 +00:00
|
|
|
Capybara.add_selector(:link_or_button) do
|
|
|
|
label "link or button"
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath do |locator, options|
|
|
|
|
self.class.all.values_at(:link, :button).map {|selector| selector.xpath.call(locator, options)}.reduce(:+)
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
|
|
|
|
2016-04-27 05:44:48 +00:00
|
|
|
filter(:disabled, default: false, boolean: true, skip_if: :all) { |node, value| node.tag_name == "a" or not(value ^ node.disabled?) }
|
2016-03-24 18:18:12 +00:00
|
|
|
|
|
|
|
describe { |options| " that is disabled" if options[:disabled] }
|
|
|
|
end
|
|
|
|
|
2011-08-27 02:16:15 +00:00
|
|
|
Capybara.add_selector(:fillable_field) do
|
2012-06-08 15:07:12 +00:00
|
|
|
label "field"
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :name, :placeholder) do |locator, options|
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath = XPath.descendant(:input, :textarea)[~XPath.attr(:type).one_of('submit', 'image', 'radio', 'checkbox', 'hidden', 'file')]
|
2016-08-18 20:27:35 +00:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
|
|
|
|
2016-08-18 20:27:35 +00:00
|
|
|
filter_set(:_field, [:disabled, :multiple])
|
|
|
|
|
|
|
|
describe do |options|
|
|
|
|
desc = String.new
|
|
|
|
expression_filters.each { |ef| desc << " with #{ef.to_s} #{options[ef]}" if options.has_key?(ef) }
|
|
|
|
desc
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:radio_button) do
|
2012-06-08 15:07:12 +00:00
|
|
|
label "radio button"
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :name) do |locator, options|
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath = XPath.descendant(:input)[XPath.attr(:type).equals('radio')]
|
2016-08-18 20:27:35 +00:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
|
|
|
|
2016-08-18 20:27:35 +00:00
|
|
|
filter_set(:_field, [:checked, :unchecked, :disabled])
|
2016-04-26 15:51:22 +00:00
|
|
|
|
2013-10-20 17:59:07 +00:00
|
|
|
filter(:option) { |node, value| node.value == value.to_s }
|
2016-03-24 18:18:12 +00:00
|
|
|
|
2014-07-02 18:17:53 +00:00
|
|
|
describe do |options|
|
2016-04-27 05:44:48 +00:00
|
|
|
desc = String.new
|
2014-07-02 18:17:53 +00:00
|
|
|
desc << " with value #{options[:option].inspect}" if options[:option]
|
2016-08-18 20:27:35 +00:00
|
|
|
expression_filters.each { |ef| desc << " with #{ef.to_s} #{options[ef]}" if options.has_key?(ef) }
|
2014-07-02 18:17:53 +00:00
|
|
|
desc
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:checkbox) do
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :name) do |locator, options|
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath = XPath.descendant(:input)[XPath.attr(:type).equals('checkbox')]
|
2016-08-18 20:27:35 +00:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
|
|
|
|
2016-08-18 20:27:35 +00:00
|
|
|
filter_set(:_field, [:checked, :unchecked, :disabled])
|
2016-04-26 15:51:22 +00:00
|
|
|
|
2013-10-20 17:59:07 +00:00
|
|
|
filter(:option) { |node, value| node.value == value.to_s }
|
2016-03-24 18:18:12 +00:00
|
|
|
|
2014-07-02 18:17:53 +00:00
|
|
|
describe do |options|
|
2016-04-27 05:44:48 +00:00
|
|
|
desc = String.new
|
2014-07-02 18:17:53 +00:00
|
|
|
desc << " with value #{options[:option].inspect}" if options[:option]
|
2016-08-18 20:27:35 +00:00
|
|
|
expression_filters.each { |ef| desc << " with #{ef.to_s} #{options[ef]}" if options.has_key?(ef) }
|
2014-07-02 18:17:53 +00:00
|
|
|
desc
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:select) do
|
2012-06-08 15:07:12 +00:00
|
|
|
label "select box"
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :name, :placeholder) do |locator, options|
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath = XPath.descendant(:select)
|
2016-08-18 20:27:35 +00:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
|
|
|
|
2016-08-18 20:27:35 +00:00
|
|
|
filter_set(:_field, [:disabled, :multiple])
|
2016-04-26 15:51:22 +00:00
|
|
|
|
2012-03-16 03:14:47 +00:00
|
|
|
filter(:options) do |node, options|
|
2015-10-06 20:42:45 +00:00
|
|
|
if node.visible?
|
|
|
|
actual = node.all(:xpath, './/option').map { |option| option.text }
|
|
|
|
else
|
|
|
|
actual = node.all(:xpath, './/option', visible: false).map { |option| option.text(:all) }
|
|
|
|
end
|
2012-03-16 03:14:47 +00:00
|
|
|
options.sort == actual.sort
|
|
|
|
end
|
2015-10-06 20:42:45 +00:00
|
|
|
filter(:with_options) do |node, options|
|
|
|
|
finder_settings = { minimum: 0 }
|
|
|
|
if !node.visible?
|
|
|
|
finder_settings[:visible] = false
|
|
|
|
end
|
|
|
|
options.all? { |option| node.first(:option, option, finder_settings) }
|
|
|
|
end
|
2012-01-02 16:12:21 +00:00
|
|
|
filter(:selected) do |node, selected|
|
2015-10-06 20:42:45 +00:00
|
|
|
actual = node.all(:xpath, './/option', visible: false).select { |option| option.selected? }.map { |option| option.text(:all) }
|
2012-03-25 04:29:24 +00:00
|
|
|
[selected].flatten.sort == actual.sort
|
2012-01-02 16:12:21 +00:00
|
|
|
end
|
2016-03-24 18:18:12 +00:00
|
|
|
|
2014-07-02 18:17:53 +00:00
|
|
|
describe do |options|
|
2016-03-01 21:53:19 +00:00
|
|
|
desc = String.new
|
2014-07-02 18:17:53 +00:00
|
|
|
desc << " with options #{options[:options].inspect}" if options[:options]
|
|
|
|
desc << " with at least options #{options[:with_options].inspect}" if options[:with_options]
|
|
|
|
desc << " with #{options[:selected].inspect} selected" if options[:selected]
|
2016-08-18 20:27:35 +00:00
|
|
|
expression_filters.each { |ef| desc << " with #{ef.to_s} #{options[ef]}" if options.has_key?(ef) }
|
2014-07-02 18:17:53 +00:00
|
|
|
desc
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:option) do
|
2016-03-24 18:18:12 +00: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 21:50:42 +00:00
|
|
|
|
|
|
|
filter(:disabled, boolean: true) { |node, value| not(value ^ node.disabled?) }
|
|
|
|
filter(:selected, boolean: true) { |node, value| not(value ^ node.selected?) }
|
|
|
|
|
|
|
|
describe do |options|
|
|
|
|
desc = String.new
|
|
|
|
desc << " that is#{' not' unless options[:disabled]} disabled" if options.has_key?(:disabled)
|
|
|
|
desc << " that is#{' not' unless options[:selected]} selected" if options.has_key?(:selected)
|
|
|
|
desc
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.add_selector(:file_field) do
|
2012-06-08 15:07:12 +00:00
|
|
|
label "file field"
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :name) do |locator, options|
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath = XPath.descendant(:input)[XPath.attr(:type).equals('file')]
|
2016-08-18 20:27:35 +00:00
|
|
|
locate_field(xpath, locator, options)
|
2016-03-24 18:18:12 +00:00
|
|
|
end
|
|
|
|
|
2016-08-18 20:27:35 +00:00
|
|
|
filter_set(:_field, [:disabled, :multiple])
|
|
|
|
|
|
|
|
describe do |options|
|
|
|
|
desc = String.new
|
|
|
|
expression_filters.each { |ef| desc << " with #{ef.to_s} #{options[ef]}" if options.has_key?(ef) }
|
|
|
|
desc
|
|
|
|
end
|
2011-08-27 02:16:15 +00:00
|
|
|
end
|
2011-08-27 21:54:55 +00:00
|
|
|
|
2016-04-06 17:59:11 +00:00
|
|
|
Capybara.add_selector(:label) do
|
|
|
|
label "label"
|
|
|
|
xpath do |locator|
|
|
|
|
xpath = XPath.descendant(:label)
|
|
|
|
xpath = xpath[XPath.string.n.is(locator.to_s) | XPath.attr(:id).equals(locator.to_s)] unless locator.nil?
|
|
|
|
xpath
|
|
|
|
end
|
|
|
|
|
|
|
|
filter(:for) do |node, field_or_value|
|
|
|
|
if field_or_value.is_a? Capybara::Node::Element
|
|
|
|
if field_or_value[:id] && (field_or_value[:id] == node[:for])
|
|
|
|
true
|
|
|
|
else
|
2016-04-06 21:20:23 +00:00
|
|
|
field_or_value.find_xpath('./ancestor::label[1]').include? node.base
|
2016-04-06 17:59:11 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
node[:for] == field_or_value.to_s
|
|
|
|
end
|
|
|
|
end
|
2016-04-21 21:25:14 +00:00
|
|
|
|
|
|
|
describe do |options|
|
|
|
|
desc = String.new
|
|
|
|
desc << " for #{options[:for]}" if options[:for]
|
2016-04-27 05:44:48 +00:00
|
|
|
desc
|
2016-04-21 21:25:14 +00:00
|
|
|
end
|
2016-04-06 17:59:11 +00:00
|
|
|
end
|
|
|
|
|
2011-08-27 21:54:55 +00:00
|
|
|
Capybara.add_selector(:table) do
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :caption) do |locator, options|
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath = XPath.descendant(:table)
|
|
|
|
xpath = xpath[XPath.attr(:id).equals(locator.to_s) | XPath.descendant(:caption).is(locator.to_s)] unless locator.nil?
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath = xpath[XPath.attr(:id).equals(options[:id])] if options[:id]
|
|
|
|
xpath = xpath[XPath.descendant(:caption).equals(options[:caption])] if options[:caption]
|
2016-03-24 18:18:12 +00:00
|
|
|
xpath
|
|
|
|
end
|
2016-08-18 20:27:35 +00:00
|
|
|
|
|
|
|
describe do |options|
|
|
|
|
desc = String.new
|
|
|
|
desc << " with id #{options[:id]}" if options[:id]
|
|
|
|
desc << " with caption #{options[:caption]}" if options[:caption]
|
|
|
|
desc
|
|
|
|
end
|
2011-08-27 21:54:55 +00:00
|
|
|
end
|
2016-07-18 20:17:22 +00:00
|
|
|
|
|
|
|
Capybara.add_selector(:frame) do
|
2016-08-18 20:27:35 +00:00
|
|
|
xpath(:id, :name) do |locator, options|
|
2016-07-24 00:17:46 +00:00
|
|
|
xpath = XPath.descendant(:iframe) + XPath.descendant(:frame)
|
2016-07-18 20:17:22 +00:00
|
|
|
xpath = xpath[XPath.attr(:id).equals(locator.to_s) | XPath.attr(:name).equals(locator)] unless locator.nil?
|
2016-08-18 20:27:35 +00:00
|
|
|
[:id, :name].each { |ef| xpath = xpath[XPath.attr(ef).equals(options[ef])] if options[ef] }
|
2016-07-18 20:17:22 +00:00
|
|
|
xpath
|
|
|
|
end
|
2016-08-18 20:27:35 +00:00
|
|
|
|
|
|
|
describe do |options|
|
|
|
|
desc = String.new
|
|
|
|
desc << " with id #{options[:id]}" if options[:id]
|
|
|
|
desc << " with name #{options[:name]}" if options[:name]
|
|
|
|
desc
|
|
|
|
end
|
2016-07-28 08:25:24 +00:00
|
|
|
end
|