Skip to content

Commit

Permalink
include file comparison and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Finn von Friesland committed Sep 16, 2015
1 parent 15fa841 commit 6c5c856
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 42 deletions.
16 changes: 9 additions & 7 deletions lib/controller/comparer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ class Comparer

attr_accessor :difference

def initialize(base, new, baseurl, urls, resolutions, screenshot_path)
def initialize(base, new, difference_path, baseurl, urls, resolutions, screenshot_path)
@base = base
@new = new
@baseurl = baseurl
@urls = urls
@resolutions = resolutions
@absolute_image_path = screenshot_path
@difference_path = difference_path
compare_images
end

Expand All @@ -20,20 +21,21 @@ def compare_images
self.difference = []
@urls.each do |page|
@resolutions.each do |width|
puts 'START'
base_name = Recorder.filename(@absolute_image_path, page, width, @base)
new_name = Recorder.filename(@absolute_image_path, page, width, @new)
puts base_name
puts new_name
images = PXDoppelganger::Images.new(
base_name,
new_name
)
puts images.difference
images.save_difference_image base_name
self.difference << images.difference
if images.difference > 1e-05 # for changes bigger than 1 per 10.000; otherwise we see mathematical artifacts
diff_name = Recorder.filename(@difference_path, page, width, 'DIFFERENCE')
images.save_difference_image diff_name
result = [base_name, new_name, diff_name, images.difference]
self.difference << result
end
end
end
difference
end

end
19 changes: 14 additions & 5 deletions lib/lineup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module Lineup

attr_accessor :difference

class Screenshot

def initialize(baseurl)
Expand All @@ -12,11 +14,17 @@ def initialize(baseurl)
resolutions('640, 800, 1180')
filepath_for_images("#{Dir.pwd}/screenshots")
use_headless(false)
difference_path("#{Dir.pwd}/screenshots")
end

def urls(urls)
raise "url for screenshot cannot be <empty string>" if urls == ''
raise_base_screenshots_taken('The urls')
@urls= urls.gsub(' ', '').split(",")
begin
@urls= urls.gsub(' ', '').split(",")
rescue NoMethodError
raise "urls must be in a "
end
end

def resolutions(resolutions)
Expand All @@ -41,12 +49,13 @@ def record_screenshot(version)
@got_base_screenshots = true
end

def compare(base, new)
Comparer.new(base, new, @baseurl, @urls, @resolutions, @screenshots_path)
def difference_path(path)
@difference_path = path
end

def difference

def compare(base, new)
comparer = Comparer.new(base, new, @difference_path, @baseurl, @urls, @resolutions, @screenshots_path)
comparer.difference
end

private
Expand Down
10 changes: 7 additions & 3 deletions lib/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ def url(base, url)

