Add support for 'button' elements in forms, and selecting

them by inner text
This commit is contained in:
Nick 2008-04-17 11:31:40 -05:00
parent 13b0ec75f4
commit e6486b640b
3 changed files with 28 additions and 2 deletions

View File

@ -101,8 +101,12 @@ module Webrat
class ButtonField < Field class ButtonField < Field
def matches_text?(text)
@element.innerHTML =~ /#{Regexp.escape(text.to_s)}/i
end
def matches_value?(value) def matches_value?(value)
@element["value"] =~ /^\W*#{Regexp.escape(value.to_s)}/i @element["value"] =~ /^\W*#{Regexp.escape(value.to_s)}/i || matches_text?(value)
end end
def to_param def to_param

View File

@ -45,7 +45,7 @@ module Webrat
@fields = [] @fields = []
(@element / "input, textarea, select").each do |field_element| (@element / "button, input, textarea, select").each do |field_element|
@fields << Field.class_for_element(field_element).new(self, field_element) @fields << Field.class_for_element(field_element).new(self, field_element)
end end

View File

@ -332,4 +332,26 @@ class ClicksButtonTest < Test::Unit::TestCase
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""}) @session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""})
@session.clicks_button @session.clicks_button
end end
def test_should_recognize_button_tags
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" />
<button type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""})
@session.clicks_button
end
def test_should_recognize_button_tags_by_content
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" />
<button type="submit">Login</button>
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""})
@session.clicks_button "Login"
end
end end