Skip to content

Commit

Permalink
save_and_open_page rewrites css and image references to provide a fri…
Browse files Browse the repository at this point in the history
…endlier debugging experience
  • Loading branch information
lukemelia committed May 5, 2008
1 parent c7765a1 commit 3bc2af2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions History.txt
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Manifest.txt
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -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"
Expand Down
17 changes: 13 additions & 4 deletions lib/webrat/page.rb
Expand Up @@ -99,7 +99,7 @@ def attaches_file(id_or_name_or_label, path)
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:
Expand All @@ -108,10 +108,15 @@ def save_and_open
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)

This comment has been minimized.

Copy link
@bjeanes

bjeanes Dec 4, 2008

Is there any way to turn this off? It is so bloody annoying because when running `rake spec:rcov` Safari opens about 15 blank webrat files that are all empty and completely destroys the ability to keep working while rcov is running in the background.

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,
Expand Down Expand Up @@ -292,5 +297,9 @@ def flunk(message)
raise message
end

def rewrite_css_and_image_references(response_html) # :nodoc
response_html.gsub(/"\/(stylesheets|images)/, RAILS_ROOT + '/public/\1')
end

end
end
45 changes: 45 additions & 0 deletions 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
<html><head>
<link href="/stylesheets/foo.css" media="screen" rel="stylesheet" type="text/css" />
</head><body>
<h1>Hello world</h1>
<img src="/images/bar.png" />
</body></html>
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

0 comments on commit 3bc2af2

Please sign in to comment.