Sha256: 600fbb68365ac04f02c157d422418271b5dead804e4621e401907213eb48cc5a

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module Capybara
  class << self
    def save_page(html, file_name=nil)
      file_name ||= "capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}#{rand(10**10)}.html"
      name = File.join(*[Capybara.save_and_open_page_path, file_name].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, file_name=nil)
      open_in_browser save_page(html, file_name)
    end

  protected

    def open_in_browser(path) # :nodoc
      require "launchy" # could raise LoadError
      raise LoadError unless Launchy::Version::MAJOR >= 2
      Launchy.open(path)
    rescue LoadError
      warn "You need launchy >=2.0.0 to open pages with `save_and_open_page`:\n" <<
        "gem 'launchy', '>= 2.0.0', :require => false, :group => :test"
    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capybara-2.0.0.beta2 lib/capybara/util/save_and_open_page.rb