Created
August 27, 2015 06:54
-
-
Save thenoseman/40969385ff775bc43dc2 to your computer and use it in GitHub Desktop.
save css files when using save_and_open_page in capybara
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 Capybara | |
class Session | |
def save_page(path=nil) | |
body = rewrite_css_and_image_references(html) | |
path ||= "capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}#{rand(10**10)}.html" | |
path = File.expand_path(path, save_folder_path) | |
FileUtils.mkdir_p(File.dirname(path)) | |
File.open(path,'w') { |f| f.write(body) } | |
path | |
end | |
private | |
def rewrite_css_and_image_references(response_html) # :nodoc: | |
return response_html unless Capybara.app_host.present? | |
directories = Dir.new(Rails.root.join("app/assets")).entries.inject([]) do |list, name| | |
list << name if File.directory?(name) and not name.to_s =~ /^\./ | |
list | |
end | |
response_html.gsub(/("|')\/(#{directories.join('|')})/, '\1' + Capybara.app_host + '/\2') | |
end | |
def save_folder_path | |
Capybara.save_and_open_page_path.present?? Capybara.save_and_open_page_path : "tmp/capybara" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment