Skip to content

Commit

Permalink
Add localStorage functionality and configuration with tests, upgrade …
Browse files Browse the repository at this point in the history
…to version 0.6.0
  • Loading branch information
StefanDenner committed Jul 5, 2016
1 parent 9020b74 commit 0b6e3ac
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 23 deletions.
26 changes: 17 additions & 9 deletions bin/lineup
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ require '../lib/lineup'

puts("This is an example. And a benchmark.")

lineup = Lineup::Screenshot.new('https://uhr.ptb.de')
lineup.wait_for_asynchron_pages(1)
lineup.use_phantomjs true
lineup = Lineup::Screenshot.new('https://prelive.otto.de')
lineup.urls("/,multimedia,moebel,damenmode,mittesten")
lineup.cookies([{"name"=>"trackingDisabled","value"=>"true","secure"=>false,"path"=>"/","domain"=>".otto.de"},
{"name"=>"survey","value"=>"1","secure"=>false,"path"=>"/","domain"=>".otto.de"}])
lineup.localStorage(["us_customerServiceWidget"=>"{'customerServiceWidgetNotificationHidden':{'value':true,'timestamp':1467723066080}}"])
lineup.resolutions("600,800,1200")
lineup.use_phantomjs false
lineup.wait_for_asynchron_pages(2)

puts("Taking base screenshots")
start = Time.now
puts("Taking screenshots")
lineup.record_screenshot('base')
screenshots = Time.now
sleep(2)
lineup.record_screenshot('nun')
screenshots2 = Time.now - 2

puts("Taking new screenshots")
lineup.record_screenshot('new')
screenshots2 = Time.now

puts("Starting comparison")
lineup.compare('base', 'nun')
lineup.compare('base', 'new')
lineup.save_json(".")
compare = Time.now - 2
compare = Time.now

s_t = screenshots - start
puts("Screenshots first run took #{s_t} seconds")
Expand Down
42 changes: 34 additions & 8 deletions lib/controller/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@

class Browser

def initialize(baseurl, urls, resolutions, path, headless, wait, cookies = [])
def initialize(baseurl, urls, resolutions, path, headless, wait, cookies = [], localStorage = [])
@absolute_image_path = path
FileUtils.mkdir_p @absolute_image_path
@baseurl = baseurl
@urls = urls
@resolutions = resolutions
@headless = headless
@wait = wait
@cookies = cookies

if cookies
@cookies = cookies
else
@cookies = []
end

if localStorage
@localStorage = localStorage
else
@localStorage = []
end
end

def record(version)
Expand Down Expand Up @@ -53,14 +64,29 @@ def screenshot_recorder(width, url, version)
@browser.driver.manage.window.resize_to(width, 1000)

url = Helper.url(@baseurl, url)
@browser.goto url
if @cookies.any?
@browser.cookies.clear
@cookies.each do |cookie|
@browser.cookies.add(cookie[:name], cookie[:value], domain: cookie[:domain], path: cookie[:path], expires: Time.now + 7200, secure: cookie[:secure])
end

if @cookies.any? || @localStorage.any?
# load url first before setting cookies and/or localStorage values
@browser.goto url

if @cookies.any?
@browser.cookies.clear
@cookies.each do |cookie|
@browser.cookies.add(cookie[:name], cookie[:value], domain: cookie[:domain], path: cookie[:path], expires: Time.now + 7200, secure: cookie[:secure])
end
end

if @localStorage.any?
@localStorage.each do |keyValue|
# Generate javascript for localStorage.setItem, escaping single quotes in key and value
stmt = "localStorage.setItem('" + keyValue.keys.first.gsub("'", "\\\\'") + "','" + keyValue.values.first.gsub("'", "\\\\'") + "')";
@browser.execute_script(stmt)
end
end
end

@browser.goto url

