2009-11-16 16:02:16 -05:00
|
|
|
module Capybara
|
2009-11-14 10:20:15 -05:00
|
|
|
module SaveAndOpenPage
|
|
|
|
extend(self)
|
|
|
|
|
|
|
|
def save_and_open_page(html)
|
2009-11-22 12:12:40 -05:00
|
|
|
name="capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}.html"
|
2009-11-22 12:16:45 -05:00
|
|
|
|
2009-11-22 12:12:40 -05:00
|
|
|
FileUtils.touch(name) unless File.exist?(name)
|
2009-11-22 12:16:45 -05:00
|
|
|
|
2009-11-22 12:12:40 -05:00
|
|
|
tempfile = File.new(name,'w')
|
2009-11-14 10:20:15 -05:00
|
|
|
tempfile.write(rewrite_css_and_image_references(html))
|
|
|
|
tempfile.close
|
2009-11-22 12:16:45 -05:00
|
|
|
|
2009-11-14 10:20:15 -05:00
|
|
|
open_in_browser(tempfile.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_in_browser(path) # :nodoc
|
|
|
|
require "launchy"
|
|
|
|
Launchy::Browser.run(path)
|
|
|
|
rescue LoadError
|
|
|
|
warn "Sorry, you need to install launchy to open pages: `gem install launchy`"
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite_css_and_image_references(response_html) # :nodoc:
|
2009-11-16 16:02:16 -05:00
|
|
|
return response_html unless Capybara.asset_root
|
2010-01-23 06:29:55 -05:00
|
|
|
directories = Dir.new(Capybara.asset_root).entries.inject([]) do |list, name|
|
|
|
|
list << name if File.directory?(name) and not name.to_s =~ /^\./
|
|
|
|
list
|
2010-01-21 05:27:20 -05:00
|
|
|
end
|
2010-01-29 17:52:17 -05:00
|
|
|
response_html.gsub(/("|')\/(#{directories.join('|')})/, '\1' + Capybara.asset_root.to_s + '/\2')
|
2009-11-14 10:20:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|