public
Created

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.

  • Download Gist
selenium.rake
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.