Using save_and_open_page to open failed cucumber scenerios in a browser with images and CSS
-Wednesday, April 21, 2010 By: Jon Kinney
I've been doing a lot of Cucumber testing lately and have really been liking it, but there is one thing that is hard with Cucumber... debugging errors. That is until I found out about the save_and_open_page method that webrat provides for opening a browser to the page the cuke puked on from Bodaniel Jeanes over on his blog.
The only problem was that the page didn't look very nice. I could have left it (it was working after all) but then I ran across an init script for cuke in a gist from Duff that allowed for the rewriting of the paths to the local image, JavaScript and CSS files so they at least could be referenced properly. While this worked, most of my images were specified as backgrounds in my actual CSS files so if I wanted to be able to see the page as it was intended I would need to find a way to change the paths in the actual CSS files as well.
After playing around with the idea of modifying my actual CSS files temporarily and then switching them back (and realizing that solution was full of fail) I settled on a way strip out all links to all css files and then embed the actual CSS inline in a style tag inside the page's head tag. This way I can more easily and permanently modify the css background image paths all in a self contained HTML file. I did this by looping through a specified directory on the file system that contains CSS files and looking for any stylesheets that the user specifies and in the order that the user specifies so that the hierarchy would remain in tact when the styles were all embedded inline.
Since Cucumber's env.rb file is auto-generated it's a bad place to store customized configuration options. Instead I encourage you to do what Cucumber says at the top of the env.rb file and put it somewhere else. From env.rb: "Cucumber will automatically load all features/**/*.rb files"
I chose to put this code in a file under the features/support directory called: open_browser_on_fail.rb. If it's sitting next to the env.rb file itself then you have the new file in the right place.
Place the following code in that file and you'll be good to go!
1 After do |scenario|
2 if scenario.status == :failed
3 save_and_open_page
4 end
5 end
6
7 module Webrat
8 module SaveAndOpenPage
9 def save_and_open_page
10 return unless File.exist?(Webrat.configuration.saved_pages_dir)
11
12 filename = "#{Webrat.configuration.saved_pages_dir}/webrat-#{Time.now.to_i}.html"
13
14 File.open(filename, "w") do |f|
15 f.write rewrite_public_file_references(response_body)
16 end
17
18 open_in_browser(filename)
19 end
20
21 def rewrite_public_file_references(response_html)
22 # remove conditional comments/ie stylesheets
23 response_html.gsub!(/<!--\[.*?\]-->/im, '')
24 # remove other stylesheets
25 response_html.gsub!(/<link href=(.*)\/>/i, '')
26
27 response_html.gsub!(/("|')\/(stylesheets|images|javascripts)/, '\1' + '../public' + '/\2')
28
29 response_html.gsub!(/<\/head>/i, "<style>#{rewrite_public_stylesheet_image_references('\/images', '../public')}<\/style>\n<\/head>")
30 end
31
32 def rewrite_public_stylesheet_image_references(regex, server_url)
33 dir = "/Users/jkinney/Sites/Rails/Client/new_rails_app/public/stylesheets/"
34
35 stylesheets = %w(reset admin main js_menu css_tabs firefox)
36
37 css = ""
38 stylesheets.each do |file|
39 # puts file
40 lines = []
41 File.open(dir+file+".css", "r"){|f| lines = f.readlines }
42 lines = lines.inject([]){|l, line| l << line.gsub(/#{regex}/i, "#{server_url}/images")}
43 css << lines.to_s
44 end
45 css
46 end
47
48 end
49 end
News & Events
Tech Review: Web Design For Developers - June 2009
A friend of mine in the web development community is releasing a book called "Web Design for Developers: A Programmer's Guide to Design Tools and Techniques". I was asked to do a tech review of the book and will be sharing my thoughts as well as some cool info presented in the book in a short series of upcoming blog posts. If you want to grab a beta copy of the book head over to my favorite publisher The Pragmatic Programmers.
First Class Audio Production - March 1st 2009
My most recent studio project was mixing and producing the latest a cappella album to come out of Eau Claire, WI. Until Proven Guilty is the Innocent Men's 4th studio album and marks a huge leap forward in recording and production quality for the group. Check back for demos of the mastered songs very soon! http://theinnocentmen.com.
Rate This Post: 5.0 (average)


