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')
This commit is contained in:
Jonas Nicklas 2010-10-03 19:15:40 +02:00
parent 1afc4c6c61
commit c67dceb537
3 changed files with 32 additions and 9 deletions

View File

@ -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
##

View File

@ -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')

View File

@ -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')