This repository was archived by the owner on Apr 18, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathphantomcss.js
More file actions
executable file
·386 lines (310 loc) · 9.51 KB
/
Copy pathphantomcss.js
File metadata and controls
executable file
·386 lines (310 loc) · 9.51 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
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
/*
Author: James Cryer
Company: Huddle
Last updated date: 06 Aug 2013
URL: https://github.com/Huddle/PhantomCSS
More: http://tldr.huddle.com/blog/css-testing/
*/
var fs = require('fs');
var _root = '.';
var _diffRoot = false;
var _count = 0;
var _realPath;
var _diffsToProcess = [];
var _libraryRoot = '.';
var exitStatus;
var _hideElements;
var _addLabelToFailedImage = true;
var _test_match;
var _test_exclude;
exports.screenshot = screenshot;
exports.compareAll = compareAll;
exports.compareMatched = compareMatched;
exports.init = init;
exports.update = init;
exports.turnOffAnimations = turnOffAnimations;
exports.getExitStatus = getExitStatus;
function init(options){
casper = options.casper || casper;
_libraryRoot = options.libraryRoot || _libraryRoot;
_root = options.screenshotRoot || _root;
_diffRoot = options.failedComparisonsRoot || _diffRoot;
_fileNameGetter = options.fileNameGetter || _fileNameGetter;
_onPass = options.onPass || _onPass;
_onFail = options.onFail || _onFail;
_onTimeout = options.onTimeout || _onTimeout;
_onComplete = options.onComplete || options.report || _onComplete;
_hideElements = options.hideElements;
if(options.addLabelToFailedImage !== undefined){
_addLabelToFailedImage = options.addLabelToFailedImage;
}
}
function turnOffAnimations(){
console.log('Turning off animations');
casper.evaluate(function turnOffAnimations(){
window.addEventListener('load', function(){
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = "* { -webkit-transition: none !important; transition: none !important; }";
document.body.appendChild(css);
if(jQuery){
$.fx.off = true;
}
},false);
});
}
function _fileNameGetter(root, fileName){
var name;
fileName = fileName || "screenshot";
name = root + fs.separator + fileName + "_" + _count++;
if(fs.isFile(name+'.png')){
return name+'.diff.png';
} else {
return name+'.png';
}
}
function screenshot(selector, timeToWait, hideSelector, fileName){
if(isNaN(Number(timeToWait)) && typeof timeToWait === 'string'){
fileName = timeToWait;
timeToWait = void 0;
}
casper.captureBase64('png'); // force pre-render
casper.wait(timeToWait || 250, function(){
if(hideSelector || _hideElements){
casper.evaluate(function(s1, s2){
if(s1){
$(s1).css('visibility', 'hidden');
}
$(s2).css('visibility', 'hidden');
}, {
s1: _hideElements,
s2: hideSelector
});
}
try{
casper.captureSelector( _fileNameGetter(_root, fileName) , selector);
}
catch(ex){
console.log("Screenshot FAILED: " + ex.message);
}
}); // give a bit of time for all the images appear
}
function asyncCompare(one, two, func){
if(!casper.evaluate(function(){ return window._imagediff_;})){
initClient();
}
casper.fill('form#image-diff', {
'one': one,
'two': two
});
casper.evaluate(function(filename){
window._imagediff_.run( filename );
}, {
label: _addLabelToFailedImage ? one : false
});
casper.waitFor(
function check() {
return this.evaluate(function(){
return window._imagediff_.hasResult;
});
},
function () {
var mismatch = casper.evaluate(function(){
return window._imagediff_.getResult();
});
if(Number(mismatch)){
func(false, mismatch);
} else {
func(true);
}
}, function(){
func(false);
},
10000
);
}
function getDiffs (path){
var filePath;
if(({'..':1,'.':1})[path]){ return true; }
if(_realPath){
_realPath += fs.separator + path;
} else {
_realPath = path;
}
filePath = _realPath;
if(fs.isDirectory(_realPath) ){
fs.list(_realPath).forEach(getDiffs);
} else {
if( /\.diff\./.test(path.toLowerCase()) ){
if(_test_match){
if( _test_match.test(_realPath.toLowerCase()) ){
if( !(_test_exclude && _test_exclude.test(_realPath.toLowerCase())) ){
console.log('Analysing', _realPath);
_diffsToProcess.push(filePath);
}
}
} else {
if( !(_test_exclude && _test_exclude.test(_realPath.toLowerCase())) ){
_diffsToProcess.push(filePath);
}
}
}
}
_realPath = _realPath.replace(fs.separator + path, '');
}
function compareMatched(match, exclude){
_test_match = typeof match === 'string' ? new RegExp(match) : match;
compareAll(exclude);
}
function compareAll(exclude){
var tests = [];
var fails = 0;
var errors = 0;
_test_exclude = typeof exclude === 'string' ? new RegExp(exclude) : exclude;
_realPath = undefined;
_diffsToProcess = [];
getDiffs(_root);
_diffsToProcess.forEach(function(file){
var baseFile = file.replace('.diff', '');
var test = {
filename: baseFile
};
if(!fs.isFile(baseFile)) {
test.error = true;
errors++;
tests.push(test);
} else {
casper.
thenOpen ( _libraryRoot+fs.separator+"resemblejscontainer.html" , function (){
asyncCompare(baseFile, file, function(isSame, mismatch){
if(!isSame){
test.fail = true;
fails++;
if(mismatch){
test.mismatch = mismatch;
_onFail(test);
} else {
_onTimeout(test);
}
casper.waitFor(
function check() {
return casper.evaluate(function(){
return window._imagediff_.hasImage;
});
},
function () {
var failFile, safeFileName, increment;
if(_diffRoot){
// flattened structure for failed diffs so that it is easier to preview
failFile = _diffRoot + fs.separator + file.split(fs.separator).pop().replace('.diff.png', '');
safeFileName = failFile;
increment = 0;
while ( fs.isFile(safeFileName+'.fail.png') ){
increment++;
safeFileName = failFile+'.'+increment;
}
failFile = safeFileName + '.fail.png';
casper.captureSelector(failFile, 'img');
}
casper.captureSelector(file.replace('.diff.png', '.fail.png'), 'img');
casper.evaluate(function(){
window._imagediff_.hasImage = false;
});
}, function(){},
10000
);
} else {
_onPass(test);
}
tests.push(test);
});
});
}
});
casper.then(function(){
casper.waitFor(function(){
return _diffsToProcess.length === tests.length;
}, function(){
_onComplete(tests, fails, errors);
}, function(){},
10000);
});
}
function initClient(){
casper.page.injectJs(_libraryRoot+fs.separator+'resemble.js');
casper.evaluate(function(){
var result;
var div = document.createElement('div');
// this is a bit of hack, need to get images into browser for analysis
div.style = "display:block;position:absolute;border:0;top:-1px;left:-1px;height:1px;width:1px;overflow:hidden;";
div.innerHTML = '<form id="image-diff">'+
'<input type="file" id="image-diff-one" name="one"/>'+
'<input type="file" id="image-diff-two" name="two"/>'+
'</form><div id="image-diff"></div>';
document.body.appendChild(div);
window._imagediff_ = {
hasResult: false,
hasImage: false,
run: run,
getResult: function(){
window._imagediff_.hasResult = false;
return result;
}
};
function run(label){
function render(data){
document.getElementById('image-diff').innerHTML = '<img src="'+data.getImageDataUrl(label)+'"/>';
window._imagediff_.hasImage = true;
}
resemble(document.getElementById('image-diff-one').files[0]).
compareTo(document.getElementById('image-diff-two').files[0]).
ignoreAntialiasing(). // <-- muy importante
onComplete(function(data){
var diffImage;
if(Number(data.misMatchPercentage) > 0.05){
result = data.misMatchPercentage;
} else {
result = false;
}
window._imagediff_.hasResult = true;
if(Number(data.misMatchPercentage) > 0.05){
render(data);
}
});
}
});
}
function _onPass(test){
// console.log('.'); just for progress indication
}
function _onFail(test){
console.log('FAILED: ('+test.mismatch+'% mismatch)', test.filename, '\n');
}
function _onTimeout(test){
console.log('TIMEOUT: ', test.filename, '\n');
}
function _onComplete(tests, noOfFails, noOfErrors){
if( tests.length === 0){
console.log("\nMust be your first time?");
console.log("Some screenshots have been generated in the directory " + _root);
console.log("This is your 'baseline', check the images manually. If they're wrong, delete the images.");
console.log("The next time you run these tests, new screenshots will be taken. These screenshots will be compared to the original.");
console.log('If they are different, PhantomCSS will report a failure.');
} else {
console.log("\nPhantomCSS found: " + tests.length + " tests.");
if(noOfFails === 0){
console.log("None of them failed. Which is good right?");
console.log("If you want to make them fail, go change some CSS - weirdo.");
} else {
console.log(noOfFails + ' of them failed.');
console.log('PhantomCSS has created some images that try to show the difference (in the directory '+_diffRoot+'). Fuchsia colored pixels indicate a difference betwen the new and old screenshots.');
}
if(noOfErrors !== 0){
console.log("There were " + noOfErrors + "errors. Is it possible that a baseline image was deleted but not the diff?");
}
exitStatus = noOfErrors+noOfFails;
}
}
function getExitStatus() {
return exitStatus;
}