Created
March 3, 2010 19:13
-
-
Save duff/320890 to your computer and use it in GitHub Desktop.
Webrat monkey patch so save_and_open_page can use stylesheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Webrat | |
module SaveAndOpenPage | |
def save_and_open_page | |
return unless File.exist?(Webrat.configuration.saved_pages_dir) | |
filename = "#{Webrat.configuration.saved_pages_dir}/webrat-#{Time.now.to_i}.html" | |
File.open(filename, "w") do |f| | |
f.write prettified(response_body) | |
end | |
open_in_browser(filename) | |
end | |
def prettified(response_html) | |
inline_css(response_html) | |
rewrite_image_and_js_references(response_html) | |
end | |
def inline_css(response_html) | |
remove_conditional_ie(response_html) | |
insert_css(response_html) | |
remove_stylesheet_references(response_html) | |
end | |
def remove_conditional_ie(response_html) | |
response_html.gsub!(/<!--\[.*?\]-->/im, '') | |
end | |
def remove_stylesheet_references(response_html) | |
response_html.gsub!(/<link href=(.*)\/>/i, '') | |
end | |
def rewrite_image_and_js_references(response_html) | |
response_html.gsub!(/("|')\/(images|javascripts)/, '\1' + '../public' + '/\2') | |
end | |
def insert_css(response_html) | |
response_html.gsub!(/<\/head>/, "<style>#{rewrite_css_image_references(response_html)}<\/style>\n<\/head>") | |
end | |
def rewrite_css_image_references(response_html) | |
css_files(response_html).map do |file| | |
content = File.read("public/stylesheets/#{file}.css") | |
content.gsub(/\/images/, "../public/images") | |
end.join("\n") | |
end | |
def css_files(response_html) | |
response_html.scan(/\<link href=".*stylesheets\/([^"]*).css/).flatten | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Incorporated some changes from Jon Kinney:
http://jonkinney.com/articles/2010/04/21/using-save_and_open_page-to-open-failed-cucumber-scenerios-in-a-browser-with-images-and-css/
In particular, the idea of inlining the css to handle image filenames there.