Added save_and_open_page. Mmmmm.

This commit is contained in:
Jonas Nicklas and Kevin Fitzpatrick 2009-11-14 16:20:15 +01:00 committed by Jonas Nicklas
parent 90ea86ffd1
commit b59c749c5c
6 changed files with 35 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -1,4 +1,5 @@
require 'webcat'
require 'webcat/dsl'
Webcat.app = ActionController::Dispatcher.new
Webcat.app = ActionController::Dispatcher.new
Webcat.asset_root = Rails.root.join('public')

View File

@ -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

View File

@ -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