Skip to content

@derwiki /selenium.rake
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Simple Rake task that will take screenshots of a pre-defined set of URLs in different OS/browser combinations. Relies on SauceLabs for all the hard work of setting up the different testing environments. No automated testing, but helpful to quickly identify visual regressions.
require 'rubygems'
require 'bundler'
require 'RMagick'
include Magick
require 'selenium-webdriver'
URLS = [
'www.apartmentlist.com',
'www.apartmentlist.com/oh/medina',
'www.apartmentlist.com/oh/medina?zip=44256#map/p13063',
]
# strip out characters from URLs unsuitable for filenames
FILE_STRIP_RE = /[\/ &=:#\?]+/
CUTOFF_HEIGHT = 1000
USERNAME = ''
TOKEN = ''
DRIVERS = [
{
capability: :chrome,
platform: 'Windows XP',
version: '',
},
{ # iOS 6
capability: :iphone,
platform: 'OS X 10.8',
version: '6',
},
{ # IE9
capability: :internet_explorer,
platform: 'Windows 7',
version: '9',
},
{ # Safari
capability: :safari,
platform: 'OS X 10.8',
version: '6',
},
{ # Windows Firefox
capability: :firefox,
platform: 'Windows XP',
version: '5',
},
{ # Windows IE8
capability: :internet_explorer,
platform: 'Windows XP',
version: '8',
},
{ # Android 4.0
capability: :android,
platform: 'Linux',
version: '4.0',
},
]
Rails.logger = Logger.new(STDOUT)
namespace :selenium do
desc "Capture screenshots of all browser/hardware combinations"
task smoke: :environment do
screenshot_filenames = []
timestamp = Time.now.strftime("%F-%H-%M-%S")
file_prefix = File.join([ 'tmp', timestamp ])
Rails.logger.info "Writing to #{ file_prefix}"
Dir.mkdir(file_prefix)
(ENV['debug'].present? ? [DRIVERS.first] : DRIVERS).each do |driver|
begin
caps = Selenium::WebDriver::Remote::Capabilities.send(driver[:capability])
caps.platform = driver[:platform]
caps.version = driver[:version]
caps[:name] = name = [ driver[:platform],
driver[:capability].to_s.titleize,
driver[:version] ].join(' ')
Rails.logger.info "Testing - #{ name }"
@driver = Selenium::WebDriver.for :remote,
url: "http://#{ USERNAME }:#{ TOKEN }@ondemand.saucelabs.com/wd/hub",
desired_capabilities: caps
(ENV['debug'].present? ? [URLS.first] : URLS).each do |url|
begin
print "#{ url }..."
@driver.navigate.to "http://#{ url }"
url_for_file = url.gsub(FILE_STRIP_RE, '-')
name_for_file = name.gsub(FILE_STRIP_RE, '-')
filename = "#{ name_for_file }-#{ url_for_file }.png"
screenshot_filenames << filename
filename_with_path = File.join([file_prefix, filename])
Thread.send(:sleep, 3)
@driver.save_screenshot(filename_with_path)
# crop and optimize
img = Magick::Image.read(filename_with_path).first
img.crop!(0, 0, img.columns, CUTOFF_HEIGHT)
img.write(filename_with_path) { self.quality = 60 }
puts 'OK'
rescue Exception => e
Rails.logger.warn e.message
Rails.logger.warn e.inspect
Rails.logger.info "Skipping #{ url }"
end
end
# teardown
@driver && @driver.quit
rescue Exception => e
Rails.logger.warn e.message
Rails.logger.warn e.inspect
Rails.logger.info "Skipping #{ driver[:platform] } #{ driver[:version] }"
end
end
index_filename = File.join([file_prefix, 'index.html'])
File.open(index_filename, 'w+') do |f|
f.write "<html><head><title>#{ timestamp }</title></head><body>"
screenshot_filenames.each do |filename|
f.write "<div>#{ filename }</div>"
f.write "<img src='#{ filename }' /><br />"
f.write "<hr>"
end
f.write "</body></html>"
end
Rails.logger.info "Wrote index to #{ index_filename }"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.