cleaning things up a lot

This commit is contained in:
John Bintz 2012-08-15 11:30:23 -04:00
parent 31aad8d7a2
commit 608f033c5e
8 changed files with 212 additions and 155 deletions

View File

@ -1,168 +1,17 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'thor' require 'thor'
require 'sauce'
require 'thread'
require 'builder' require 'builder'
module Hollandaise $: << File.expand_path('../../lib')
class << self
attr_accessor :url, :browsers
def configure require 'hollandaise'
yield self require 'hollandaise/cli'
end
end
module Browsers
class << self
def for(browsers)
browsers.collect { |browser| self.send(browser) }
end
def ie7
[ :sauce, 'iexplore', '7', 'Windows 2003' ]
end
def ie8
[ :sauce, 'iexplore', '8', 'Windows 2003' ]
end
def ie9
[ :sauce, 'iexplore', '9', 'Windows 2008' ]
end
def ffwin10
[ :sauce, 'firefox', '10', 'Windows 2008' ]
end
def chromewin
[ :sauce, 'chrome', '', 'Windows 2008' ]
end
def firefox
[ :selenium, :firefox ]
end
def chrome
[ :selenium, :chrome ]
end
end
end
end
begin begin
load File.join(Dir.pwd, 'hollandaise.rb') load File.join(Dir.pwd, 'hollandaise.rb')
rescue LoadError => e rescue LoadError => e
end end
class Hollandaise::CLI < Thor
desc "sauce URL BROWSER BROWSER...", "Take screenshots of a URL on Sauce Labs"
method_options :delay => 0
def sauce(*browsers)
if browsers.first && browsers.first[%r{^http}]
url = browsers.shift
else
url = Hollandaise.url
end
if Hollandaise.browsers && browsers.empty?
browsers = Hollandaise.browsers
end
threads = []
FileUtils.rm_rf(dir) if File.directory?(dir)
FileUtils.mkdir_p(dir)
mutex = Mutex.new
Hollandaise::Browsers.for(browsers).each do |browser|
info = { :browser_url => url, :job_name => "#{url}" }
[ :type, :browser, :browser_version, :os ].each_with_index { |key, index| info[key] = browser[index] }
thread = Thread.new do
selenium = nil
Thread.stop if Thread.current == self
begin
target = screenshot_target_for(Thread.current[:browser])
FileUtils.mkdir_p(File.dirname(target))
mutex.synchronize { puts "#{target}..." }
selenium = case Thread.current[:browser].first
when :sauce
require 'sauce/selenium'
Sauce::Selenium2.new(Thread.current[:info])
when :selenium
Selenium::WebDriver.for Thread.current[:browser].last
end
selenium.navigate.to url
sleep options[:delay].to_i
selenium.execute_script %{window.resizeTo(1280, 1024)}
selenium.save_screenshot(target)
mutex.synchronize { puts "#{target} done!" }
ensure
selenium.quit if selenium
end
end
thread[:info] = info
thread[:browser] = browser
thread.run
threads << thread
end
threads.each(&:join)
html = Builder::XmlMarkup.new
html.html {
html.head {
html.title { "Sauce Labs screenshots for #{url}" }
}
html.body {
html.table {
html.tr {
browsers.each { |browser| html.th(browser) }
}
html.tr {
Hollandaise::Browsers.for(browsers).each { |browser|
html.td(:valign => 'top') {
html.img(:src => screenshot_target_for(browser))
}
}
}
}
}
}
File.open('index.html', 'wb') { |fh| fh.print html.to_s }
end
default_task :sauce
no_tasks do
def dir
"screenshots"
end
def screenshot_target_for(browser)
case browser.first
when :sauce
File.join(dir, browser[3], "#{browser[1]} #{browser[2]}.png")
when :selenium
File.join(dir, "#{browser[1]}.png")
end
end
end
end
Hollandaise::CLI.start Hollandaise::CLI.start

View File

@ -1,5 +1,14 @@
require "hollandaise/version" require "hollandaise/version"
require 'hollandaise/browsers'
require 'hollandaise/browser'
module Hollandaise module Hollandaise
# Your code goes here... class << self
attr_accessor :url, :browsers
end
def self.configure
yield self
end
end end

