Skip to content

Instantly share code, notes, and snippets.

@nickdunn
Created May 30, 2017 13:28
Show Gist options
  • Save nickdunn/0ba4cb81326d3252e4b75f60c848777f to your computer and use it in GitHub Desktop.
Save nickdunn/0ba4cb81326d3252e4b75f60c848777f to your computer and use it in GitHub Desktop.
require 'bundler/setup'
require 'rake'
require 'dotenv/tasks'
require 'nokogiri'
require 'selenium-webdriver'
require 'spectre_client'
require 'open-uri'
WIDTHS = [1200, 768, 375]
BROWSER = :firefox
SPECTRE_DOMAIN = 'http://spectre.tools.fridayengineering.net'
SELENIUM_GRID_HUB = 'http://selenium.tools.fridayengineering.net:4444/wd/hub'
STDOUT.sync = true
def create_driver
if SELENIUM_GRID_HUB.empty?
@driver = Selenium::WebDriver.for BROWSER
else
# TODO: connect to grid
@driver = Selenium::WebDriver.for(:remote, url: SELENIUM_GRID_HUB, desired_capabilities: BROWSER)
end
@driver.manage.timeouts.implicit_wait = 120
@driver.manage.timeouts.script_timeout = 120
@driver.manage.timeouts.page_load = 120
end
def upload_screenshots_to_spectre
tests = [
{
name: 'Page 1',
key: 'page1',
href: 'http://...',
},
{
name: 'Page 2',
key: 'page2',
href: 'http://...',
}
]
uploads = []
#delete existing screenshots
puts "Deleting existing images..."
FileUtils.rm_rf("screenshots/.", secure: true)
puts "All existing images deleted."
puts "Creating Selenium driver..."
create_driver
tests.each do |test|
WIDTHS.each do |width|
t = test.clone
@driver.manage.window.resize_to(width, 1500)
puts "Navigating to #{t[:href]} @ #{width}px"
@driver.navigate.to t[:href]
# hide google map, causes antialiasing issues
@driver.execute_script("var map = document.getElementById('results__g-map'); if (map) map.style.visibility = 'hidden';")
# hide statistic components
@driver.execute_script("var statistics = document.getElementsByClassName('statistic__number'); for (var i=0; i < statistics.length; i++) statistics[i].style.display = 'none';")
image = @driver.screenshot_as(:png)
t[:image_filename] = "screenshots/#{t[:key]}_#{width}_#{BROWSER}.png"
t[:width] = width
File.open(t[:image_filename], 'w+') do |f|
f.write image
end
uploads << t
end
end
spectre_client = SpectreClient::Client.new('Client Name Here', 'Project Name Here', SPECTRE_DOMAIN)
failed_tests = []
uploads.each do |test|
puts "Uploading #{test[:name]} @ #{test[:width]}"
test_options = {
name: test[:name],
browser: BROWSER.to_s.capitalize,
size: "#{test[:width]}px",
source_url: test[:href],
screenshot: File.new(test[:image_filename], 'rb'),
fuzz_level: '35%'
}
result = spectre_client.submit_test(test_options)
puts result[:pass] ? 'Pass' : 'Fail'
puts "---"
if result[:pass] != true
failed_tests << result
end
end
unless failed_tests.empty?
puts "#{failed_tests.count} failed tests:"
failed_tests.each do |test|
puts "#{test[:name]} #{SPECTRE_DOMAIN}#{test[:url]}"
end
exit 1
end
end
task :upload_screenshots_to_spectre => :dotenv do
upload_screenshots_to_spectre
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment