diff --git a/History.txt b/History.txt index 378f661..303cb50 100644 --- a/History.txt +++ b/History.txt @@ -10,7 +10,8 @@ * Minor enhancements - * Add support for clicking areas of an image map (Alex Lang) + * Support selecting options by their values (Alex Lang) + * Support for clicking areas of an image map (Alex Lang) * Add should_see and should_not_see for verifying HTML response bodys * Support relative links, including href="?foo=bar" (Kyle Hargraves) * Separated Rails-specific code from the Webrat core to make it easier to use Webrat with other environments diff --git a/lib/webrat/core/field.rb b/lib/webrat/core/field.rb index dde96ca..05cec00 100644 --- a/lib/webrat/core/field.rb +++ b/lib/webrat/core/field.rb @@ -30,11 +30,11 @@ module Webrat end def matches_id?(id) - @element["id"] == id.to_s + matches?(self.id, id) end def matches_name?(name) - @element["name"] == name.to_s + matches?(self.name, name) end def matches_label?(label_text) @@ -69,6 +69,14 @@ module Webrat protected + def matches?(string, string_or_regex) + if string_or_regex.is_a?(Regexp) + string_or_regex.match string + else + string == string_or_regex + end + end + def id @element["id"] end @@ -303,7 +311,7 @@ module Webrat class SelectField < Field def find_option(text) - options.detect { |o| o.matches_text?(text) } + options.detect { |o| o.matches_text?(text) || o.matches_value?(text)} end protected diff --git a/lib/webrat/core/select_option.rb b/lib/webrat/core/select_option.rb index c46224f..8df63dd 100644 --- a/lib/webrat/core/select_option.rb +++ b/lib/webrat/core/select_option.rb @@ -14,6 +14,10 @@ module Webrat end end + def matches_value?(value) + @element.attributes['value'] == value.to_s + end + def choose @select.raise_error_if_disabled @select.set(value) diff --git a/spec/api/selects_spec.rb b/spec/api/selects_spec.rb index d33afce..75e44d9 100644 --- a/spec/api/selects_spec.rb +++ b/spec/api/selects_spec.rb @@ -108,6 +108,18 @@ describe "selects" do @session.clicks_button end + it "should send value from option in list specified by value" do + @session.response_body = <<-EOS +
+ + +
+ EOS + @session.should_receive(:post).with("/login", "start_month" => "s1") + @session.selects "s1", :from => "start_month" + @session.clicks_button + end + it "should send value from option in list specified by label" do @session.response_body = <<-EOS