1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/util/save_and_open_page.rb
2011-02-13 23:17:29 +01:00

43 lines
1.3 KiB
Ruby

module Capybara
class << self
def save_page(html)
name = File.join(*[Capybara.save_and_open_page_path, "capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}.html"].compact)
unless Capybara.save_and_open_page_path.nil? || File.directory?(Capybara.save_and_open_page_path )
FileUtils.mkdir_p(Capybara.save_and_open_page_path)
end
FileUtils.touch(name) unless File.exist?(name)
tempfile = File.new(name,'w')
tempfile.write(rewrite_css_and_image_references(html))
tempfile.close
tempfile.path
end
def save_and_open_page(html)
open_in_browser save_page(html)
end
protected
def open_in_browser(path) # :nodoc
require "launchy"
Launchy::Browser.run(path)
rescue LoadError
warn "Sorry, you need to install launchy (`gem install launchy`) and " <<
"make sure it's available to open pages with `save_and_open_page`."
end
def rewrite_css_and_image_references(response_html) # :nodoc:
root = Capybara.asset_root
return response_html unless root
directories = Dir.new(root).entries.select { |name|
(root+name).directory? and not name.to_s =~ /^\./
}
if not directories.empty?
response_html.gsub!(/("|')\/(#{directories.join('|')})/, '\1' + root.to_s + '/\2')
end
return response_html
end
end
end