sleep @wait if @wait
@browser.screenshot.save( File.expand_path(filename))
end
Expand Down
35 changes: 33 additions & 2 deletions lib/lineup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,36 @@ def cookies(cookies)
end


def localStorage(localStorage)

# a hash for localStorage Key Value pair can be set here. this is optional.
#
# e.g {'key': 'value'}
#
# if it is not nil it has to be an array:

@localStorage = []
if localStorage
localStorage.each do |keyValue|
# generate :symbol => "value" hash map from "symbol" => "value"
#keyValue = keyValue.inject({}) { |element, (symbol, value)| element[symbol.to_sym] = value; element }

#Validation
(raise "LocalStorage Key Value pair must be a hash of format
{'key'=>'value'}
" unless keyValue.is_a? Hash)

raise "LocalStorage Key Value pair must have exactly one key" unless (keyValue.keys.length) == 1
raise "LocalStorage Key Value pair must have exactly one value" unless (keyValue.values.length) == 1
raise "LocalStorage Key Value pair must have a string as key" unless (keyValue.keys.first).is_a? String
raise "LocalStorage Key Value pair must have a string as value" unless (keyValue.values.first).is_a? String

@localStorage.push(keyValue)
end
end

end

def wait_for_asynchron_pages(wait)

# if required a wait time in seconds can be set. This may be needed, if after pageload some
Expand Down Expand Up @@ -265,6 +295,7 @@ def load_json_config(path)
wait_for_asynchron_pages(configuration["wait_for_asynchron_pages"])
cookie_for_experiment(configuration["cookie_for_experiment"])
cookies(configuration["cookies"])
localStorage(configuration["localStorage"])

if @cookies
cookies_merged= @cookies.inject([]) { |a, element| a << element.dup }
Expand All @@ -278,7 +309,7 @@ def load_json_config(path)
# the method calls set the variables for the parameters, we return an array with all of them.
# for the example above it is:
# [["/multimedia", "/sport"], [600, 800, 1200], "~/images/", true, "#/images/diff"]
[@urls, @resolutions, @screenshots_path, @headless, @difference_path, @wait_for_asynchron_pages, cookies_merged]
[@urls, @resolutions, @screenshots_path, @headless, @difference_path, @wait_for_asynchron_pages, cookies_merged, @localStorage]
end


Expand All @@ -303,7 +334,7 @@ def record_screenshot(version)
cookies_merged.push(@cookie_for_experiment)
end

browser = Browser.new(@baseurl, @urls, @resolutions, @screenshots_path, @headless, @wait_for_asynchron_pages, cookies_merged)
browser = Browser.new(@baseurl, @urls, @resolutions, @screenshots_path, @headless, @wait_for_asynchron_pages, cookies_merged, @localStorage)
# the only argument missing is if this is the "base" or "new" screenshot, this can be
# passed as an argument. The value does not need to be "base" or "new", but can be anything
browser.record(version)
Expand Down
4 changes: 2 additions & 2 deletions lib/lineup/version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Lineup
class Version
MAJOR = 0
MINOR = 5
PATCH = 2
MINOR = 6
PATCH = 0

class << self
def to_s
Expand Down
112 changes: 110 additions & 2 deletions tests/rspec/lineup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@

after(:each) { FileUtils.rmtree SCREENSHOTS }

it 'loads minimum configuration from a json file' do
# Given
file = "#{Dir.pwd}/test_configuration.json"
FileUtils.rm file if (File.exists? file)
json = '{"urls":"page1",
"resolutions": "13",
"filepath_for_images":"screenshots/path",
"use_phantomjs":true,
"difference_path":"screenshots/path/difference",
"wait_for_asynchron_pages":5
}'
save_json(json, file)

# When
lineup = Lineup::Screenshot.new(BASE_URL)

# Then
expect(
lineup.load_json_config(file)
).to eq([['page1'], [13], 'screenshots/path', true, 'screenshots/path/difference', 5, [], []])

