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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
/*!
* Piwik - free/libre analytics platform
*
* chai assertion extensions
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
var fs = require('fs'),
PageRenderer = require('./page-renderer.js').PageRenderer,
AssertionError = chai.AssertionError;
// add screenshot keyword to `expect`
expect.screenshot = function (file, prefix) {
if (!prefix) {
prefix = app.runner.suite.title; // note: runner is made global by run-tests.js
}
return chai.expect(prefix + '_' + file);
};
// add page keyword to `expect`
expect.page = function (url) {
return chai.expect(url);
};
// add file keyword to `expect`
expect.file = expect.screenshot;
expect.current_page = expect.page(null);
function getPageLogsString(pageLogs, indent) {
var result = "";
if (pageLogs.length) {
result = "\n\n" + indent + "Rendering logs:\n";
pageLogs.forEach(function (message) {
result += indent + " " + message.replace(/\n/g, "\n" + indent + " ") + "\n";
});
result = result.substring(0, result.length - 1);
}
return result;
}
// add capture assertion
var pageRenderer = new PageRenderer(config.piwikUrl + path.join("tests", "PHPUnit", "proxy"));
function getExpectedFilePath(fileName) {
var expectedScreenshotDir = path.join(app.runner.suite.baseDirectory, config.expectedScreenshotsDir);
fileName = assumeFileIsImageIfNotSpecified(fileName);
return path.join(expectedScreenshotDir, fileName);
}
function getProcessedFilePath(fileName) {
var pathToUITests = options['store-in-ui-tests-repo'] ? uiTestsDir : app.runner.suite.baseDirectory;
var processedScreenshotDir = path.join(pathToUITests, config.processedScreenshotsDir);
if (!fs.isDirectory(processedScreenshotDir)) {
fs.makeTree(processedScreenshotDir);
}
fileName = assumeFileIsImageIfNotSpecified(fileName);
return path.join(processedScreenshotDir, fileName);
}
function assumeFileIsImageIfNotSpecified(filename) {
if(!endsWith(filename, '.png') && !endsWith(filename, '.txt') ) {
return filename + '.png';
}
return filename;
}
function endsWith(string, needle)
{
return string.substr(-1 * needle.length, needle.length) === needle;
}
function failCapture(fileTypeString, pageRenderer, testInfo, expectedFilePath, processedFilePath, message, done) {
app.diffViewerGenerator.failures.push(testInfo);
var expectedPath = testInfo.expected ? path.resolve(testInfo.expected) :
(expectedFilePath + " (not found)"),
processedPath = testInfo.processed ? path.resolve(testInfo.processed) :
(processedFilePath + " (not found)");
var indent = " ";
var failureInfo = message + "\n";
failureInfo += indent + "Url to reproduce: " + pageRenderer.getCurrentUrl() + "\n";
failureInfo += indent + "Generated " + fileTypeString + ": " + processedPath + "\n";
failureInfo += indent + "Expected " + fileTypeString + ": " + expectedPath + "\n";
failureInfo += getPageLogsString(pageRenderer.pageLogs, indent);
error = new AssertionError(message);
// stack traces are useless so we avoid the clutter w/ this
error.stack = failureInfo;
done(error);
}
function getScreenshotDiffDir() {
return path.join(options['store-in-ui-tests-repo'] ? uiTestsDir : app.runner.suite.baseDirectory, config.screenshotDiffDir);
}
function capture(screenName, compareAgainst, selector, pageSetupFn, comparisonThreshold, done) {
if (!(done instanceof Function)) {
throw new Error("No 'done' callback specified in capture assertion.");
}
screenName = assumeFileIsImageIfNotSpecified(screenName);
compareAgainst = assumeFileIsImageIfNotSpecified(compareAgainst);
var expectedScreenshotPath = getExpectedFilePath(compareAgainst),
processedScreenshotPath = getProcessedFilePath(screenName),
screenshotDiffDir = getScreenshotDiffDir();
if (!fs.isDirectory(screenshotDiffDir)) {
fs.makeTree(screenshotDiffDir);
}
pageSetupFn(pageRenderer);
try {
pageRenderer.capture(processedScreenshotPath, function (err) {
if (err) {
var indent = " ";
err.stack = err.message + "\n" + indent + getPageLogsString(pageRenderer.pageLogs, indent);
done(err);
return;
}
var testInfo = {
name: screenName,
processed: fs.isFile(processedScreenshotPath) ? processedScreenshotPath : null,
expected: fs.isFile(expectedScreenshotPath) ? expectedScreenshotPath : null,
baseDirectory: app.runner.suite.baseDirectory
};
var fail = function (message) {
failCapture("screenshot", pageRenderer, testInfo, expectedScreenshotPath, processedScreenshotPath, message, done);
};
var pass = function () {
if (options['print-logs']) {
console.log(getPageLogsString(pageRenderer.pageLogs, " "));
}
done();
};
if (!testInfo.processed) {
fail("Failed to generate screenshot to " + screenName + ".");
return;
}
if (!testInfo.expected) {
app.appendMissingExpected(screenName);
fail("No expected screenshot found for " + screenName + ".");
return;
}
function screenshotMatches(misMatchPercentage) {
if (comparisonThreshold) {
return misMatchPercentage <= 100 * (1 - comparisonThreshold);
} else {
return misMatchPercentage == 0;
}
}
function compareImages(expected, processed)
{
var args = ["-metric", "AE", expected, processed, 'null:'];
var child = require('child_process').spawn('compare', args);
var testFailure = '';
function onCommandResponse (numPxDifference) {
// on success we get numPxDifference = '0' meaning no pixel was different
// on different images we get the number of different pixels
// on any error we get an error message (eg image size different)
numPxDifference = numPxDifference.trim();
if (numPxDifference && numPxDifference !== '0') {
if (/^(\d+)$/.test(numPxDifference)) {
testFailure += "(" + numPxDifference + "px difference";
} else {
testFailure += "(image magick error: " + numPxDifference;
}
testFailure += ")\n";
}
}
child.stdout.on("data", onCommandResponse);
child.stderr.on("data", onCommandResponse);
child.on("exit", function (code) {
if (testFailure) {
testFailure = 'Processed screenshot does not match expected for ' + screenName + ' ' + testFailure;
testFailure += 'TestEnvironment was ' + JSON.stringify(testEnvironment);
}
if (code == 0 && !testFailure) {
pass();
} else if (comparisonThreshold) {
// we use image magick only for exact match comparison, if there is a threshold we now check if this one fails
resemble("file://" + processedScreenshotPath).compareTo("file://" + expectedScreenshotPath).onComplete(function(data) {
if (!screenshotMatches(data.misMatchPercentage)) {
fail(testFailure + ". (mismatch = " + data.misMatchPercentage + ")");
return;
}
pass();
});
} else {
fail(testFailure);
}
});
}
compareImages(expectedScreenshotPath, processedScreenshotPath);
}, selector);
} catch (ex) {
var err = new Error(ex.message);
err.stack = ex.message;
done(err);
}
}
function compareContents(compareAgainst, pageSetupFn, done) {
if (!(done instanceof Function)) {
throw new Error("No 'done' callback specified in 'pageContents' assertion.");
}
compareAgainst = assumeFileIsImageIfNotSpecified(compareAgainst);
var screenshotDiffDir = getScreenshotDiffDir(),
processedFilePath = getProcessedFilePath(compareAgainst),
expectedFilePath = getExpectedFilePath(compareAgainst);
if (!fs.isDirectory(screenshotDiffDir)) {
fs.makeTree(screenshotDiffDir);
}
pageSetupFn(pageRenderer);
try {
pageRenderer.capture(processedFilePath, function (err) {
if (err) {
var indent = " ";
err.stack = err.message + "\n" + indent + getPageLogsString(pageRenderer.pageLogs, indent);
done(err);
return;
}
var fail = function (message) {
failCapture("file", pageRenderer, testInfo, expectedFilePath, processedFilePath, message, done);
};
var pass = function () {
if (options['print-logs']) {
console.log(getPageLogsString(pageRenderer.pageLogs, " "));
}
done();
};
var processed = pageRenderer.getPageContents();
fs.write(processedFilePath, processed);
var filename = processedFilePath.split(/[\\/]/).pop();
var testInfo = {
name: filename,
processed: fs.isFile(processedFilePath) ? processedFilePath : null,
expected: fs.isFile(expectedFilePath) ? expectedFilePath : null,
baseDirectory: app.runner.suite.baseDirectory
};
if (!fs.isFile(testInfo.expected)) {
fail("No expected output file found at " + testInfo.expected + ".");
return;
}
var expected = fs.read(testInfo.expected);
if (processed == expected) {
pass();
} else {
fail("Processed page contents does not equal expected file contents.");
}
});
} catch (ex) {
var err = new Error(ex.message);
err.stack = ex.message;
done(err);
}
}
chai.Assertion.addChainableMethod('captureSelector', function () {
var compareAgainst = this.__flags['object'];
if (arguments.length == 3) {
var screenName = compareAgainst,
selector = arguments[0],
pageSetupFn = arguments[1],
done = arguments[2];
} else {
var screenName = app.runner.suite.title + "_" + arguments[0],
selector = arguments[1],
pageSetupFn = arguments[2],
done = arguments[3];
}
var comparisonThreshold = this.__flags['comparisonThreshold'];
capture(screenName, compareAgainst, selector, pageSetupFn, comparisonThreshold, done);
});
chai.Assertion.addChainableMethod('capture', function () {
var compareAgainst = this.__flags['object'];
if (arguments.length == 2) {
var screenName = compareAgainst,
pageSetupFn = arguments[0],
done = arguments[1];
} else {
var screenName = app.runner.suite.title + "_" + arguments[0],
pageSetupFn = arguments[1],
done = arguments[2];
}
var comparisonThreshold = this.__flags['comparisonThreshold'];
capture(screenName, compareAgainst, null, pageSetupFn, comparisonThreshold, done);
});
// add `contains` assertion
chai.Assertion.addChainableMethod('contains', function () {
var self = this,
url = this.__flags['object']
;
if (arguments.length == 3) {
var elementSelector = arguments[0],
pageSetupFn = arguments[1],
screenName = null,
done = arguments[2];
} else {
var elementSelector = arguments[0],
screenName = app.runner.suite.title + "_" + arguments[1],
pageSetupFn = arguments[2],
done = arguments[3];
}
if (url !== null
&& url !== undefined
&& pageRenderer.getCurrentUrl() !== url
) {
pageRenderer.load(url);
}
pageSetupFn(pageRenderer);
if (!(done instanceof Function)) {
throw new Error("No 'done' callback specified in 'contains' assertion.");
}
var capturePath = screenName ? getProcessedFilePath(screenName) : null;
pageRenderer.capture(capturePath, function (err) {
var indent = " ";
if (err) {
err.stack = err.message + "\n" + indent + getPageLogsString(pageRenderer.pageLogs, indent);
done(err);
return;
}
try {
self.assert(
pageRenderer.contains(elementSelector),
"Expected page to contain element '" + elementSelector + "', but could not find it in page.",
"Expected page to not contain element '" + elementSelector + "', but found it in page."
);
done();
} catch (originalError) {
var stack = originalError.message + "\n\n";
if (capturePath) {
stack += indent + "View the captured screenshot at '" + capturePath + "'.";
} else {
stack += indent + "NOTE: No screenshot name was supplied to this '.contains(' assertion. If the second argument is a screenshot name, "
+ "the screenshot will be saved so you can debug this failure.";
}
stack += getPageLogsString(pageRenderer.pageLogs, indent);
var error = new AssertionError(originalError.message);
error.stack = stack;
done(error);
}
});
});
chai.Assertion.addChainableMethod('similar', function (comparisonThreshold) {
if (comparisonThreshold === null
|| comparisonThreshold === undefined
) {
throw new Error("No comparison threshold supplied to '.similar('!");
}
this.__flags['comparisonThreshold'] = comparisonThreshold;
});
// add pageContents assertion
chai.Assertion.addChainableMethod('pageContents', function (pageSetupFn, done) {
var compareAgainst = this.__flags['object'];
compareContents(compareAgainst, pageSetupFn, done);
});
|