diff --git a/History.txt b/History.txt index c6513a66..a4fcbd64 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,7 @@ +* Minor enhancements + + * Save and open page directory specified via configuration (Noah Davis) + * Bug fixes * Make "should contain" ignore extra whitespace when doing string comparisons (Noah Davis) diff --git a/lib/webrat/adapters/rails.rb b/lib/webrat/adapters/rails.rb index 1944c8f6..c7c99921 100644 --- a/lib/webrat/adapters/rails.rb +++ b/lib/webrat/adapters/rails.rb @@ -15,10 +15,6 @@ def doc_root File.expand_path(File.join(RAILS_ROOT, 'public')) end - def saved_page_dir - File.expand_path(File.join(RAILS_ROOT, "tmp")) - end - def get(url, data, headers = nil) do_request(:get, url, data, headers) end diff --git a/lib/webrat/core/configuration.rb b/lib/webrat/core/configuration.rb index 98bb6740..40ef5da6 100755 --- a/lib/webrat/core/configuration.rb +++ b/lib/webrat/core/configuration.rb @@ -26,6 +26,9 @@ class Configuration # Save and open pages with error status codes (500-599) in a browser? Defualts to true. attr_writer :open_error_files + # Save and open page storage directory, defaults to current directory + attr_accessor :saved_pages_dir + # Which rails environment should the selenium tests be run in? Defaults to selenium. attr_accessor :application_environment webrat_deprecate :selenium_environment, :application_environment @@ -60,6 +63,7 @@ class Configuration def initialize # :nodoc: self.open_error_files = true + self.saved_pages_dir = File.expand_path(".") self.application_environment = :test self.application_port = 3001 self.application_address = 'localhost' diff --git a/lib/webrat/core/save_and_open_page.rb b/lib/webrat/core/save_and_open_page.rb index 44264e9c..01e8b52e 100644 --- a/lib/webrat/core/save_and_open_page.rb +++ b/lib/webrat/core/save_and_open_page.rb @@ -6,9 +6,9 @@ module SaveAndOpenPage # Example: # save_and_open_page def save_and_open_page - return unless File.exist?(saved_page_dir) + return unless File.exist?(Webrat.configuration.saved_pages_dir) - filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.html" + filename = "#{Webrat.configuration.saved_pages_dir}/webrat-#{Time.now.to_i}.html" File.open(filename, "w") do |f| f.write rewrite_css_and_image_references(response_body) @@ -29,10 +29,6 @@ def rewrite_css_and_image_references(response_html) # :nodoc: response_html.gsub(/("|')\/(stylesheets|images)/, '\1' + doc_root + '/\2') end - def saved_page_dir #:nodoc: - File.expand_path(".") - end - def doc_root #:nodoc: nil end diff --git a/lib/webrat/selenium/selenium_session.rb b/lib/webrat/selenium/selenium_session.rb index 7d445064..53d6016d 100644 --- a/lib/webrat/selenium/selenium_session.rb +++ b/lib/webrat/selenium/selenium_session.rb @@ -195,9 +195,9 @@ def selenium def save_and_open_screengrab - return unless File.exist?(saved_page_dir) + return unless File.exist?(Webrat.configuration.saved_pages_dir) - filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.png" + filename = "#{Webrat.configuration.saved_pages_dir}/webrat-#{Time.now.to_i}.png" if $browser.chrome_backend? $browser.capture_entire_page_screenshot(filename, '') diff --git a/spec/private/core/configuration_spec.rb b/spec/private/core/configuration_spec.rb index 165ce4f6..fcb959f3 100755 --- a/spec/private/core/configuration_spec.rb +++ b/spec/private/core/configuration_spec.rb @@ -17,6 +17,11 @@ config.should open_error_files end + it "should default saved_pages_dir to current dir" do + config = Webrat::Configuration.new + config.saved_pages_dir.should == File.expand_path(".") + end + it "should detect infinite redirects after 10" do config = Webrat::Configuration.new config.infinite_redirect_limit.should == 10 diff --git a/spec/private/rails/rails_adapter_spec.rb b/spec/private/rails/rails_adapter_spec.rb index a5182e7d..af3d412b 100644 --- a/spec/private/rails/rails_adapter_spec.rb +++ b/spec/private/rails/rails_adapter_spec.rb @@ -76,10 +76,6 @@ end end - it "should provide a saved_page_dir" do - Webrat::RailsAdapter.new(mock("integration session")).should respond_to(:saved_page_dir) - end - it "should provide a doc_root" do Webrat::RailsAdapter.new(mock("integration session")).should respond_to(:doc_root) end diff --git a/spec/public/save_and_open_spec.rb b/spec/public/save_and_open_spec.rb index 7adba921..d207d231 100644 --- a/spec/public/save_and_open_spec.rb +++ b/spec/public/save_and_open_spec.rb @@ -22,10 +22,17 @@ Launchy::Browser.stub!(:run) @file_handle = mock("file handle") - File.stub!(:open).with(filename, 'w').and_yield(@file_handle) + File.stub!(:open).and_yield(@file_handle) @file_handle.stub!(:write) end + it "should save pages to the directory configured" do + Webrat.configuration.stub!(:saved_pages_dir => "path/to/dir") + File.should_receive(:open).with("path/to/dir/webrat-1234.html", "w").and_yield(@file_handle) + + save_and_open_page + end + it "should rewrite css rules" do @file_handle.should_receive(:write) do |html| html.should =~ %r|"#{webrat_session.doc_root}/stylesheets/foo.css"|s @@ -63,8 +70,4 @@ end.should_not raise_error end - def filename - File.expand_path("./webrat-#{Time.now}.html") - end - end