From 39183b031a1ddbb0b6b9cc5e9a8c39639770438f Mon Sep 17 00:00:00 2001 From: Damian Janowski Date: Fri, 23 Apr 2010 18:57:11 -0300 Subject: [PATCH] Limit the amount of error pages open. --- lib/webrat/core/configuration.rb | 6 +++--- lib/webrat/core/session.rb | 13 +++++++++++-- spec/integration/rack/app.rb | 4 ++++ spec/integration/rack/test/webrat_rack_test.rb | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/webrat/core/configuration.rb b/lib/webrat/core/configuration.rb index 4e46a67..9a92af3 100755 --- a/lib/webrat/core/configuration.rb +++ b/lib/webrat/core/configuration.rb @@ -42,7 +42,7 @@ module Webrat # Which port should selenium use to access the application. Defaults to application_port attr_writer :application_port_for_selenium - + def application_port_for_selenium @application_port_for_selenium || self.application_port end @@ -91,9 +91,9 @@ module Webrat tmp_dir = Pathname.new(Dir.pwd).join("tmp") self.saved_pages_dir = tmp_dir.exist? ? tmp_dir : Dir.pwd end - + def open_error_files? #:nodoc: - @open_error_files ? true : false + !! @open_error_files end # Allows setting of webrat's mode, valid modes are: diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index 3f5f471..93b616e 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -76,6 +76,7 @@ For example: @default_headers = {} @custom_headers = {} @current_url = nil + @exceptions = 0 reset end @@ -118,8 +119,7 @@ For example: process_request(http_method, url, data, h) - save_and_open_page if exception_caught? && Webrat.configuration.open_error_files? - raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code? + process_response_errors reset @@ -299,5 +299,14 @@ For example: @_page_scope = nil end + def process_response_errors + if exception_caught? && @exceptions < 3 && Webrat.configuration.open_error_files? + save_and_open_page + @exceptions += 1 + end + + raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code? + end + end end diff --git a/spec/integration/rack/app.rb b/spec/integration/rack/app.rb index bbd18b5..95c7bf3 100644 --- a/spec/integration/rack/app.rb +++ b/spec/integration/rack/app.rb @@ -41,6 +41,10 @@ class RackApp < Sinatra::Base uploaded_file = params[:uploaded_file] Marshal.dump(:tempfile => uploaded_file[:tempfile].read, :type => uploaded_file[:type], :filename => uploaded_file[:filename]) end + + get "/error" do + ["Exception caught: you wanted it."] + end end __END__ diff --git a/spec/integration/rack/test/webrat_rack_test.rb b/spec/integration/rack/test/webrat_rack_test.rb index 1f0991d..1e29c34 100644 --- a/spec/integration/rack/test/webrat_rack_test.rb +++ b/spec/integration/rack/test/webrat_rack_test.rb @@ -58,6 +58,24 @@ class WebratRackTest < Test::Unit::TestCase assert_equal "webrat_rack_test.rb", upload[:filename] assert_equal File.read(__FILE__), upload[:tempfile] end + + def test_open_errors + $open_pages = [] + + Webrat.configure do |config| + config.open_error_files = true + end + + def webrat_session.open_in_browser(path) + $open_pages << path + end + + 5.times do + visit "/error" + end + + assert_equal 3, $open_pages.size + end end class WebratRackSetupTest < Test::Unit::TestCase