Skip to content

Instantly share code, notes, and snippets.

@Raven24
Created Sep 14, 2012
Embed
What would you like to do?
website screenshot and comparison generator
source 'http://rubygems.org'
gem 'selenium-webdriver'
gem 'rmagick'
require 'rubygems'
require 'logger'
require 'selenium-webdriver'
require 'RMagick'
module Rifleman
module Configurable
# config options
attr_accessor :standalone, :image_path, :delay, :logger, :driver
def configure
yield self
self
end
def logger
@logger ||= Logger.new(STDOUT)
end
def driver
@driver ||= Selenium::WebDriver.for(:firefox)
end
end
module Photographer
# take screenshots for each of the requested sites
def shoot!
self.class.logger.info ":: taking screenshots ..."
self.class.driver.manage.window.resize_to(1280, 1024)
@sites.each do |site, comp_pair|
self.class.logger.debug "::~ #{site.to_s}"
comp_pair.each_with_index do |address, i|
self.class.driver.navigate.to address
self.class.driver.save_screenshot self.class.image_path + "#{i}_#{site.to_s}.png"
end
end
ensure
self.class.driver.quit if self.class.standalone==true
end
end
module Compositor
def flicker!
self.class.logger.info ":: generating flicker images ..."
@sites.each do |site, pair|
self.class.logger.debug "::~ #{site.to_s}"
f0 = self.class.image_path + "0_#{site.to_s}.png"
f1 = self.class.image_path + "1_#{site.to_s}.png"
list = Magick::ImageList.new f0, f1
list.delay = self.class.delay
list.iterations = 0
list.write self.class.image_path + "#{site}_flicker.gif"
end
end
end
class SiteComparison
class << self
include Rifleman::Configurable
end
include Photographer
include Compositor
def initialize(sites={})
@sites = sites
end
def compare!
shoot!
flicker!
end
end
end
Rifleman::SiteComparison.configure do |c|
c.standalone = true # this decides if the driver should quit after
# the screenshots were taken
# (`true` closes the brower)
c.image_path = './shots/' # path for storing the screenshots and the
# generated images
c.delay = 60 # number of ticks between each image in the flicker
# (100 ticks/second)
#c.logger = Logger.new(STDOUT)
#c.driver = Selenium::WebDriver.for(:firefox)
end
sites = {
:wikipedia => [ 'http://de.wikipedia.org', 'http://en.wikipedia.org' ],
:twitter => [ 'https://twitter.com/donttrythis', 'https://twitter.com/JamieNoTweet' ]
#:diaspora => [ 'https://joindiaspora.com', 'https://pod.fulll.name' ]
#:google => [ 'http://www.google.at', 'http://www.google.com' ]
}
shooter = Rifleman::SiteComparison.new sites
shooter.compare!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment