Implement enabled_options and disabled_options filters for select selector
Document new filters
This commit is contained in:
parent
c5cf02b1a6
commit
7eb7e1deec
|
@ -108,6 +108,8 @@ require 'capybara/selector/definition'
|
||||||
# * :disabled (Boolean, :all) - Match disabled field? (Default: false)
|
# * :disabled (Boolean, :all) - Match disabled field? (Default: false)
|
||||||
# * :multiple (Boolean) - Match fields that accept multiple values
|
# * :multiple (Boolean) - Match fields that accept multiple values
|
||||||
# * :options (Array<String>) - Exact match options
|
# * :options (Array<String>) - Exact match options
|
||||||
|
# * :enabled_options (Array<String>) - Exact match enabled options
|
||||||
|
# * :disabled_options (Array<String>) - Exact match disabled options
|
||||||
# * :with_options (Array<String>) - Partial match options
|
# * :with_options (Array<String>) - Partial match options
|
||||||
# * :selected (String, Array<String>) - Match the selection(s)
|
# * :selected (String, Array<String>) - Match the selection(s)
|
||||||
# * :with_selected (String, Array<String>) - Partial match the selection(s)
|
# * :with_selected (String, Array<String>) - Partial match the selection(s)
|
||||||
|
|
|
@ -11,16 +11,26 @@ Capybara.add_selector(:select, locator_type: [String, Symbol]) do
|
||||||
filter_set(:_field, %i[disabled multiple name placeholder])
|
filter_set(:_field, %i[disabled multiple name placeholder])
|
||||||
|
|
||||||
node_filter(:options) do |node, options|
|
node_filter(:options) do |node, options|
|
||||||
actual = if node.visible?
|
actual = options_text(node)
|
||||||
node.all(:xpath, './/option', wait: false).map(&:text)
|
|
||||||
else
|
|
||||||
node.all(:xpath, './/option', visible: false, wait: false).map { |option| option.text(:all) }
|
|
||||||
end
|
|
||||||
(options.sort == actual.sort).tap do |res|
|
(options.sort == actual.sort).tap do |res|
|
||||||
add_error("Expected options #{options.inspect} found #{actual.inspect}") unless res
|
add_error("Expected options #{options.inspect} found #{actual.inspect}") unless res
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
node_filter(:enabled_options) do |node, options|
|
||||||
|
actual = options_text(node) { |o| !o.disabled? }
|
||||||
|
(options.sort == actual.sort).tap do |res|
|
||||||
|
add_error("Expected enabled options #{options.inspect} found #{actual.inspect}") unless res
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
node_filter(:disabled_options) do |node, options|
|
||||||
|
actual = options_text(node, &:disabled?)
|
||||||
|
(options.sort == actual.sort).tap do |res|
|
||||||
|
add_error("Expected disabled options #{options.inspect} found #{actual.inspect}") unless res
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
expression_filter(:with_options) do |expr, options|
|
expression_filter(:with_options) do |expr, options|
|
||||||
options.inject(expr) do |xpath, option|
|
options.inject(expr) do |xpath, option|
|
||||||
xpath[expression_for(:option, option)]
|
xpath[expression_for(:option, option)]
|
||||||
|
@ -28,18 +38,14 @@ Capybara.add_selector(:select, locator_type: [String, Symbol]) do
|
||||||
end
|
end
|
||||||
|
|
||||||
node_filter(:selected) do |node, selected|
|
node_filter(:selected) do |node, selected|
|
||||||
actual = node.all(:xpath, './/option', visible: false, wait: false)
|
actual = options_text(node, visible: false, &:selected?)
|
||||||
.select(&:selected?)
|
|
||||||
.map { |option| option.text(:all) }
|
|
||||||
(Array(selected).sort == actual.sort).tap do |res|
|
(Array(selected).sort == actual.sort).tap do |res|
|
||||||
add_error("Expected #{selected.inspect} to be selected found #{actual.inspect}") unless res
|
add_error("Expected #{selected.inspect} to be selected found #{actual.inspect}") unless res
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
node_filter(:with_selected) do |node, selected|
|
node_filter(:with_selected) do |node, selected|
|
||||||
actual = node.all(:xpath, './/option', visible: false, wait: false)
|
actual = options_text(node, visible: false, &:selected?)
|
||||||
.select(&:selected?)
|
|
||||||
.map { |option| option.text(:all) }
|
|
||||||
(Array(selected) - actual).empty?.tap do |res|
|
(Array(selected) - actual).empty?.tap do |res|
|
||||||
add_error("Expected at least #{selected.inspect} to be selected found #{actual.inspect}") unless res
|
add_error("Expected at least #{selected.inspect} to be selected found #{actual.inspect}") unless res
|
||||||
end
|
end
|
||||||
|
@ -51,12 +57,25 @@ Capybara.add_selector(:select, locator_type: [String, Symbol]) do
|
||||||
desc
|
desc
|
||||||
end
|
end
|
||||||
|
|
||||||
describe_node_filters do |options: nil, selected: nil, with_selected: nil, disabled: nil, **|
|
describe_node_filters do |
|
||||||
|
options: nil, disabled_options: nil, enabled_options: nil,
|
||||||
|
selected: nil, with_selected: nil,
|
||||||
|
disabled: nil, **|
|
||||||
desc = +''
|
desc = +''
|
||||||
desc << " with options #{options.inspect}" if options
|
desc << " with options #{options.inspect}" if options
|
||||||
|
desc << " with disabled options #{disabled_options.inspect}}" if disabled_options
|
||||||
|
desc << " with enabled options #{enabled_options.inspect}" if enabled_options
|
||||||
desc << " with #{selected.inspect} selected" if selected
|
desc << " with #{selected.inspect} selected" if selected
|
||||||
desc << " with at least #{with_selected.inspect} selected" if with_selected
|
desc << " with at least #{with_selected.inspect} selected" if with_selected
|
||||||
desc << ' which is disabled' if disabled
|
desc << ' which is disabled' if disabled
|
||||||
desc
|
desc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def options_text(node, **opts, &filter_block)
|
||||||
|
opts[:wait] = false
|
||||||
|
opts[:visible] = false unless node.visible?
|
||||||
|
node.all(:xpath, './/option', **opts, &filter_block).map do |o|
|
||||||
|
o.text((:all if opts[:visible] == false))
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -117,6 +117,34 @@ Capybara::SpecHelper.spec '#has_select?' do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'with enabled options' do
|
||||||
|
it 'should be true if the listed options exist and are enabled' do
|
||||||
|
expect(@session).to have_select('form_title', enabled_options: %w[Mr Mrs Miss])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should be false if the listed options do not exist' do
|
||||||
|
expect(@session).not_to have_select('form_title', enabled_options: ['Not there'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should be false if the listed option exists but is not enabled' do
|
||||||
|
expect(@session).not_to have_select('form_title', enabled_options: %w[Mr Mrs Miss Other])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with disabled options' do
|
||||||
|
it 'should be true if the listed options exist and are disabled' do
|
||||||
|
expect(@session).to have_select('form_title', disabled_options: ['Other'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should be false if the listed options do not exist' do
|
||||||
|
expect(@session).not_to have_select('form_title', disabled_options: ['Not there'])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should be false if the listed option exists but is not disabled' do
|
||||||
|
expect(@session).not_to have_select('form_title', disabled_options: %w[Other Mrs])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'with partial options' do
|
context 'with partial options' do
|
||||||
it 'should be true if a field with the given partial options is on the page' do
|
it 'should be true if a field with the given partial options is on the page' do
|
||||||
expect(@session).to have_select('Region', with_options: %w[Norway Sweden])
|
expect(@session).to have_select('Region', with_options: %w[Norway Sweden])
|
||||||
|
|
Loading…
Reference in New Issue