From 7afa0f0561c833b492f3004b6ae43c8813b56f33 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 7 Apr 2008 18:48:45 +0100 Subject: [PATCH] All tests passing now. Yay! --- lib/webrat/field.rb | 19 +++- lib/webrat/form.rb | 24 +++--- lib/webrat/page.rb | 8 +- lib/webrat/select_option.rb | 1 - test/chooses_test.rb | 167 +++++++++++++++++++----------------- 5 files changed, 122 insertions(+), 97 deletions(-) diff --git a/lib/webrat/field.rb b/lib/webrat/field.rb index fb5152c..51ad39b 100644 --- a/lib/webrat/field.rb +++ b/lib/webrat/field.rb @@ -177,11 +177,28 @@ module Webrat 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 + def other_options + @form.fields.select { |f| f.name == name } + end + def default_value if @element["checked"] == "checked" - @element["value"] + @element["value"] || "on" else nil end diff --git a/lib/webrat/form.rb b/lib/webrat/form.rb index 19b5bc1..c557900 100644 --- a/lib/webrat/form.rb +++ b/lib/webrat/form.rb @@ -28,6 +28,18 @@ module Webrat nil 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 Page.new(@page.session, form_action, form_method, params) @@ -65,18 +77,6 @@ module Webrat fields.select { |f| field_types.include?(f.class) } 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 all_params = {} diff --git a/lib/webrat/page.rb b/lib/webrat/page.rb index 22c3b83..b0a45d2 100644 --- a/lib/webrat/page.rb +++ b/lib/webrat/page.rb @@ -58,11 +58,9 @@ module Webrat # # Example: # chooses 'First Option' - def chooses(field) - radio = find_field_by_name_or_label(field) - flunk("Could not find radio button #{field.inspect}") if radio.nil? - flunk("Input #{radio.inspect} is not a radio button") unless radio['type'] == 'radio' - add_form_data(radio, radio["value"] || "on") + def chooses(label) + field = find_field(label, RadioField) + field.choose end # Verifies that a an option element exists on the current page with the specified diff --git a/lib/webrat/select_option.rb b/lib/webrat/select_option.rb index a60aca1..a1ba3a6 100644 --- a/lib/webrat/select_option.rb +++ b/lib/webrat/select_option.rb @@ -7,7 +7,6 @@ module Webrat end def matches_text?(text) - # require "rubygems"; require "ruby-debug"; Debugger.start; debugger @element.innerHTML =~ /^\W*#{Regexp.escape(text.to_s)}\b/i end diff --git a/test/chooses_test.rb b/test/chooses_test.rb index 683204e..6b9afa8 100644 --- a/test/chooses_test.rb +++ b/test/chooses_test.rb @@ -1,78 +1,89 @@ -# require File.dirname(__FILE__) + "/helper" -# -# class ChoosesTest < Test::Unit::TestCase -# -# def setup -# @session = ActionController::Integration::Session.new -# @session.stubs(:assert_response) -# @session.stubs(:get_via_redirect) -# @session.stubs(:response).returns(@response=mock) -# end -# -# def test_should_fail_if_no_radio_buttons_found -# @response.stubs(:body).returns(<<-EOS) -#
-#
-# EOS -# -# assert_raises RuntimeError do -# @session.chooses "first option" -# end -# end -# -# def test_should_fail_if_input_is_not_a_radio_button -# @response.stubs(:body).returns(<<-EOS) -#
-# -#
-# EOS -# -# assert_raises RuntimeError do -# @session.chooses "first_option" -# end -# end -# -# def test_should_check_rails_style_radio_buttons -# @response.stubs(:body).returns(<<-EOS) -#
-# -# -# -# -# -#
-# EOS -# @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"}) -# @session.chooses "Male" -# @session.clicks_button -# end -# -# def test_should_only_submit_last_chosen_value -# @response.stubs(:body).returns(<<-EOS) -#
-# -# -# -# -# -#
-# EOS -# @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"}) -# @session.chooses "Female" -# @session.chooses "Male" -# @session.clicks_button -# end -# -# def test_should_result_in_the_value_on_being_posted_if_not_specified -# @response.stubs(:body).returns(<<-EOS) -#
-# -# -#
-# EOS -# @session.expects(:post_via_redirect).with("/login", "first_option" => "on") -# @session.chooses "first_option" -# @session.clicks_button -# end -# -# end \ No newline at end of file +require File.dirname(__FILE__) + "/helper" + +class ChoosesTest < Test::Unit::TestCase + + def setup + @session = ActionController::Integration::Session.new + @session.stubs(:assert_response) + @session.stubs(:get_via_redirect) + @session.stubs(:response).returns(@response=mock) + end + + def test_should_fail_if_no_radio_buttons_found + @response.stubs(:body).returns(<<-EOS) +
+
+ EOS + + assert_raises RuntimeError do + @session.chooses "first option" + end + end + + def test_should_fail_if_input_is_not_a_radio_button + @response.stubs(:body).returns(<<-EOS) +
+ +
+ EOS + + assert_raises RuntimeError do + @session.chooses "first_option" + end + end + + def test_should_check_rails_style_radio_buttons + @response.stubs(:body).returns(<<-EOS) +
+ + + + + +
+ EOS + @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"}) + @session.chooses "Male" + @session.clicks_button + end + + def test_should_only_submit_last_chosen_value + @response.stubs(:body).returns(<<-EOS) +
+ + + + + +
+ EOS + @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "M"}) + @session.chooses "Female" + @session.chooses "Male" + @session.clicks_button + end + + def test_should_result_in_the_value_on_being_posted_if_not_specified + @response.stubs(:body).returns(<<-EOS) +
+ + +
+ EOS + @session.expects(:post_via_redirect).with("/login", "first_option" => "on") + @session.chooses "first_option" + @session.clicks_button + end + + def test_should_result_in_the_value_on_being_posted_if_not_specified_and_checked_by_default + @response.stubs(:body).returns(<<-EOS) +
+ + +
+ EOS + @session.expects(:post_via_redirect).with("/login", "first_option" => "on") + @session.clicks_button + end + +end \ No newline at end of file