From c67dceb5379712e4b3590c68589992169b462fb4 Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Sun, 3 Oct 2010 19:15:40 +0200 Subject: [PATCH] select/unselect do not require a from option This way they can more conveniently be called on a found element. E.g.: find('#my_select').select('Some option') --- lib/capybara/node/actions.rb | 26 +++++++++++++++------- lib/capybara/spec/session/select_spec.rb | 8 ++++++- lib/capybara/spec/session/unselect_spec.rb | 7 ++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/capybara/node/actions.rb b/lib/capybara/node/actions.rb index f89b43be..8fc52e34 100644 --- a/lib/capybara/node/actions.rb +++ b/lib/capybara/node/actions.rb @@ -106,10 +106,15 @@ module Capybara # @param [String] locator Which check box to uncheck # def select(value, options={}) - no_select_msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found" - no_option_msg = "cannot select option, no option with text '#{value}' in select box '#{options[:from]}'" - select = find(:xpath, XPath::HTML.select(options[:from]), :message => no_select_msg) - select.find(:xpath, XPath::HTML.option(value), :message => no_option_msg).select_option + if options.has_key?(:from) + no_select_msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found" + no_option_msg = "cannot select option, no option with text '#{value}' in select box '#{options[:from]}'" + select = find(:xpath, XPath::HTML.select(options[:from]), :message => no_select_msg) + select.find(:xpath, XPath::HTML.option(value), :message => no_option_msg).select_option + else + no_option_msg = "cannot select option, no option with text '#{value}'" + find(:xpath, XPath::HTML.option(value), :message => no_option_msg).select_option + end end ## @@ -123,10 +128,15 @@ module Capybara # @param [String] locator Which check box to uncheck # def unselect(value, options={}) - no_select_msg = "cannot unselect option, no select box with id, name, or label '#{options[:from]}' found" - no_option_msg = "cannot unselect option, no option with text '#{value}' in select box '#{options[:from]}'" - select = find(:xpath, XPath::HTML.select(options[:from]), :message => no_select_msg) - select.find(:xpath, XPath::HTML.option(value), :message => no_option_msg).unselect_option + if options.has_key?(:from) + no_select_msg = "cannot unselect option, no select box with id, name, or label '#{options[:from]}' found" + no_option_msg = "cannot unselect option, no option with text '#{value}' in select box '#{options[:from]}'" + select = find(:xpath, XPath::HTML.select(options[:from]), :message => no_select_msg) + select.find(:xpath, XPath::HTML.option(value), :message => no_option_msg).unselect_option + else + no_option_msg = "cannot unselect option, no option with text '#{value}'" + find(:xpath, XPath::HTML.option(value), :message => no_option_msg).unselect_option + end end ## diff --git a/lib/capybara/spec/session/select_spec.rb b/lib/capybara/spec/session/select_spec.rb index 8b0dad38..63f4d379 100644 --- a/lib/capybara/spec/session/select_spec.rb +++ b/lib/capybara/spec/session/select_spec.rb @@ -3,7 +3,7 @@ shared_examples_for "select" do before do @session.visit('/form') end - + it "should return value of the first option" do @session.find_field('Title').value.should == 'Mrs' end @@ -25,6 +25,12 @@ shared_examples_for "select" do extract_results(@session)['locale'].should == 'fi' end + it "should select an option without giving a select box" do + @session.select("Mr") + @session.click_button('awesome') + extract_results(@session)['title'].should == 'Mr' + end + it "should favour exact matches to option labels" do @session.select("Mr", :from => 'Title') @session.click_button('awesome') diff --git a/lib/capybara/spec/session/unselect_spec.rb b/lib/capybara/spec/session/unselect_spec.rb index 632970c8..ec33e227 100644 --- a/lib/capybara/spec/session/unselect_spec.rb +++ b/lib/capybara/spec/session/unselect_spec.rb @@ -12,6 +12,13 @@ shared_examples_for "unselect" do extract_results(@session)['underwear'].should_not include('Commando') end + it "should unselect an option without a select box" do + @session.unselect('Commando') + @session.click_button('awesome') + extract_results(@session)['underwear'].should include('Briefs', 'Boxer Briefs') + extract_results(@session)['underwear'].should_not include('Commando') + end + it "should unselect an option from a select box by label" do @session.unselect('Commando', :from => 'Underwear') @session.click_button('awesome')