def name(page)
if page == '/'
name = 'storefront'
name = 'frontpage'
else #remove forward slash
name = page.gsub(/\//, "_")
name = page.gsub(/\//, "")
end
name
end

def clean(url)
'' if url == '/' #avoid two dashes at the end, e.g. www.otto.de//
if url == '/' #avoid two dashes at the end, e.g. www.otto.de//
''
else
url
end
end

end
61 changes: 34 additions & 27 deletions tests/rspec/lineup_spec.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
require 'rspec'
require 'dimensions'
require 'fileutils'
require_relative '../../lib/lineup'

describe '#screeshot_recorder' do

BASE_URL = 'https://www.otto.de'
SCREENSHOTS = "#{Dir.pwd}/screenshots/"

#after(:each) { FileUtils.rmtree SCREENSHOTS }

it 'opens a URL and takes mobile/tablet/desktop screenshots' do
lineup = Lineup::Screenshot.new('https://www.otto.de')
lineup = Lineup::Screenshot.new(BASE_URL)
lineup.record_screenshot('base')

expect(
File.exist? ("#{Dir.pwd}/screenshots/storefront_640_base.png")
File.exist? ("#{Dir.pwd}/screenshots/frontpage_640_base.png")
).to be(true)

expect(
File.exist? ("#{Dir.pwd}/screenshots/storefront_800_base.png")
File.exist? ("#{Dir.pwd}/screenshots/frontpage_800_base.png")
).to be(true)

expect(
File.exist? ("#{Dir.pwd}/screenshots/storefront_1180_base.png")
File.exist? ("#{Dir.pwd}/screenshots/frontpage_1180_base.png")
).to be(true)

end

it 'takes a screenshot a desired resolution' do
width = '320' #min width firefox as of Sep 2015
lineup = Lineup::Screenshot.new('https://www.otto.de')
lineup = Lineup::Screenshot.new(BASE_URL)
lineup.resolutions(width)
lineup.record_screenshot('base')
imagewidth = Dimensions.width("#{Dir.pwd}/screenshots/storefront_#{width}_base.png")
imagewidth = Dimensions.width("#{Dir.pwd}/screenshots/frontpage_#{width}_base.png")

expect(imagewidth).to be < (width.to_i + 10) #depending on the browser, there may be frames in the window. the screenshot represents the width of the viewport though

end

it 'takes screenshots of different pages, if specified' do
urls = '/,multimedia,sport'
lineup = Lineup::Screenshot.new('https://www.otto.de')
urls = '/, multimedia, sport'
lineup = Lineup::Screenshot.new(BASE_URL)
lineup.urls(urls)
lineup.resolutions('1180')
lineup.record_screenshot('base')

expect(
File.exist? ("#{Dir.pwd}/screenshots/storefront_1180_base.png")
File.exist? ("#{Dir.pwd}/screenshots/frontpage_1180_base.png")
).to be(true)

expect(
Expand All @@ -51,7 +57,7 @@
end

it 'raises and exception if, parameters are changed after the base screenshot' do
lineup = Lineup::Screenshot.new('https://www.otto.de')
lineup = Lineup::Screenshot.new(BASE_URL)
lineup.urls('/')
lineup.resolutions('400')
lineup.record_screenshot('base')
Expand All @@ -63,32 +69,33 @@
end

it 'compares a base and a new screenshot and detects no difference if images are the same' do
lineup = Lineup::Screenshot.new('https://www.otto.de')
lineup = Lineup::Screenshot.new(BASE_URL)
lineup.urls('/')
lineup.resolutions('400')
lineup.record_screenshot('base')
lineup.record_screenshot('new')
lineup.compare('base', 'new')
expect(
lineup.difference
).to be(nil)
lineup.compare('base', 'new')
).to eq([])

end

it 'compares a base and a new screenshot and returns the difference' do
#lineup = Lineup::Screenshot.new('https://www.otto.de')
#lineup.urls('/')
#lineup.resolutions('400')
#lineup.record_screenshot('base')
lineup = Lineup::Screenshot.new('https://www.otto.de')
lineup.urls('/')
lineup.resolutions('400')
#lineup.record_screenshot('new')
lineup.compare('base', 'new')
puts lineup.difference
it 'compares a base and a new screenshot and returns the difference if the images are NOT the same' do
width = '400'
base_site = '/index.html'
new_site = '/schwer.html'
lineup = Lineup::Screenshot.new(BASE_URL)
lineup.urls(base_site)
lineup.resolutions(width)
lineup.record_screenshot('base')
FileUtils.mv "#{Dir.pwd}/screenshots#{base_site}_#{width}_base.png", "#{Dir.pwd}/screenshots#{new_site}_#{width}_base.png"
lineup = Lineup::Screenshot.new(BASE_URL)
lineup.urls(new_site)
lineup.resolutions(width)
lineup.record_screenshot('new')
expect(
lineup.difference
).to be(nil)
lineup.compare('base', 'new')
).to eq([0])
end

end

0 comments on commit 6c5c856

Please sign in to comment.