mirror of
https://github.com/zhigang1992/viff.git
synced 2026-01-12 22:53:25 +08:00
refactor case images generator
This commit is contained in:
@@ -15,7 +15,6 @@ class Comparison
|
||||
fileData = _.values(@images)
|
||||
|
||||
Comparison.compare fileData[0], fileData[1], (diffObj) ->
|
||||
|
||||
if diffObj
|
||||
diffBase64 = diffObj.getImageDataUrl().replace('data:image/png;base64,', '')
|
||||
that.images.diff = new Buffer diffBase64, 'base64'
|
||||
@@ -30,8 +29,7 @@ class Comparison
|
||||
defer.promise()
|
||||
|
||||
@compare: (fileAData, fileBData, callback) ->
|
||||
defer = mr.Deferred()
|
||||
defer.done callback
|
||||
defer = mr.Deferred().done callback
|
||||
|
||||
resemble(fileAData).compareTo(fileBData).onComplete (data) ->
|
||||
defer.resolve data
|
||||
|
||||
@@ -19,11 +19,11 @@ module.exports = (viff) ->
|
||||
currentBrowserName = _case.browser
|
||||
console.log "#{currentBrowserName.info}"
|
||||
|
||||
caseName = Viff.getPathKey _case.url
|
||||
caseName = Viff.getCaseKey _case
|
||||
caseDuration += duration
|
||||
|
||||
if currentCaseName isnt caseName
|
||||
console.log "#{Viff.getPathKey _case.url} (#{durationFormat(duration)})"
|
||||
console.log "#{Viff.getCaseKey _case} (#{durationFormat(duration)})"
|
||||
currentCaseName = caseName
|
||||
caseDuration = 0
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ wrench = require 'wrench'
|
||||
|
||||
Viff = require './viff'
|
||||
|
||||
preprocessFolderName = (name) ->
|
||||
encodeURIComponent Viff.getPathKey name
|
||||
preprocessFolderName = (_case) ->
|
||||
encodeURIComponent Viff.getCaseKey _case
|
||||
|
||||
currentRunningDirname = process.cwd()
|
||||
screenshotPath = path.join currentRunningDirname, './screenshots'
|
||||
@@ -46,11 +46,11 @@ _.extend ImageGenerator,
|
||||
|
||||
generateByCase: (_case) ->
|
||||
browserFolderPath = path.join screenshotPath, _case.browser
|
||||
urlFolderPath = path.join browserFolderPath, preprocessFolderName(_case.url)
|
||||
urlFolderPath = path.join browserFolderPath, preprocessFolderName(_case)
|
||||
|
||||
ImageGenerator.createFolder browserFolderPath
|
||||
ImageGenerator.createFolder urlFolderPath
|
||||
|
||||
|
||||
_.each _case.result.images, (img, env) ->
|
||||
imagePath = path.join(urlFolderPath, env + '.png')
|
||||
ImageGenerator.createImageFile imagePath, img
|
||||
@@ -63,7 +63,7 @@ _.extend ImageGenerator,
|
||||
|
||||
_.each cases, (_case) ->
|
||||
if _case.result
|
||||
path = Viff.getPathKey _case.url
|
||||
path = Viff.getCaseKey _case
|
||||
compares[_case.browser] = compares[_case.browser] || {}
|
||||
compares[_case.browser][path] = _case.result
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ class Viff extends EventEmitter
|
||||
|
||||
return
|
||||
).addErrback (ex) ->
|
||||
console.error "ERROR: For path #{url} with selector #{selector||''}, #{ex.message.split('\n')[0]}"
|
||||
console.error "ERROR: For path #{util.inspect(url)} with selector #{selector||''}, #{ex.message.split('\n')[0]}"
|
||||
defer.resolve ''
|
||||
|
||||
defer.promise()
|
||||
@@ -64,14 +64,15 @@ class Viff extends EventEmitter
|
||||
_.each links, (url) ->
|
||||
_.each browsers, (browser) ->
|
||||
|
||||
if _.contains(browser, ':')
|
||||
[browserFrom, browserTo] = browser.split ':'
|
||||
if _.contains(browser, '-')
|
||||
[browserFrom, browserTo] = browser.split '-'
|
||||
|
||||
_.each envHosts, (host, envName) ->
|
||||
cases.push Viff.constructCase browser, browserFrom, browserTo, host, host, envName, envName, url
|
||||
else
|
||||
[[from, envFromHost], [to, envToHost]] = _.pairs envHosts
|
||||
cases.push Viff.constructCase browser, browser, browser, envFromHost, envToHost, from, to, url
|
||||
|
||||
cases
|
||||
|
||||
takeScreenshots: (browsers, envHosts, links, callback) ->
|
||||
@@ -84,9 +85,7 @@ class Viff extends EventEmitter
|
||||
mr.asynEach(cases, (_case) ->
|
||||
iterator = this
|
||||
|
||||
path = Viff.getPathKey _case.url
|
||||
startcase = Date.now()
|
||||
|
||||
that.takeScreenshot _case.from.browser, _case.from.host, _case.url, (fromImage, fromImgEx) ->
|
||||
that.takeScreenshot _case.to.browser, _case.to.host, _case.url, (toImage, toImgEx) ->
|
||||
|
||||
@@ -94,7 +93,7 @@ class Viff extends EventEmitter
|
||||
that.emit 'afterEach', _case, 0
|
||||
iterator.next()
|
||||
else
|
||||
imgWithEnvs = _.object [[_case.from.name, fromImage], [_case.to.name, toImage]]
|
||||
imgWithEnvs = _.object [[_case.from.browser + '-' + _case.from.name, fromImage], [_case.to.browser + '-' + _case.to.name, toImage]]
|
||||
comparison = new Comparison imgWithEnvs
|
||||
|
||||
comparison.diff (diffImg) ->
|
||||
@@ -104,7 +103,7 @@ class Viff extends EventEmitter
|
||||
iterator.next()
|
||||
, ->
|
||||
endTime = Date.now() - start
|
||||
that.drivers[browser].quit() for browser in browsers
|
||||
# that.drivers[browser].quit() for browser in browsers
|
||||
that.emit 'after', cases, endTime
|
||||
|
||||
defer.resolve cases, endTime
|
||||
@@ -120,6 +119,13 @@ class Viff extends EventEmitter
|
||||
path = "#{path} (#{selector})" if _.isString selector
|
||||
path
|
||||
|
||||
@getCaseKey: (_case) ->
|
||||
path = Viff.getPathKey _case.url
|
||||
if _case.from.name is _case.to.name
|
||||
path = _case.from.name + ':' + path
|
||||
|
||||
path
|
||||
|
||||
@dealWithPartial: (base64Img, driver, selector, callback) ->
|
||||
defer = mr.Deferred().done callback
|
||||
|
||||
|
||||
@@ -33,10 +33,14 @@ module.exports =
|
||||
c =
|
||||
browser: 'firefox'
|
||||
url: '/link1'
|
||||
fromname: 'build'
|
||||
toname: 'prod'
|
||||
from: { 'build': 'http://localhost:4000' }
|
||||
to: { 'prod': 'http://localhost:4001' }
|
||||
from:
|
||||
browser: 'firefox'
|
||||
name: 'build'
|
||||
host: 'http://localhost:4000'
|
||||
to:
|
||||
browser: 'firefox'
|
||||
name: 'prod'
|
||||
host: 'http://localhost:4001'
|
||||
result:
|
||||
images:
|
||||
build: 'ABCD'
|
||||
@@ -56,3 +60,34 @@ module.exports =
|
||||
test.ok @writeFileSync.firstCall.args[0].indexOf('/viff/screenshots/firefox/%2Flink1/build.png') >= 0
|
||||
test.done()
|
||||
|
||||
'it could generate images by case when comparing cross browsers': (test) ->
|
||||
c =
|
||||
browser: 'firefox:safari'
|
||||
url: '/link1'
|
||||
from:
|
||||
browser: 'firefox'
|
||||
name: 'build'
|
||||
host: 'http://localhost:4000'
|
||||
to:
|
||||
browser: 'safari'
|
||||
name: 'build'
|
||||
host: 'http://localhost:4000'
|
||||
result:
|
||||
images:
|
||||
'firefox-build': 'ABCD'
|
||||
'safari-build': 'EFGH'
|
||||
diff: 'IJKL'
|
||||
isSameDimensions: true
|
||||
misMatchPercentage: 0.2
|
||||
analysisTime: 2000
|
||||
|
||||
@existsSync = @existsSync.returns false
|
||||
|
||||
ImageGenerator.generateByCase c
|
||||
|
||||
test.ok @mkdirSync.firstCall.args[0].indexOf('/viff/screenshots/firefox:safari') >= 0
|
||||
test.ok @mkdirSync.secondCall.args[0].indexOf('/viff/screenshots/firefox:safari/build%3A%2Flink1') >= 0
|
||||
test.equals @writeFileSync.callCount, 3
|
||||
test.ok @writeFileSync.firstCall.args[0].indexOf('/viff/screenshots/firefox:safari/build%3A%2Flink1/firefox-build.png') >= 0
|
||||
test.done()
|
||||
|
||||
|
||||
@@ -238,7 +238,7 @@ module.exports =
|
||||
test.done()
|
||||
|
||||
'it should construct cases when set comparing cross browsers': (test) ->
|
||||
browsers = ['chrome:firefox', 'firefox'];
|
||||
browsers = ['chrome-firefox', 'firefox'];
|
||||
cases = Viff.constructCases(browsers, @config.compare, @links)
|
||||
cases.length.should.equal 6
|
||||
_.first(cases).from.name.should.equal 'build'
|
||||
|
||||
Reference in New Issue
Block a user