From b59c749c5c2f4cd0eabc6b895c5065863c101a7e Mon Sep 17 00:00:00 2001 From: Jonas Nicklas and Kevin Fitzpatrick Date: Sat, 14 Nov 2009 16:20:15 +0100 Subject: [PATCH] Added save_and_open_page. Mmmmm. --- Manifest.txt | 1 + lib/webcat.rb | 2 +- lib/webcat/dsl.rb | 2 +- lib/webcat/rails.rb | 3 ++- lib/webcat/save_and_open_page.rb | 25 +++++++++++++++++++++++++ lib/webcat/session.rb | 5 +++++ 6 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 lib/webcat/save_and_open_page.rb diff --git a/Manifest.txt b/Manifest.txt index b2d537ac..4e4978e5 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -13,6 +13,7 @@ lib/webcat/driver/safariwatir_driver.rb lib/webcat/driver/selenium_driver.rb lib/webcat/dsl.rb lib/webcat/rails.rb +lib/webcat/save_and_open_page.rb lib/webcat/server.rb lib/webcat/session.rb script/console diff --git a/lib/webcat.rb b/lib/webcat.rb index 7c01ff4f..6173ca2b 100644 --- a/lib/webcat.rb +++ b/lib/webcat.rb @@ -6,7 +6,7 @@ module Webcat class ElementNotFound < WebcatError; end class << self - attr_accessor :debug + attr_accessor :debug, :asset_root def log(message) puts "[webcat] #{message}" if debug diff --git a/lib/webcat/dsl.rb b/lib/webcat/dsl.rb index bdeca099..fae276d8 100644 --- a/lib/webcat/dsl.rb +++ b/lib/webcat/dsl.rb @@ -36,7 +36,7 @@ module Webcat SESSION_METHODS = [ :visit, :body, :click_link, :click_button, :fill_in, :choose, - :check, :uncheck, :attach_file, :select, :has_content?, :within + :check, :uncheck, :attach_file, :select, :has_content?, :within, :save_and_open_page ] SESSION_METHODS.each do |method| class_eval <<-RUBY, __FILE__, __LINE__+1 diff --git a/lib/webcat/rails.rb b/lib/webcat/rails.rb index 7d2cc64f..85fbb76f 100644 --- a/lib/webcat/rails.rb +++ b/lib/webcat/rails.rb @@ -1,4 +1,5 @@ require 'webcat' require 'webcat/dsl' -Webcat.app = ActionController::Dispatcher.new \ No newline at end of file +Webcat.app = ActionController::Dispatcher.new +Webcat.asset_root = Rails.root.join('public') \ No newline at end of file diff --git a/lib/webcat/save_and_open_page.rb b/lib/webcat/save_and_open_page.rb new file mode 100644 index 00000000..9a5b4ce7 --- /dev/null +++ b/lib/webcat/save_and_open_page.rb @@ -0,0 +1,25 @@ +module Webcat + module SaveAndOpenPage + extend(self) + + def save_and_open_page(html) + require 'tempfile' + tempfile = Tempfile.new("webcat#{rand(1000000)}") + tempfile.write(rewrite_css_and_image_references(html)) + tempfile.close + 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: + return response_html unless Webcat.asset_root + response_html.gsub(/("|')\/(stylesheets|images)/, '\1' + Webcat.asset_root + '/\2') + end + end +end diff --git a/lib/webcat/session.rb b/lib/webcat/session.rb index a784abbf..662551e9 100644 --- a/lib/webcat/session.rb +++ b/lib/webcat/session.rb @@ -68,6 +68,11 @@ class Webcat::Session yield scopes.pop end + + def save_and_open_page + require 'webcat/save_and_open_page' + Webcat::SaveAndOpenPage.save_and_open_page(body) + end private