View File

@ -0,0 +1,19 @@
require 'hollandaise/browser/base'
require 'hollandaise/browser/sauce'
require 'hollandaise/browser/selenium'
module Hollandaise
module Browser
def self.for(browser, options)
browser = browser.dup
case browser.shift
when :sauce
Hollandaise::Browser::Sauce.new(*browser, options)
when :selenium
Hollandaise::Browser::Selenium.new(*browser, options)
end
end
end
end

View File

@ -0,0 +1,27 @@
module Hollandaise
module Browser
class Base
def run(url)
@url = url
selenium.navigate.to url
sleep @options[:delay].to_i
selenium.execute_script %{window.resizeTo(1280, 1024)}
end
def take_screenshot(target_dir)
target = target_for(target_dir)
target.parent.mkpath
selenium.save_screenshot(target.to_s)
end
def close
selenium.quit
@selenium = nil
end
end
end
end

View File

@ -0,0 +1,31 @@
module Hollandaise
module Browser
class Sauce < Base
def initialize(browser, version, os, options)
@browser, @version, @os, @options = browser, version, os, options
end
def selenium
require 'sauce'
require 'sauce/selenium'
@selenium ||= ::Sauce::Selenium2.new(info)
end
def target_for(dir)
dir.join(@os.to_s).join("#{@browser} #{@version}.png")
end
def info
{
:browser => @browser,
:browser_version => @version,
:os => @os,
:browser_url => @url,
:job_name => "#{@url}"
}
end
end
end
end

View File

@ -0,0 +1,20 @@
module Hollandaise
module Browser
class Selenium < Base
def initialize(browser, options)
@browser, @options = browser, options
end
def selenium
require 'selenium/webdriver'
@selenium ||= ::Selenium::WebDriver.for(@browser)
end
def target_for(dir)
dir.join("#{@browser}.png")
end
end
end
end

View File

@ -0,0 +1,39 @@
module Hollandaise
module Browsers
def self.for(browsers, options)
browsers.collect { |browser| Hollandaise::Browser.for(self.send(browser), options) }
end
def self.each(browsers, options, &block)
self.for(browsers, options).each(&block)
end
def self.ie7
[ :sauce, 'iexplore', '7', 'Windows 2003' ]
end
def self.ie8
[ :sauce, 'iexplore', '8', 'Windows 2003' ]
end
def self.ie9
[ :sauce, 'iexplore', '9', 'Windows 2008' ]
end
def self.ffwin10
[ :sauce, 'firefox', '10', 'Windows 2008' ]
end
def self.chromewin
[ :sauce, 'chrome', '', 'Windows 2008' ]
end
def self.firefox
[ :selenium, :firefox ]
end
def self.chrome
[ :selenium, :chrome ]
end
end
end

63
lib/hollandaise/cli.rb Normal file
View File

@ -0,0 +1,63 @@
module Hollandaise
class CLI < Thor
desc "sauce URL BROWSER BROWSER...", "Take screenshots of a URL on Sauce Labs"
method_options :delay => 0
def sauce(*browsers)
if browsers.first && browsers.first[%r{^http}]
url = browsers.shift
else
url = Hollandaise.url
end
if Hollandaise.browsers && browsers.empty?
browsers = Hollandaise.browsers
end
Hollandaise::Browsers.each(browsers, options) do |browser|
begin
browser.run(url)
browser.take_screenshot(dir)
ensure
browser.close
end
end
if false
html = Builder::XmlMarkup.new
html.html {
html.head {
html.title { "Sauce Labs screenshots for #{url}" }
}
html.body {
html.table {
html.tr {
browsers.each { |browser| html.th(browser) }
}
html.tr {
Hollandaise::Browsers.for(browsers).each { |browser|
html.td(:valign => 'top') {
html.img(:src => screenshot_target_for(browser))
}
}
}
}
}
}
File.open('index.html', 'wb') { |fh| fh.print html.to_s }
end
end
default_task :sauce
no_tasks do
def dir
Pathname("screenshots")
end
end
end
end