All tests passing now. Yay!

This commit is contained in:
Bryan Helmkamp 2008-04-07 18:48:45 +01:00
parent effc446daf
commit 7afa0f0561
5 changed files with 122 additions and 97 deletions

View File

@ -177,11 +177,28 @@ module Webrat
class RadioField < Field class RadioField < Field
def to_param
return nil if @value.nil?
super
end
def choose
other_options.each do |option|
option.unset
end
set(@element["value"] || "on")
end
protected protected
def other_options
@form.fields.select { |f| f.name == name }
end
def default_value def default_value
if @element["checked"] == "checked" if @element["checked"] == "checked"
@element["value"] @element["value"] || "on"
else else
nil nil
end end

View File

@ -29,6 +29,18 @@ module Webrat
nil nil
end end
def fields
return @fields if @fields
@fields = []
(@element / "input, textarea, select").each do |field_element|
@fields << Field.class_for_element(field_element).new(self, field_element)
end
@fields
end
def submit def submit
Page.new(@page.session, form_action, form_method, params) Page.new(@page.session, form_action, form_method, params)
end end
@ -65,18 +77,6 @@ module Webrat
fields.select { |f| field_types.include?(f.class) } fields.select { |f| field_types.include?(f.class) }
end end
def fields
return @fields if @fields
@fields = []
(@element / "input, textarea, select").each do |field_element|
@fields << Field.class_for_element(field_element).new(self, field_element)
end
@fields
end
def params def params
all_params = {} all_params = {}

View File

@ -58,11 +58,9 @@ module Webrat
# #
# Example: # Example:
# chooses 'First Option' # chooses 'First Option'
def chooses(field) def chooses(label)
radio = find_field_by_name_or_label(field) field = find_field(label, RadioField)
flunk("Could not find radio button #{field.inspect}") if radio.nil? field.choose
flunk("Input #{radio.inspect} is not a radio button") unless radio['type'] == 'radio'
add_form_data(radio, radio["value"] || "on")
end end
# Verifies that a an option element exists on the current page with the specified # Verifies that a an option element exists on the current page with the specified

View File

@ -7,7 +7,6 @@ module Webrat
end end
def matches_text?(text) def matches_text?(text)
# require "rubygems"; require "ruby-debug"; Debugger.start; debugger
@element.innerHTML =~ /^\W*#{Regexp.escape(text.to_s)}\b/i @element.innerHTML =~ /^\W*#{Regexp.escape(text.to_s)}\b/i
end end

View File

@ -1,78 +1,89 @@
# require File.dirname(__FILE__) + "/helper" require File.dirname(__FILE__) + "/helper"
#
# class ChoosesTest < Test::Unit::TestCase class ChoosesTest < Test::Unit::TestCase
#
# def setup def setup
# @session = ActionController::Integration::Session.new @session = ActionController::Integration::Session.new
# @session.stubs(:assert_response) @session.stubs(:assert_response)
# @session.stubs(:get_via_redirect) @session.stubs(:get_via_redirect)
# @session.stubs(:response).returns(@response=mock) @session.stubs(:response).returns(@response=mock)
# end end
#
# def test_should_fail_if_no_radio_buttons_found def test_should_fail_if_no_radio_buttons_found
# @response.stubs(:body).returns(<<-EOS) @response.stubs(:body).returns(<<-EOS)
# <form method="post" action="/login"> <form method="post" action="/login">
# </form> </form>
# EOS EOS
#
# assert_raises RuntimeError do assert_raises RuntimeError do
# @session.chooses "first option" @session.chooses "first option"
# end end
# end end
#
# def test_should_fail_if_input_is_not_a_radio_button def test_should_fail_if_input_is_not_a_radio_button
# @response.stubs(:body).returns(<<-EOS) @response.stubs(:body).returns(<<-EOS)
# <form method="post" action="/login"> <form method="post" action="/login">
# <input type="text" name="first_option" /> <input type="text" name="first_option" />
# </form> </form>
# EOS EOS
#
# assert_raises RuntimeError do assert_raises RuntimeError do
# @session.chooses "first_option" @session.chooses "first_option"
# end end
# end end
#
# def test_should_check_rails_style_radio_buttons def test_should_check_rails_style_radio_buttons
# @response.stubs(:body).returns(<<-EOS) @response.stubs(:body).returns(<<-EOS)
# <form method="get" action="/login"> <form method="get" action="/login">
# <input id="user_gender_male" name="user[gender]" type="radio" value="M" /> <input id="user_gender_male" name="user[gender]" type="radio" value="M" />
# <label for="user_gender_male">Male</label> <label for="user_gender_male">Male</label>
# <input id="user_gender_female" name="user[gender]" type="radio" value="F" /> <input id="user_gender_female" name="user[gender]" type="radio" value="F" />
# <label for="user_gender_female">Female</label> <label for="user_gender_female">Female</label>
# <input type="submit" /> <input type="submit" />
# </form> </form>
# EOS EOS
# @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"}) @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"})
# @session.chooses "Male" @session.chooses "Male"
# @session.clicks_button @session.clicks_button
# end end
#
# def test_should_only_submit_last_chosen_value def test_should_only_submit_last_chosen_value
# @response.stubs(:body).returns(<<-EOS) @response.stubs(:body).returns(<<-EOS)
# <form method="get" action="/login"> <form method="get" action="/login">
# <input id="user_gender_male" name="user[gender]" type="radio" value="M" /> <input id="user_gender_male" name="user[gender]" type="radio" value="M" />
# <label for="user_gender_male">Male</label> <label for="user_gender_male">Male</label>
# <input id="user_gender_female" name="user[gender]" type="radio" value="F" /> <input id="user_gender_female" name="user[gender]" type="radio" value="F" />
# <label for="user_gender_female">Female</label> <label for="user_gender_female">Female</label>
# <input type="submit" /> <input type="submit" />
# </form> </form>
# EOS EOS
# @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"}) @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"})
# @session.chooses "Female" @session.chooses "Female"
# @session.chooses "Male" @session.chooses "Male"
# @session.clicks_button @session.clicks_button
# end end
#
# def test_should_result_in_the_value_on_being_posted_if_not_specified def test_should_result_in_the_value_on_being_posted_if_not_specified
# @response.stubs(:body).returns(<<-EOS) @response.stubs(:body).returns(<<-EOS)
# <form method="post" action="/login"> <form method="post" action="/login">
# <input type="radio" name="first_option" /> <input type="radio" name="first_option" />
# <input type="submit" /> <input type="submit" />
# </form> </form>
# EOS EOS
# @session.expects(:post_via_redirect).with("/login", "first_option" => "on") @session.expects(:post_via_redirect).with("/login", "first_option" => "on")
# @session.chooses "first_option" @session.chooses "first_option"
# @session.clicks_button @session.clicks_button
# end end
#
# end def test_should_result_in_the_value_on_being_posted_if_not_specified_and_checked_by_default
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="radio" name="first_option" checked="checked"/>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "first_option" => "on")
@session.clicks_button
end
end