forked from datuhealth/stranger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstranger.js
More file actions
284 lines (237 loc) · 7.8 KB
/
stranger.js
File metadata and controls
284 lines (237 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var del = require('del')
var gm = require('gm')
var webdriver = require('selenium-webdriver')
module.exports = Stranger
/**
* Stranger - The entry point for the tool. Compare a set of images to previously generated set.
* @example
* stranger(mobileConfig, false)
* @param {object} options - A javascript object configuring the output
* @param {boolean} [generate] - Whether or not to generate the base set of images. Defaults to false
* @param {function} [callback] - A function to run after stranger has run
* @returns {void}
*/
function Stranger (options, generate, callback) {
var self = this
if (!options) {
throw new Error('You need to pass a configuration object')
}
if (!(self instanceof Stranger)) {
return new Stranger(options, generate, callback)
}
if (generate) {
self.generateImages = true
}
self.config = options
self.callback = callback
self.setupSystem()
self.config.tests.forEach(function (test, idx) {
var filename = self.createFilename(test)
if (!test.url) {
return
}
self.createScreenshot(test.url, self.generateImages ? self.config.baseDir : self.config.compareDir, filename, test.local)
})
self.driver.quit().then(function (err) {
if (err) {
throw new Error(err)
}
// Report the number of images generated if the generate flag was used
if (self.generateImages) {
self.callback({
imagesProcessed: 0,
imagesGenerated: self.config.tests.length,
imagesDir: self.config.baseDir
})
}
// Otherwise, compare the images
if (!self.generateImages) {
del(self.config.diffDir + '**/*').then(function () {
self.compareImages()
}).catch(function (err) {
throw new Error(err)
})
}
})
}
/**
* setupSystem - Run a bunchf of things, probably syncronously, to setup folders, defaults, etc.
* @example
* this.setupSystem()
* @private
* @returns {void}
*/
Stranger.prototype.setupSystem = function () {
var self = this
self.checkConfig()
if (typeof self.generateImages === 'undefined') {
self.generateImages = false
}
self.diffCount = 0
self.noMatchCount = 0
self.imagesProcessed = 0
self.driver = new webdriver.Builder()
.forBrowser(self.config.browser || 'firefox')
.setChromeOptions(self.config.chromeOptions)
.setFirefoxOptions(self.config.firefoxOptions)
.setIeOptions(self.config.ieOptions)
.setOperaOptions(self.config.operaOptions)
.setSafariOptions(self.config.safariOptions)
.build()
// Create the folder for master images
mkdirp.sync(self.config.baseDir)
// Create the folder for the comparison images
mkdirp.sync(self.config.compareDir)
// Create the folder for the diff images
mkdirp.sync(self.config.diffDir)
// Check if master images have been generated and error if none have been created
// TODO - Think about just running generate quickly then continuing instead of erroring
if (!self.checkForFiles(self.config.baseDir) && !self.generateImages) {
throw new Error('\n× You don\'t have any reference images created yet.\nRerun stranger with the --generate flag')
}
// Remove any previously generated images
del.sync((self.generateImages ? self.config.baseDir : self.config.compareDir) + '**/*')
self.driver.manage().window().setSize(self.config.browserOptions.width, self.config.browserOptions.height)
}
/**
* checkConfig - Check a couple of properties in the config to set us up the urls.
* @example
* this.checkConfig()
* @private
* @returns {void}
*/
Stranger.prototype.checkConfig = function () {
var self = this
if (!self.config.browserOptions) {
self.config.browserOptions = {
width: 1024,
height: 768
}
}
if (!self.config.browserOptions.width) {
self.config.browserOptions.width = 1024
}
if (!self.config.browserOptions.height) {
self.config.browserOptions.height = 768
}
if (self.config.baseDir.charAt(self.config.baseDir.length - 1) !== '/') {
self.config.baseDir = self.config.baseDir + '/'
}
if (self.config.compareDir.charAt(self.config.compareDir.length - 1) !== '/') {
self.config.compareDir = self.config.compareDir + '/'
}
if (self.config.diffDir.charAt(self.config.diffDir.length - 1) !== '/') {
self.config.diffDir = self.config.diffDir + '/'
}
}
/**
* createScreenshot - Given some basic info, open the url and take a screenshot.
* @example
* this.createScreenshot('localhost/home', './test/export', 'home.png')
* @private
* @param {string} url - The url that the browser should be pointed to
* @param {string} path - The directory path that the image should be saved to
* @param {string} filename - The filename for the screenshot
* @returns {void}
*/
Stranger.prototype.createScreenshot = function (url, imgPath, filename, local) {
var self = this
var screenshot
if (local) {
url = 'file://' + path.join(__dirname, url)
}
self.driver.get(url)
screenshot = self.driver.takeScreenshot()
screenshot.then(function (png) {
// Convert it to a buffer right quick
var buf = new Buffer(png, 'base64')
fs.writeFileSync(imgPath + filename, buf)
}, function (err) {
throw new Error(err)
})
}
/**
* chechForFiles - Quickly check if a directory is empty or not
* @example
* this.checkForFiles('./test/export')
* @private
* @param {string} path - The directory path to check
* @returns {boolean} - True if there are files, false if the directory is empty
*/
Stranger.prototype.checkForFiles = function (path) {
var files = fs.readdirSync(path)
if (files && files.length) {
return true
} else {
return false
}
}
/**
* createFilename - Given a test object, cerate a filename based on the name given or the url
* @example
* this.createFilename({ url: 'localhost/home', name: 'home' })
* @private
* @param {object} test - The test object. Needs to contain the url. The name is optional.
* @returns {string} - The best guess for a filename. Doesn't guaruntee uniqueness.
*/
Stranger.prototype.createFilename = function (test) {
var filename = test.name || test.url.split('/')[test.url.split('/').length - 1] || 'index'
return filename + '.png'
}
/**
* compareImages - Begin the process of comparing a set of images to another.
* @example
* this.compareImages()
* @private
* @returns {void}
*/
Stranger.prototype.compareImages = function () {
var self = this
var mismatchedImages = []
var diffedImages = []
var similarImages = []
self.config.tests.forEach(function (test, idx) {
var filename = self.createFilename(test)
fs.readFile(self.config.baseDir + filename, function (err, buf) {
if (err) {
self.imagesProcessed++
mismatchedImages.push(filename)
return self.noMatchCount++
}
gm.compare(self.config.compareDir + filename, self.config.baseDir + filename, {
file: self.config.diffDir + filename,
highlightColor: 'red',
tolerance: 0.0,
highlightStyle: 'assign'
}, function (err, imagesAreSame) {
if (err) {
throw new Error(err)
}
if (!imagesAreSame) {
self.diffCount++
diffedImages.push(filename)
} else {
fs.unlink(self.config.diffDir + filename)
similarImages.push(filename)
}
self.imagesProcessed++
if (self.imagesProcessed === self.config.tests.length) {
self.callback({
imagesGenerated: 0,
imagesProcessed: self.imagesProcessed,
imagesDir: self.config.compareDir,
noMatchCount: self.noMatchCount,
similarImages: similarImages,
diffedImages: diffedImages,
mismatchedImages: mismatchedImages,
diffCount: self.diffCount,
diffDir: self.config.diffDir
})
}
})
})
})
}