1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

has_select? :options filter matches only exact options

This commit is contained in:
Gonzalo Rodriguez 2012-03-16 00:14:47 -03:00
parent 3a3d6e00ea
commit bcdd23d42d
3 changed files with 13 additions and 8 deletions

View file

@ -345,7 +345,7 @@ module Capybara
#
# page.has_select?('Language', :selected => ['English', 'German'])
#
# It's also possible to check if a given set of options exists for
# It's also possible to check if the exact set of options exists for
# this select box:
#
# page.has_select?('Language', :options => ['English', 'German'])

View file

@ -127,7 +127,10 @@ end
Capybara.add_selector(:select) do
xpath { |locator| XPath::HTML.select(locator) }
failure_message { |node, selector| "no select box with id, name, or label '#{selector.locator}' found" }
filter(:options) { |node, options| options.all? { |option| node.first(:option, option) } }
filter(:options) do |node, options|
actual = node.all(:xpath, './/option').map { |option| option.text }
options.sort == actual.sort
end
filter(:selected) do |node, selected|
actual = node.all(:xpath, './/option').select { |option| option.selected? }.map { |option| option.text }
([selected].flatten - actual).empty?

View file

@ -50,14 +50,15 @@ shared_examples_for "has_select" do
context 'with options' do
it "should be true if a field with the given options is on the page" do
@session.should have_select('form_locale', :options => ['English'])
@session.should have_select('Region', :options => ['Norway', 'Sweden'])
@session.should have_select('Region', :options => ['Norway', 'Sweden', 'Finland'])
end
it "should be false if the given field is not on the page" do
@session.should_not have_select('Locale', :options => ['Not there'])
@session.should_not have_select('Locale', :options => ['Swedish'])
@session.should_not have_select('Does not exist', :options => ['John'])
@session.should_not have_select('City', :options => ['London', 'Made up city'])
@session.should_not have_select('Region', :options => ['Norway', 'Sweden'])
@session.should_not have_select('Region', :options => ['Norway', 'Norway', 'Norway'])
end
end
end
@ -113,14 +114,15 @@ shared_examples_for "has_select" do
context 'with options' do
it "should be false if a field with the given options is on the page" do
@session.should_not have_no_select('form_locale', :options => ['English'])
@session.should_not have_no_select('Region', :options => ['Norway', 'Sweden'])
@session.should_not have_no_select('Region', :options => ['Norway', 'Sweden', 'Finland'])
end
it "should be true if the given field is not on the page" do
@session.should have_no_select('Locale', :options => ['Not there'])
@session.should have_no_select('Locale', :options => ['Swedish'])
@session.should have_no_select('Does not exist', :options => ['John'])
@session.should have_no_select('City', :options => ['London', 'Made up city'])
@session.should have_no_select('Region', :options => ['Norway', 'Sweden'])
@session.should have_no_select('Region', :options => ['Norway', 'Norway', 'Norway'])
end
end
end