Implement enabled_options and disabled_options filters for select selector

Document new filters
This commit is contained in:
Thomas Walpole 2019-12-30 10:53:31 -08:00
parent c5cf02b1a6
commit 7eb7e1deec
3 changed files with 61 additions and 12 deletions

View File

@ -108,6 +108,8 @@ require 'capybara/selector/definition'
# * :disabled (Boolean, :all) - Match disabled field? (Default: false)
# * :multiple (Boolean) - Match fields that accept multiple values
# * :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
# * :selected (String, Array<String>) - Match the selection(s)
# * :with_selected (String, Array<String>) - Partial match the selection(s)

View File

@ -11,16 +11,26 @@ Capybara.add_selector(:select, locator_type: [String, Symbol]) do
filter_set(:_field, %i[disabled multiple name placeholder])
node_filter(:options) do |node, options|
actual = if node.visible?
node.all(:xpath, './/option', wait: false).map(&:text)
else
node.all(:xpath, './/option', visible: false, wait: false).map { |option| option.text(:all) }
end
actual = options_text(node)
(options.sort == actual.sort).tap do |res|
add_error("Expected options #{options.inspect} found #{actual.inspect}") unless res
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|
options.inject(expr) do |xpath, option|
xpath[expression_for(:option, option)]
@ -28,18 +38,14 @@ Capybara.add_selector(:select, locator_type: [String, Symbol]) do
end
node_filter(:selected) do |node, selected|
actual = node.all(:xpath, './/option', visible: false, wait: false)
.select(&:selected?)
.map { |option| option.text(:all) }
actual = options_text(node, visible: false, &:selected?)
(Array(selected).sort == actual.sort).tap do |res|
add_error("Expected #{selected.inspect} to be selected found #{actual.inspect}") unless res
end
end
node_filter(:with_selected) do |node, selected|
actual = node.all(:xpath, './/option', visible: false, wait: false)
.select(&:selected?)
.map { |option| option.text(:all) }
actual = options_text(node, visible: false, &:selected?)
(Array(selected) - actual).empty?.tap do |res|
add_error("Expected at least #{selected.inspect} to be selected found #{actual.inspect}") unless res
end
@ -51,12 +57,25 @@ Capybara.add_selector(:select, locator_type: [String, Symbol]) do
desc
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 << " 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 at least #{with_selected.inspect} selected" if with_selected
desc << ' which is disabled' if disabled
desc
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

View File

@ -117,6 +117,34 @@ Capybara::SpecHelper.spec '#has_select?' do
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
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])