# cleanup:
FileUtils.rm file if (File.exists? file)
end

it 'loads all configuration from a json file' do
# Given
file = "#{Dir.pwd}/test_configuration.json"
Expand All @@ -20,7 +45,29 @@
"filepath_for_images":"screenshots/path",
"use_phantomjs":true,
"difference_path":"screenshots/path/difference",
"wait_for_asynchron_pages":5
"wait_for_asynchron_pages":5,
"cookie_for_experiment":{
"name":"cookie1",
"value":"11111",
"domain":".google.de",
"path":"/",
"secure":false
},
"cookies" : [{
"name":"cookie2",
"value":"22222",
"domain":".google.de",
"path":"/",
"secure":false
},
{
"name":"cookie3",
"value":"33333",
"domain":".google.de",
"path":"/",
"secure":false
}],
"localStorage":[{"myKey1":"myValue1"},{"myKey2":"myValue2"}]
}'
save_json(json, file)

Expand All @@ -30,7 +77,11 @@
# Then
expect(
lineup.load_json_config(file)
).to eq([['page1', 'page2'], [13,42], 'screenshots/path', true, 'screenshots/path/difference', 5, []])
).to eq([['page1', 'page2'], [13,42], 'screenshots/path', true, 'screenshots/path/difference', 5,
[{:name=>"cookie2", :value=>"22222", :domain=>".google.de", :path=>"/", :secure=>false},
{:name=>"cookie3", :value=>"33333", :domain=>".google.de", :path=>"/", :secure=>false},
{:name=>"cookie1", :value=>"11111", :domain=>".google.de", :path=>"/", :secure=>false}],
[{"myKey1"=>"myValue1"}, {"myKey2"=>"myValue2"}]])

# cleanup:
FileUtils.rm file if (File.exists? file)
Expand Down Expand Up @@ -215,6 +266,63 @@

end

it 'takes a screenshot when loading a json config and setting local storage key value pair containing single quotes' do
# Given
file = "#{Dir.pwd}/test_configuration.json"
FileUtils.rm file if (File.exists? file)

json = '{"urls":"page1",
"resolutions":"200",
"filepath_for_images":"screenshots/path",
"use_phantomjs":true,
"difference_path":"screenshots/path/difference",
"wait_for_asynchron_pages":5,
"localStorage":[{"{\'mySpecialKey\'}":"{\'customerServiceWidgetNotificationHidden\':{\'value\':true,\'timestamp\':1467723066092}}"}]
}'
save_json(json, file)
lineup = Lineup::Screenshot.new(BASE_URL)
lineup.load_json_config(file)

lineup.record_screenshot('base')
# expect: no exception

# cleanup:
FileUtils.rm file if (File.exists? file)

end

it 'compares a base and a new screenshot when loading a json config and setting local storage key value pairs' do
# Given
file = "#{Dir.pwd}/test_configuration.json"
FileUtils.rm file if (File.exists? file)

json = '{"urls":"page1",
"resolutions":"200",
"filepath_for_images":"screenshots/path",
"use_phantomjs":true,
"difference_path":"screenshots/path/difference",
"wait_for_asynchron_pages":5,
"localStorage":[{"us_customerServiceWidget":"{\'customerServiceWidgetNotificationHidden\':{\'value\':true,\'timestamp\':1467723066092}}"},{"myKey2":"myValue2"}]
}'
save_json(json, file)
lineup = Lineup::Screenshot.new(BASE_URL)
lineup.load_json_config(file)

lineup.record_screenshot('base')
lineup.record_screenshot('new')

expect(
# When
lineup.compare('base', 'new')

# Then
).to eq([])

# cleanup:
FileUtils.rm file if (File.exists? file)

end

it 'compares a base and a new screenshot and returns the difference if the images are NOT the same as json log' do
# Given
width = '800'
Expand Down

0 comments on commit 0b6e3ac

Please sign in to comment.