Sha256: e972a94f9d2088ac9340fbbf54ee75ee6d577897fc8c6428c935adc147f76640

Contents?: true

Size: 1.18 KB

Versions: 38

Compression:

Stored size: 1.18 KB

Contents

module Webrat
  module SaveAndOpenPage
    # Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
    # web browser if on OS X. Useful for debugging.
    # 
    # Example:
    #   save_and_open_page
    def save_and_open_page
      return unless File.exist?(saved_page_dir)

      filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.html"
      
      File.open(filename, "w") do |f|
        f.write rewrite_css_and_image_references(response_body)
      end

      open_in_browser(filename)
    end
    
    def open_in_browser(path) # :nodoc
      platform = ruby_platform
      if platform =~ /cygwin/ || platform =~ /win32/
        `rundll32 url.dll,FileProtocolHandler #{path.gsub("/", "\\\\")}`
      elsif platform =~ /darwin/
        `open #{path}`
      end
    end
    
    def rewrite_css_and_image_references(response_html) # :nodoc:
      return response_html unless doc_root
      response_html.gsub(/"\/(stylesheets|images)/, doc_root + '/\1')
    end
    
    def saved_page_dir #:nodoc:
      File.expand_path(".")
    end
    
    def doc_root #:nodoc:
      nil
    end
  
  private

    # accessor for testing
    def ruby_platform
      RUBY_PLATFORM
    end
    
  end
end

Version data entries

38 entries across 38 versions & 12 rubygems

Version Path
adva-0.2.4 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.2.3 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.2.2 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.2.1 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.2.0 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.1.4 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.1.3 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.1.2 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.1.1 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.1.0 test/webrat/lib/webrat/core/save_and_open_page.rb
adva-0.0.1 test/webrat/lib/webrat/core/save_and_open_page.rb
aslakhellesoy-webrat-0.3.2.1 lib/webrat/core/save_and_open_page.rb
aslakhellesoy-webrat-0.3.2.2 lib/webrat/core/save_and_open_page.rb
auxesis-webrat-0.4.1 lib/webrat/core/save_and_open_page.rb
brynary-webrat-0.3.2.2 lib/webrat/core/save_and_open_page.rb
brynary-webrat-0.4.0 lib/webrat/core/save_and_open_page.rb
diabolo-webrat-0.4.2 lib/webrat/core/save_and_open_page.rb
hectoregm-webrat-0.4.2 lib/webrat/core/save_and_open_page.rb
jferris-webrat-0.4.3.0.1238640835 lib/webrat/core/save_and_open_page.rb
jferris-webrat-0.4.3 lib/webrat/core/save_and_open_page.rb