From 3bc2af2c14fdea317756ae39a90acf5dc71f425a Mon Sep 17 00:00:00 2001 From: Luke Melia Date: Sun, 4 May 2008 23:44:00 -0400 Subject: [PATCH] save_and_open_page rewrites css and image references to provide a friendlier debugging experience --- History.txt | 1 + Manifest.txt | 1 + Rakefile | 2 +- lib/webrat/page.rb | 17 ++++++++++---- spec/save_and_open_spec.rb | 45 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 spec/save_and_open_spec.rb diff --git a/History.txt b/History.txt index 77af696..2043d6d 100644 --- a/History.txt +++ b/History.txt @@ -7,6 +7,7 @@ * Support matching select options by regexp (Patch from Kyle Hargraves) * Support relative links, including href="?foo=bar" (Patch from Kyle Hargraves) * Support links to fully qualified URLs starting with http:// or https:// (Luke Melia) + * save_and_open_page rewrites css and image references to provide a friendlier debugging experience (Luke Melia) * Bug fixes diff --git a/Manifest.txt b/Manifest.txt index fb9900b..94b69bb 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -34,6 +34,7 @@ spec/clicks_link_spec.rb spec/fills_in_spec.rb spec/rcov.opts spec/reloads_spec.rb +spec/save_and_open_page_spec.rb spec/selects_spec.rb spec/spec.opts spec/spec_helper.rb diff --git a/Rakefile b/Rakefile index 35fcfc8..008dc48 100644 --- a/Rakefile +++ b/Rakefile @@ -56,7 +56,7 @@ end require 'spec/rake/verify_rcov' RCov::VerifyTask.new(:verify_rcov => :rcov) do |t| - t.threshold = 95.5 # Make sure you have rcov 0.7 or higher! + t.threshold = 96.3 end remove_task "default" diff --git a/lib/webrat/page.rb b/lib/webrat/page.rb index 17fcd0e..e2bf648 100644 --- a/lib/webrat/page.rb +++ b/lib/webrat/page.rb @@ -99,7 +99,7 @@ module Webrat field.set(path) end - # Saves the currently loaded page out to RAILS_ROOT/tmp/ and opens it in the default + # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default # web browser if on OS X. Useful for debugging. # # Example: @@ -108,10 +108,15 @@ module Webrat 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 + path = RAILS_ROOT + "/tmp/#{filename}" + File.open(path, "w") do |f| + f.write rewrite_css_and_image_references(response.body) end - `open tmp/#{filename}` + open_in_browser(path) + end + + def open_in_browser(path) # :nodoc + `open #{path}` end # Issues a request for the URL pointed to by a link on the current page, @@ -292,5 +297,9 @@ module Webrat raise message end + def rewrite_css_and_image_references(response_html) # :nodoc + response_html.gsub(/"\/(stylesheets|images)/, RAILS_ROOT + '/public/\1') + end + end end \ No newline at end of file diff --git a/spec/save_and_open_spec.rb b/spec/save_and_open_spec.rb new file mode 100644 index 0000000..d0a1191 --- /dev/null +++ b/spec/save_and_open_spec.rb @@ -0,0 +1,45 @@ +require File.expand_path(File.dirname(__FILE__) + "/spec_helper") + +RAILS_ROOT = "." unless defined?(RAILS_ROOT) + +describe "save_and_open_page" do + before do + @session = ActionController::Integration::Session.new + @response = mock + @session.stubs(:response).returns(@response) + @response.stubs(:body).returns(<<-HTML + + + +

Hello world

+ + + HTML + ) + File.stubs(:exist?).returns(true) + Time.stubs(:now).returns(1234) + end + + it "should rewrite css rules" do + file_handle = mock() + File.expects(:open).with(RAILS_ROOT + "/tmp/webrat-1234.html", 'w').yields(file_handle) + file_handle.expects(:write).with{|html| html =~ %r|#{RAILS_ROOT}/public/stylesheets/foo.css|s } + Webrat::Page.any_instance.stubs(:open_in_browser) + @session.save_and_open_page + end + + it "should rewrite image paths" do + file_handle = mock() + File.expects(:open).with(RAILS_ROOT + "/tmp/webrat-1234.html", 'w').yields(file_handle) + file_handle.expects(:write).with{|html| html =~ %r|#{RAILS_ROOT}/public/images/bar.png|s } + Webrat::Page.any_instance.stubs(:open_in_browser) + @session.save_and_open_page + end + + it "should open the temp file in a browser" do + File.stubs(:open) + Webrat::Page.any_instance.expects(:open_in_browser).with(RAILS_ROOT + "/tmp/webrat-1234.html") + @session.save_and_open_page + end + +end