Add save_and_open_page. Add radio button support via #chooses method

This commit is contained in:
Bryan Helmkamp 2008-03-02 20:06:43 -05:00
parent 7b85e0d15a
commit f7420463fd
6 changed files with 145 additions and 7 deletions

View File

@ -1,6 +1,8 @@
== SVN
* Fix bad reference to #select method in README (Patch from Luke Melia)
* Add save_and_open_page to aid in debugging
* Add radio button support via #chooses method
* Docfix: bad reference to #select method in README (Patch from Luke Melia)
* Allow specifying the input name/label when doing a select (Patch from David Chelimsky)
* Raise a specific exception if the developer tries to manipulate form elements before loading a page (Patch from James Deville)
* Add basic support for Rails-generated JavaScript link tags

View File

@ -1,4 +1,3 @@
Option button support
Full support for multiple forms on a page
Track the current form based on the location of the last manipulated input, use this as a default for clicks_button
Make current_url work with redirections

View File

@ -140,6 +140,18 @@ module ActionController
end
end
# Verifies that an input radio button exists on the current page and marks it
# as checked, so that the value will be submitted with the form.
#
# Example:
# chooses 'First Option'
def chooses(field)
radio = find_field_by_name_or_label(field)
return flunk("Could not find radio button #{field.inspect}") if radio.nil?
return flunk("Input #{radio.inspect} is not a radio button") unless radio.attributes['type'] == 'radio'
add_form_data(radio, radio.attributes["value"] || "on")
end
# Verifies that a submit button exists for the form, then submits the form, follows
# any redirects, and verifies the final page was successful.
#
@ -159,6 +171,21 @@ module ActionController
def submits_form(form_id = nil) # :nodoc:
end
# Saves the currently loaded page out to RAILS_ROOT/tmp/ and opens it in the default
# web browser if on OS X. Useful for debugging.
#
# Example:
# save_and_open_page
def save_and_open_page
return unless File.exist?(RAILS_ROOT + "/tmp")
filename = "webrat-#{Time.now.to_i}.html"
File.open(RAILS_ROOT + "/tmp/#{filename}", "w") do |f|
f.write response.body
end
`open tmp/#{filename}`
end
protected # Methods you could call, but probably shouldn't
def authenticity_token_value(onclick)
@ -222,7 +249,7 @@ module ActionController
def find_button(value = nil) # :nodoc:
return nil unless value
submit_buttons.detect { |el| el.attributes["value"] == value }
submit_buttons.detect { |el| el.attributes["value"] =~ /^\W*#{value}\b/i }
end
def add_form_data(input_element, value) # :nodoc:
@ -269,6 +296,11 @@ module ActionController
debug_log "REQUESTING PAGE: #{method.to_s.upcase} #{url} with #{data.inspect}"
@current_url = url
self.send "#{method}_via_redirect", @current_url, data || {}
if response.body =~ /Exception caught/ || response.body.blank?
save_and_open_page
end
assert_response :success
reset_dom
end
@ -343,7 +375,8 @@ module ActionController
def add_default_params_for(form) # :nodoc:
add_default_params_from_inputs_for(form)
add_default_params_from_checkboxes_for(form)
add_default_params_from_textateas_for(form)
add_default_params_from_radio_buttons_for(form)
add_default_params_from_textareas_for(form)
end
def add_default_params_from_inputs_for(form) # :nodoc:
@ -362,7 +395,16 @@ module ActionController
end
end
def add_default_params_from_textateas_for(form) # :nodoc:
def add_default_params_from_radio_buttons_for(form) # :nodoc:
(form / "input").each do |input|
next if input.attributes["type"] != "radio"
if input.attributes["checked"] == "checked"
add_form_data(input, input.attributes["value"])
end
end
end
def add_default_params_from_textareas_for(form) # :nodoc:
(form / "textarea").each do |input|
add_form_data(input, input.inner_html)
end

74
test/chooses_test.rb Normal file
View File

@ -0,0 +1,74 @@
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)
<form method="post" action="/login">
</form>
EOS
@session.expects(:flunk)
@session.chooses "first option"
end
def test_should_fail_if_input_is_not_a_radio_button
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="text" name="first_option" />
</form>
EOS
@session.expects(:flunk)
@session.chooses "first_option"
end
def test_should_check_rails_style_radio_buttons
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<input id="user_gender_female" name="user[gender]" type="radio" value="F" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
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)
<form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<input id="user_gender_female" name="user[gender]" type="radio" value="F" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
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)
<form method="post" action="/login">
<input type="radio" name="first_option" />
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "first_option" => "on")
@session.chooses "first_option"
@session.clicks_button
end
end

View File

@ -5,7 +5,8 @@ class ClicksButtonTest < Test::Unit::TestCase
@session = ActionController::Integration::Session.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@session.stubs(:response).returns(@response=mock)
@response = mock
@session.stubs(:response).returns(@response)
end
def test_should_fail_if_no_buttons
@ -158,6 +159,20 @@ class ClicksButtonTest < Test::Unit::TestCase
@session.clicks_button
end
def test_should_send_default_radio_options
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<input id="user_gender_female" name="user[gender]" type="radio" value="F" checked="checked" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "F"})
@session.clicks_button
end
def test_should_send_correct_data_for_rails_style_unchecked_fields
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">

View File

@ -1,10 +1,16 @@
require File.dirname(__FILE__) + "/helper"
RAILS_ROOT = "." unless defined?(RAILS_ROOT)
class VisitsTest < Test::Unit::TestCase
def setup
@session = ActionController::Integration::Session.new
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@response = mock
@session.stubs(:response).returns(@response)
@response.stubs(:body).returns("")
end
def test_should_use_get