diff --git a/bin/hollandaise b/bin/hollandaise index 6ba45ea..5be44d1 100755 --- a/bin/hollandaise +++ b/bin/hollandaise @@ -8,10 +8,7 @@ $: << File.expand_path('../../lib', __FILE__) require 'hollandaise' require 'hollandaise/cli' -begin - load File.join(Dir.pwd, 'hollandaise.rb') -rescue LoadError => e -end +Hollandaise.load_config! Hollandaise::CLI.start diff --git a/hollandaise.gemspec b/hollandaise.gemspec index 3ffc5ab..50aed71 100644 --- a/hollandaise.gemspec +++ b/hollandaise.gemspec @@ -19,5 +19,7 @@ Gem::Specification.new do |gem| gem.add_dependency 'capybara' gem.add_dependency 'thor' gem.add_dependency 'arbre' + gem.add_dependency 'rainbow' + gem.add_dependency 'rack-proxy' end diff --git a/lib/hollandaise.rb b/lib/hollandaise.rb index 4cb4208..57783ab 100644 --- a/lib/hollandaise.rb +++ b/lib/hollandaise.rb @@ -3,6 +3,8 @@ require 'hollandaise/browsers' require 'hollandaise/project' require 'hollandaise/browser' +require 'hollandaise/railtie' if defined?(Rails::Railtie) + module Hollandaise class << self attr_accessor :url, :browsers, :delay @@ -12,6 +14,17 @@ module Hollandaise yield self end + def self.load_config! + begin + load File.join(Dir.pwd, 'hollandaise.rb') + rescue LoadError => e + begin + load File.join(Dir.pwd, 'config/hollandaise.rb') + rescue LoadError => e + end + end + end + def self.project(name) project = Project.new(name) projects << project diff --git a/lib/hollandaise/browser/base.rb b/lib/hollandaise/browser/base.rb index 1491b6f..59ad828 100644 --- a/lib/hollandaise/browser/base.rb +++ b/lib/hollandaise/browser/base.rb @@ -3,12 +3,12 @@ module Hollandaise class Base attr_reader :browser - def run_and_take_screenshot(url) + def run_and_take_screenshot(url, screen_size = [ 1280, 1024 ]) @url = url selenium.navigate.to url sleep @options[:delay].to_i - selenium.execute_script %{window.resizeTo(1280, 1024)} + selenium.manage.window.resize_to(*screen_size) take_screenshot end diff --git a/lib/hollandaise/browser/sauce.rb b/lib/hollandaise/browser/sauce.rb index d4b5526..c3929f4 100644 --- a/lib/hollandaise/browser/sauce.rb +++ b/lib/hollandaise/browser/sauce.rb @@ -12,8 +12,30 @@ module Hollandaise @selenium ||= ::Sauce::Selenium2.new(info) end + def run_and_take_screenshot(url, *args) + self.class.start_tunnel if URI(url).host == 'localhost' + + super + end + + def self.start_tunnel + return @tunnel if @tunnel + + @tunnel = ::Sauce::Connect.new + @tunnel.connect + @tunnel.wait_until_ready + + at_exit do + @tunnel.disconnect + end + end + def target - dir.join(@os.to_s).join("#{@browser} #{@version}.png") + dir.join(@os.to_s).join("#{@browser} #{@version || 'latest'}.png") + end + + def name + "#{@browser} #{@version} #{@os}" end def info diff --git a/lib/hollandaise/browser/selenium.rb b/lib/hollandaise/browser/selenium.rb index 00a0375..4819958 100644 --- a/lib/hollandaise/browser/selenium.rb +++ b/lib/hollandaise/browser/selenium.rb @@ -11,6 +11,10 @@ module Hollandaise @selenium ||= ::Selenium::WebDriver.for(@browser) end + def name + @browser + end + def target dir.join("#{@browser}.png") end diff --git a/lib/hollandaise/project.rb b/lib/hollandaise/project.rb index 3b1f287..a051877 100644 --- a/lib/hollandaise/project.rb +++ b/lib/hollandaise/project.rb @@ -1,4 +1,7 @@ require 'uri' +require 'rainbow' +require 'rack/proxy' +require 'base64' module Hollandaise class Project @@ -7,7 +10,51 @@ module Hollandaise end def root(root) - @root = URI(root) + @root = root + end + + def port + 32516 + end + + def proxy_root(root) + uri = URI(root) + + @userinfo = uri.userinfo + @host = uri.host + @port = uri.port + + @root = "http://localhost:#{port}#{uri.path}" + end + + def with_proxy + if !@host + yield + else + proxy = Class.new(Rack::Proxy) do + def initialize(userinfo, host, port) + @userinfo, @host, @port = userinfo, host, port + end + + def rewrite_env(env) + env['HTTP_HOST'] = @host + env['SERVER_PORT'] = @port + env['HTTP_AUTHORIZATION'] = "Basic #{Base64.encode64(@userinfo)}" + + env + end + end + + server = Thread.new do + Rack::Handler.default.run(proxy.new(@userinfo, @host, @port), :Port => port) do |server| + Thread.current[:server] = server + end + end + + yield + + server[:server].stop + end end def screenshot(name, uri) @@ -20,11 +67,21 @@ module Hollandaise @browsers = browsers end + def screen_size(size) + @screen_size = size.split('x').collect(&:to_i) + end + def run - urls.each do |url, name| - Hollandaise.chdir "#{@project_name}/#{name}" do - browser_objects.run do |browser| - browser.run_and_take_screenshot(url) + puts "Running #{@project_name}".foreground(:green) + with_proxy do + urls.each do |url, name| + puts "... #{name} (#{url})".foreground(:yellow) + + Hollandaise.chdir "#{@project_name}/#{name}" do + browser_objects.run do |browser| + puts " ... #{browser.name}".foreground(:white) + browser.run_and_take_screenshot(url, @screen_size) + end end end end @@ -33,7 +90,7 @@ module Hollandaise private def urls @screenshots.collect do |name, uri| - [ @root.merge(URI(uri)), name ] + [ File.join(@root, uri), name ] end end diff --git a/lib/hollandaise/railtie.rb b/lib/hollandaise/railtie.rb new file mode 100644 index 0000000..f797c2f --- /dev/null +++ b/lib/hollandaise/railtie.rb @@ -0,0 +1,12 @@ +module Hollandaise + class Railtie < ::Rails::Railtie + rake_tasks do + desc "Take screenshots from Hollandaise configuration" + task :take_screenshots do + Hollandaise.load_config! + + Hollandaise.projects.each(&:run) + end + end + end +end diff --git a/lib/hollandaise/version.rb b/lib/hollandaise/version.rb index 1a996c3..cd4d27c 100644 --- a/lib/hollandaise/version.rb +++ b/lib/hollandaise/version.rb @@ -1,3 +1,3 @@ module Hollandaise - VERSION = "0.1.0" + VERSION = "0.1.1" end