updates for local proxying for auth and stuff

This commit is contained in:
John Bintz 2012-09-10 15:01:06 -04:00
parent 6a2d290bdb
commit 85a1b148db
9 changed files with 121 additions and 14 deletions

View File

@ -8,10 +8,7 @@ $: << File.expand_path('../../lib', __FILE__)
require 'hollandaise' require 'hollandaise'
require 'hollandaise/cli' require 'hollandaise/cli'
begin Hollandaise.load_config!
load File.join(Dir.pwd, 'hollandaise.rb')
rescue LoadError => e
end
Hollandaise::CLI.start Hollandaise::CLI.start

View File

@ -19,5 +19,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'capybara' gem.add_dependency 'capybara'
gem.add_dependency 'thor' gem.add_dependency 'thor'
gem.add_dependency 'arbre' gem.add_dependency 'arbre'
gem.add_dependency 'rainbow'
gem.add_dependency 'rack-proxy'
end end

View File

@ -3,6 +3,8 @@ require 'hollandaise/browsers'
require 'hollandaise/project' require 'hollandaise/project'
require 'hollandaise/browser' require 'hollandaise/browser'
require 'hollandaise/railtie' if defined?(Rails::Railtie)
module Hollandaise module Hollandaise
class << self class << self
attr_accessor :url, :browsers, :delay attr_accessor :url, :browsers, :delay
@ -12,6 +14,17 @@ module Hollandaise
yield self yield self
end 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) def self.project(name)
project = Project.new(name) project = Project.new(name)
projects << project projects << project

View File

@ -3,12 +3,12 @@ module Hollandaise
class Base class Base
attr_reader :browser attr_reader :browser
def run_and_take_screenshot(url) def run_and_take_screenshot(url, screen_size = [ 1280, 1024 ])
@url = url @url = url
selenium.navigate.to url selenium.navigate.to url
sleep @options[:delay].to_i sleep @options[:delay].to_i
selenium.execute_script %{window.resizeTo(1280, 1024)} selenium.manage.window.resize_to(*screen_size)
take_screenshot take_screenshot
end end

View File

@ -12,8 +12,30 @@ module Hollandaise
@selenium ||= ::Sauce::Selenium2.new(info) @selenium ||= ::Sauce::Selenium2.new(info)
end 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 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 end
def info def info

View File

@ -11,6 +11,10 @@ module Hollandaise
@selenium ||= ::Selenium::WebDriver.for(@browser) @selenium ||= ::Selenium::WebDriver.for(@browser)
end end
def name
@browser
end
def target def target
dir.join("#{@browser}.png") dir.join("#{@browser}.png")
end end

View File

@ -1,4 +1,7 @@
require 'uri' require 'uri'
require 'rainbow'
require 'rack/proxy'
require 'base64'
module Hollandaise module Hollandaise
class Project class Project
@ -7,7 +10,51 @@ module Hollandaise
end end
def root(root) 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 end
def screenshot(name, uri) def screenshot(name, uri)
@ -20,11 +67,21 @@ module Hollandaise
@browsers = browsers @browsers = browsers
end end
def screen_size(size)
@screen_size = size.split('x').collect(&:to_i)
end
def run def run
puts "Running #{@project_name}".foreground(:green)
with_proxy do
urls.each do |url, name| urls.each do |url, name|
puts "... #{name} (#{url})".foreground(:yellow)
Hollandaise.chdir "#{@project_name}/#{name}" do Hollandaise.chdir "#{@project_name}/#{name}" do
browser_objects.run do |browser| browser_objects.run do |browser|
browser.run_and_take_screenshot(url) puts " ... #{browser.name}".foreground(:white)
browser.run_and_take_screenshot(url, @screen_size)
end
end end
end end
end end
@ -33,7 +90,7 @@ module Hollandaise
private private
def urls def urls
@screenshots.collect do |name, uri| @screenshots.collect do |name, uri|
[ @root.merge(URI(uri)), name ] [ File.join(@root, uri), name ]
end end
end end

View File

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

View File

@ -1,3 +1,3 @@
module Hollandaise module Hollandaise
VERSION = "0.1.0" VERSION = "0.1.1"
end end