add project mode

This commit is contained in:
John Bintz 2012-09-10 13:25:11 -04:00
parent 8a526f8c77
commit b8a827dd2d
7 changed files with 136 additions and 27 deletions

9
hollandaise.rb Normal file
View File

@ -0,0 +1,9 @@
Hollandaise.project "cats" do |p|
p.root 'http://my.site/'
p.screenshot 'Home', '/'
p.screenshot 'Archive', '/archive'
p.browsers :chrome
end

View File

@ -1,14 +1,41 @@
require "hollandaise/version" require "hollandaise/version"
require 'hollandaise/browsers' require 'hollandaise/browsers'
require 'hollandaise/project'
require 'hollandaise/browser' require 'hollandaise/browser'
module Hollandaise module Hollandaise
class << self class << self
attr_accessor :url, :browsers attr_accessor :url, :browsers, :delay
end end
def self.configure def self.configure
yield self yield self
end end
def self.project(name)
project = Project.new(name)
projects << project
yield project
project
end
def self.dir
@dir ||= Pathname("screenshots")
end
def self.chdir(name)
_odir = dir.clone
@dir = @dir.join(name)
yield
@dir = _odir
end
def self.projects
@projects ||= []
end
end end

View File

@ -3,16 +3,13 @@ module Hollandaise
class Base class Base
attr_reader :browser attr_reader :browser
def run(url) def run_and_take_screenshot(url)
@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.execute_script %{window.resizeTo(1280, 1024)}
end take_screenshot
def dir
@options[:dir]
end end
def take_screenshot def take_screenshot
@ -26,6 +23,10 @@ module Hollandaise
@selenium = nil @selenium = nil
end end
def dir
Hollandaise.dir
end
end end
end end
end end

View File

@ -1,11 +1,7 @@
module Hollandaise module Hollandaise
module Browsers class Browsers
def self.for(browsers, options) def self.for(browsers, options = {})
browsers.collect { |browser| Hollandaise::Browser.for(self.send(browser), options) } new(browsers.flatten.collect { |browser| Hollandaise::Browser.for(self.send(browser), options) })
end
def self.each(browsers, options, &block)
self.for(browsers, options).each(&block)
end end
def self.ie7 def self.ie7
@ -35,5 +31,23 @@ module Hollandaise
def self.chrome def self.chrome
[ :selenium, :chrome ] [ :selenium, :chrome ]
end end
def initialize(browsers)
@browsers = browsers
end
def each(&block)
@browsers.each(&block)
end
def run
@browsers.each do |browser|
begin
yield browser
ensure
browser.close
end
end
end
end end
end end

View File

@ -3,6 +3,21 @@ require 'pathname'
module Hollandaise module Hollandaise
class CLI < Thor class CLI < Thor
include Thor::Actions
source_root File.expand_path('../../../skel', __FILE__)
desc "cook", "Use the config file and take all the screenshots listed in there"
def cook
Hollandaise.projects.each(&:run)
end
desc "project NAME", "Create a skeletal project file"
def project(name)
@name = name
template 'hollandaise.rb.tt', 'hollandaise.rb'
end
desc "sauce URL BROWSER BROWSER...", "Take screenshots of a URL on Sauce Labs" desc "sauce URL BROWSER BROWSER...", "Take screenshots of a URL on Sauce Labs"
method_options :delay => 0 method_options :delay => 0
def sauce(*browsers) def sauce(*browsers)
@ -16,15 +31,10 @@ module Hollandaise
browsers = Hollandaise.browsers browsers = Hollandaise.browsers
end end
browser_objects = Hollandaise::Browsers.for(browsers, options.merge(:dir => dir)) browser_objects = Hollandaise::Browsers.for(browsers, options)
browser_objects.each do |browser| browser_objects.run do |browser|
begin browser.run_and_take_screenshot(url)
browser.run(url)
browser.take_screenshot
ensure
browser.close
end
end end
html = Arbre::Context.new do html = Arbre::Context.new do
@ -59,11 +69,5 @@ module Hollandaise
end end
default_task :sauce default_task :sauce
no_tasks do
def dir
Pathname("screenshots")
end
end
end end
end end

View File

@ -0,0 +1,45 @@
require 'uri'
module Hollandaise
class Project
def initialize(project_name)
@project_name = project_name
end
def root(root)
@root = URI(root)
end
def screenshot(name, uri)
@screenshots ||= []
@screenshots << [ name, uri ]
end
def browsers(*browsers)
@browsers = browsers
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)
end
end
end
end
private
def urls
@screenshots.collect do |name, uri|
[ @root.merge(URI(uri)), name ]
end
end
def browser_objects
@browser_objects ||= Hollandaise::Browsers.for(@browsers)
end
end
end

9
skel/hollandaise.rb.tt Normal file
View File

@ -0,0 +1,9 @@
Hollandaise.project "<%= @name %>" do |p|
p.root 'http://my.site/'
p.screenshot 'Home', '/'
p.screenshot 'Archive', '/archive'
p.browsers :chrome
end