Permalink
Browse files

has_select? supports matching a partial options set

  • Loading branch information...
1 parent e41014a commit 6089acc349277eb7d0bd4beb280cd22178d21d06 Gonzalo Rodriguez committed Mar 23, 2012
Showing with 35 additions and 3 deletions.
  1. +6 −1 lib/capybara/node/matchers.rb
  2. +1 −0 lib/capybara/selector.rb
  3. +28 −2 lib/capybara/spec/session/has_select_spec.rb
@@ -348,10 +348,15 @@ def has_no_unchecked_field?(locator)
# It's also possible to check if the exact set of options exists for
# this select box:
#
- # page.has_select?('Language', :options => ['English', 'German'])
+ # page.has_select?('Language', :options => ['English', 'German', 'Spanish'])
+ #
+ # You can also check for a partial set of options:
+ #
+ # page.has_select?('Language', :with_options => ['English', 'German'])
#
# @param [String] locator The label, name or id of a select box
# @option options [Array] :options Options which should be contained in this select box
+ # @option options [Array] :with_options Partial set of options which should be contained in this select box
# @option options [String, Array] :selected Options which should be selected
# @return [Boolean] Whether it exists
#
View
@@ -131,6 +131,7 @@ def filter(name, &block)
actual = node.all(:xpath, './/option').map { |option| option.text }
options.sort == actual.sort
end
+ filter(:with_options) { |node, options| options.all? { |option| node.first(:option, option) } }
filter(:selected) do |node, selected|
actual = node.all(:xpath, './/option').select { |option| option.selected? }.map { |option| option.text }
([selected].flatten - actual).empty?
@@ -48,7 +48,7 @@
end
end
- context 'with options' do
+ context 'with exact options' do
it "should be true if a field with the given options is on the page" do
@session.should have_select('Region', :options => ['Norway', 'Sweden', 'Finland'])
@session.should have_select('Tendency', :options => [])
@@ -62,6 +62,19 @@
@session.should_not have_select('Region', :options => ['Norway', 'Norway', 'Norway'])
end
end
+
+ context 'with partial options' do
+ it "should be true if a field with the given partial options is on the page" do
+ @session.should have_select('Region', :with_options => ['Norway', 'Sweden'])
+ @session.should have_select('City', :with_options => ['London'])
+ end
+
+ it "should be false if a field with the given partial options is not on the page" do
+ @session.should_not have_select('Locale', :with_options => ['Uruguayan'])
+ @session.should_not have_select('Does not exist', :with_options => ['John'])
+ @session.should_not have_select('Region', :with_options => ['Norway', 'Sweden', 'Finland', 'Latvia'])
+ end
+ end
end
describe '#has_no_select?' do
@@ -113,7 +126,7 @@
end
end
- context 'with options' do
+ context 'with exact options' do
it "should be false if a field with the given options is on the page" do
@session.should_not have_no_select('Region', :options => ['Norway', 'Sweden', 'Finland'])
end
@@ -126,5 +139,18 @@
@session.should have_no_select('Region', :options => ['Norway', 'Norway', 'Norway'])
end
end
+
+ context 'with partial options' do
+ it "should be false if a field with the given partial options is on the page" do
+ @session.should_not have_no_select('Region', :with_options => ['Norway', 'Sweden'])
+ @session.should_not have_no_select('City', :with_options => ['London'])
+ end
+
+ it "should be true if a field with the given partial options is not on the page" do
+ @session.should have_no_select('Locale', :with_options => ['Uruguayan'])
+ @session.should have_no_select('Does not exist', :with_options => ['John'])
+ @session.should have_no_select('Region', :with_options => ['Norway', 'Sweden', 'Finland', 'Latvia'])
+ end
+ end
end
end

0 comments on commit 6089acc

Please sign in to comment.