From 7eb7e1deecddc3e735bf9399c027b9639ee0fc7b Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 30 Dec 2019 10:53:31 -0800 Subject: [PATCH] Implement enabled_options and disabled_options filters for select selector Document new filters --- lib/capybara/selector.rb | 2 + lib/capybara/selector/definition/select.rb | 43 ++++++++++++++------ lib/capybara/spec/session/has_select_spec.rb | 28 +++++++++++++ 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/lib/capybara/selector.rb b/lib/capybara/selector.rb index ae58034d..3c16f104 100644 --- a/lib/capybara/selector.rb +++ b/lib/capybara/selector.rb @@ -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) - Exact match options +# * :enabled_options (Array) - Exact match enabled options +# * :disabled_options (Array) - Exact match disabled options # * :with_options (Array) - Partial match options # * :selected (String, Array) - Match the selection(s) # * :with_selected (String, Array) - Partial match the selection(s) diff --git a/lib/capybara/selector/definition/select.rb b/lib/capybara/selector/definition/select.rb index 09d3df4d..92582f55 100644 --- a/lib/capybara/selector/definition/select.rb +++ b/lib/capybara/selector/definition/select.rb @@ -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 diff --git a/lib/capybara/spec/session/has_select_spec.rb b/lib/capybara/spec/session/has_select_spec.rb index c3527459..8e47c1d1 100644 --- a/lib/capybara/spec/session/has_select_spec.rb +++ b/lib/capybara/spec/session/has_select_spec.rb @@ -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])