Support selecting options by their values (Alex Lang)

This commit is contained in:
Bryan Helmkamp 2008-10-21 22:41:02 -04:00
parent 7adeb86f2c
commit 1b51de8f0f
4 changed files with 29 additions and 4 deletions

View File

@ -10,7 +10,8 @@
* Minor enhancements * 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 * Add should_see and should_not_see for verifying HTML response bodys
* Support relative links, including href="?foo=bar" (Kyle Hargraves) * 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 * Separated Rails-specific code from the Webrat core to make it easier to use Webrat with other environments

View File

@ -30,11 +30,11 @@ module Webrat
end end
def matches_id?(id) def matches_id?(id)
@element["id"] == id.to_s matches?(self.id, id)
end end
def matches_name?(name) def matches_name?(name)
@element["name"] == name.to_s matches?(self.name, name)
end end
def matches_label?(label_text) def matches_label?(label_text)
@ -69,6 +69,14 @@ module Webrat
protected 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 def id
@element["id"] @element["id"]
end end
@ -303,7 +311,7 @@ module Webrat
class SelectField < Field class SelectField < Field
def find_option(text) def find_option(text)
options.detect { |o| o.matches_text?(text) } options.detect { |o| o.matches_text?(text) || o.matches_value?(text)}
end end
protected protected

View File

@ -14,6 +14,10 @@ module Webrat
end end
end end
def matches_value?(value)
@element.attributes['value'] == value.to_s
end
def choose def choose
@select.raise_error_if_disabled @select.raise_error_if_disabled
@select.set(value) @select.set(value)

View File

@ -108,6 +108,18 @@ describe "selects" do
@session.clicks_button @session.clicks_button
end end
it "should send value from option in list specified by value" do
@session.response_body = <<-EOS
<form method="post" action="/login">
<select name="start_month"><option value="s1">January</option></select>
<input type="submit" />
</form>
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 it "should send value from option in list specified by label" do
@session.response_body = <<-EOS @session.response_body = <<-EOS
<form method="post" action="/login"> <form method="post" action="/login">