-
-
Save lynaghk/0c73af18f95cfc530bed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Saucelabs | |
SAUCE_USERNAME = '' | |
SAUCE_TOKEN = '' | |
def self.setup_saucelabs_connection!(driver=nil) | |
WebMock.allow_net_connect! | |
driver ||= { | |
capability: :chrome, | |
platform: 'Windows 8', | |
version: '', | |
} | |
capabilities = | |
Selenium::WebDriver::Remote::Capabilities.send(driver[:capability]) | |
capabilities.platform = driver[:platform] | |
capabilities.version = driver[:version] | |
capabilities[:name] = name = [ driver[:platform], | |
driver[:capability].to_s.titleize, | |
driver[:version] ].join(' ') | |
client = Selenium::WebDriver::Remote::Http::Default.new | |
client.timeout = 120 | |
web_driver = Selenium::WebDriver.for :remote, | |
url: "http://#{ SAUCE_USERNAME }:#{ SAUCE_TOKEN }@ondemand.saucelabs.com/wd/hub", | |
desired_capabilities: capabilities, | |
http_client: client | |
fail "Couldn't create instance" if web_driver.nil? | |
web_driver | |
end | |
def self.filename_for_url(url, date=nil) | |
date ||= Date.today | |
date.strftime('%Y%m%d') + '-' + | |
url.gsub(%r[https?://], '').gsub(/[^0-9A-Za-z.\-]/, '-') + '.png' | |
end | |
end | |
require 'saucelabs' | |
DRIVERS = { | |
xpchrome: { | |
platform: 'Windows XP', | |
capability: :chrome, | |
version: '', | |
}, | |
xpfirefox: { | |
platform: 'Windows XP', | |
capability: :firefox, | |
version: '', | |
}, | |
xpie8: { | |
platform: 'Windows XP', | |
capability: :internet_explorer, | |
version: '8', | |
}, | |
win7ie10: { | |
platform: 'Windows 7', | |
capability: :internet_explorer, | |
version: '10', | |
}, | |
win7ie9: { | |
platform: 'Windows 7', | |
capability: :internet_explorer, | |
version: '9', | |
}, | |
win7chrome: { | |
platform: 'Windows 7', | |
capability: :chrome, | |
version: '27', | |
}, | |
win7firefox: { | |
platform: 'Windows 7', | |
capability: :firefox, | |
version: '25', | |
}, | |
win8ie10: { | |
platform: 'Windows 8', | |
capability: :internet_explorer, | |
version: '10', | |
}, | |
win8firefox: { | |
platform: 'Windows 8', | |
capability: :firefox, | |
version: '25', | |
}, | |
win8chrome: { | |
platform: 'Windows 8', | |
capability: :chrome, | |
version: '27', | |
}, | |
linuxchrome: { | |
platform: 'Linux', | |
capability: :chrome, | |
version: '28', | |
}, | |
linuxfirefox: { | |
platform: 'Linux', | |
capability: :firefox, | |
version: '24', | |
}, | |
osxchrome: { | |
platform: 'OS X 10.8', | |
capability: :chrome, | |
version: '27', | |
}, | |
osxsafari: { | |
platform: 'OS X 10.8', | |
capability: :safari, | |
version: '6', | |
}, | |
} | |
namespace :screenshot do | |
desc "TODO" | |
task all: :environment do | |
today = Date.today | |
finished_user_ids = DailyScreenshot.where(date: today).pluck(:user_id) | |
where = if finished_user_ids.present? | |
['id NOT IN (?)', finished_user_ids] | |
else | |
{} | |
end | |
url_count = User.where(where).count | |
Rails.logger.info "Processing #{ url_count } URLs, skipping "\ | |
"#{ finished_user_ids.count }" | |
User.where(where).each do |user| | |
@driver ||= Saucelabs.setup_saucelabs_connection!(DRIVERS[:linuxchrome]) | |
Rails.logger.info "Processing user_id #{ user.id }" | |
next if user.url.blank? | |
Rails.logger.info "Loading #{ user.url }" | |
@driver.navigate.to(user.url) | |
fullpath = File.join('tmp', Saucelabs.filename_for_url(user.url)) | |
@driver.save_screenshot(fullpath) | |
ds = DailyScreenshot.create! screenshot: File.open(fullpath), | |
date: today, | |
user_id: user.id | |
File.delete(fullpath) | |
begin | |
UserMailer.new_screenshot(ds).deliver if ds.user.receive_email | |
rescue => e | |
Rails.logger.info "Problem delivering daily_snapshot #{ ds.id } to "\ | |
"#{ ds.user.email }" | |
Rails.logger.info e.message | |
end | |
end | |
@driver.try(:quit) | |
end | |
desc 'test browser quality' | |
task quality: :environment do | |
url = 'http://www.dailysitesnap.com' | |
DRIVERS.each do |key, platform| | |
Rails.logger.info key.to_s | |
@driver = Saucelabs.setup_saucelabs_connection!(platform) | |
@driver.navigate.to(url) | |
fullpath = | |
File.join('tmp', "#{ key }-#{ Saucelabs.filename_for_url(url) }") | |
@driver.save_screenshot(fullpath) | |
@driver.quit | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment