From 7ee7bae7574ea5c19a5770fa2120a508664d4feb Mon Sep 17 00:00:00 2001 From: Kamal Fariz Mahyuddin Date: Sat, 8 Mar 2008 14:13:38 +0800 Subject: [PATCH] Added reloads method to reload the page (note: it may resubmit forms) --- History.txt | 1 + lib/webrat/session.rb | 26 ++++++++++++++++++-------- test/reloads_test.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 test/reloads_test.rb diff --git a/History.txt b/History.txt index 5efe6f3..3437fa9 100644 --- a/History.txt +++ b/History.txt @@ -1,5 +1,6 @@ == Trunk +* Added reloads method to reload the page (Patch from Kamal Fariz Mahyuddi) * Prevent making a request if clicking on local anchor link (Patch from Kamal Fariz Mahyuddi) * Serialize empty text field values just like browsers (Patch from Kamal Fariz Mahyuddi) * Added clicks_link_within(selector, link_text), allowing restricting link search diff --git a/lib/webrat/session.rb b/lib/webrat/session.rb index 1d756f8..0a8168f 100644 --- a/lib/webrat/session.rb +++ b/lib/webrat/session.rb @@ -177,14 +177,23 @@ module ActionController # 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 - + + 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 + + # Reloads the last page requested. Note that this will resubmit forms + # and their data. + # + # Example: + # reloads + def reloads + request_page(*@last_request_args) if defined?(@last_request_args) && @last_request_args + end + protected # Methods you could call, but probably shouldn't def authenticity_token_value(onclick) @@ -313,6 +322,7 @@ module ActionController def request_page(method, url, data = {}) # :nodoc: debug_log "REQUESTING PAGE: #{method.to_s.upcase} #{url} with #{data.inspect}" @current_url = url + @last_request_args = [method, url, data] self.send "#{method}_via_redirect", @current_url, data || {} if response.body =~ /Exception caught/ || response.body.blank? diff --git a/test/reloads_test.rb b/test/reloads_test.rb new file mode 100644 index 0000000..1f3e1ff --- /dev/null +++ b/test/reloads_test.rb @@ -0,0 +1,26 @@ +require File.dirname(__FILE__) + "/helper" + +RAILS_ROOT = "." unless defined?(RAILS_ROOT) + +class ReloadsTest < 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_reload_the_page + @session.expects(:get_via_redirect).with("/", {}).times(2) + @session.visits("/") + @session.reloads + end + + def test_should_not_request_page_if_not_visited_any_page + @session.expects(:get_via_redirect).never + @session.reloads + end +end