Restore behavior where we don't require select names (for now)

This commit is contained in:
Bryan Helmkamp 2008-04-07 22:26:24 -04:00
parent a99d785ea0
commit 7ab6dd720f
3 changed files with 41 additions and 2 deletions

View File

@ -16,6 +16,17 @@ module Webrat
find_field_by_label(possible_fields, id_or_name_or_label) || find_field_by_label(possible_fields, id_or_name_or_label) ||
nil nil
end end
def find_select_option(option_text)
select_fields = fields_by_type([SelectField])
select_fields.each do |select_field|
result = select_field.find_option(option_text)
return result if result
end
nil
end
def find_button(value = nil) def find_button(value = nil)
return fields_by_type([ButtonField]).first if value.nil? return fields_by_type([ButtonField]).first if value.nil?

View File

@ -76,8 +76,14 @@ module Webrat
# selects "February", :from => "Event Month" # selects "February", :from => "Event Month"
def selects(option_text, options = {}) def selects(option_text, options = {})
id_or_name_or_label = options[:from] id_or_name_or_label = options[:from]
field = find_field(id_or_name_or_label, SelectField)
option = field.find_option(option_text) if id_or_name_or_label
field = find_field(id_or_name_or_label, SelectField)
option = field.find_option(option_text)
else
option = find_select_option(option_text)
end
flunk("Could not find option #{option_text.inspect}") if option.nil? flunk("Could not find option #{option_text.inspect}") if option.nil?
option.choose option.choose
end end
@ -190,6 +196,16 @@ module Webrat
protected protected
def find_select_option(option_text)
forms.each do |form|
result = form.find_select_option(option_text)
return result if result
end
nil
end
def find_link(text) def find_link(text)
matching_links = [] matching_links = []

View File

@ -57,6 +57,18 @@ class SelectsTest < Test::Unit::TestCase
@session.clicks_button @session.clicks_button
end end
def test_should_work_without_specifying_the_field_name_or_label
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<select name="month"><option value="1">January</option></select>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "month" => "1")
@session.selects "January"
@session.clicks_button
end
def test_should_send_value_from_option_in_list_specified_by_name def test_should_send_value_from_option_in_list_specified_by_name
@response.stubs(:body).returns(<<-EOS) @response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login"> <form method="post" action="/login">