forked from segmentio/niffy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
237 lines (187 loc) · 5.08 KB
/
index.js
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
/*jshint esnext:true*/
var debug = require('debug')('niffy');
var Nightmare = require('nightmare');
var mkdirp = require('mkdirp');
var fs = require('fs');
var defaults = require('defaults');
var sprintf = require('sprintf-js').sprintf;
var diffImages = require('./lib/diff');
var co = require('co');
/**
* Export `Niffy`
*/
module.exports = Niffy;
/**
* Initialize `Nightmare`
*
* @param {String} base
* @param {String} test
* @param {Object} options
*/
function Niffy(base, test, options) {
if (!(this instanceof Niffy)) return new Niffy(base, test, options);
options = defaults(options, { show: false, width: 1400, height: 1000, threshold: .2 });
this.queue = []
this.nightmare = new Nightmare(options);
this.basehost = base;
this.testhost = test;
this.starts = {};
this.profiles = {};
this.errorThreshold = options.threshold;
}
/**
* goto a specific path and optionally take some actions.
*
* @param {String} path
* @param {String} (Optional) If given, takes a screenshot of the given name
*/
Niffy.prototype.goto = function (path, name) {
this.queue.push({ type: 'goto', path: path });
if ( name ) {
this.queue.push({ type: 'screenshot', name: name })
}
return this;
};
function* runGoto (type, path) {
var destination = this[type+'host'] + path
this.startProfile('goto');
yield this.nightmare.goto(destination);
this.stopProfile('goto');
};
/**
* Convenience function to both navigate and screenshot.
*
* @param {String} name
* @param {Function} fn
*/
Niffy.prototype.capture = function (name, threshold, fn) {
if ( arguments.length === 2 ) {
fn = threshold
threshold = this.errorThreshold
}
this.queue.push({ type: 'navigate', fn: fn });
this.queue.push({ type: 'screenshot', name: name, threshold: threshold });
return this;
};
/**
* Navigate nightmare, without taking a screenshot.
*
* @param {Function} fn
*/
Niffy.prototype.navigate = function (fn) {
this.queue.push({ type: 'navigate', fn: fn });
return this;
};
function* runNavigate (type, fn) {
this.startProfile('navigate');
yield timeout(1000);
yield fn(this.nightmare, type);
yield timeout(2000);
this.stopProfile('navigate');
}
/**
* Screenshot and compare across base and test domains.
*
* @param {String} name
* @param {Number} (Optional) threshold
*/
Niffy.prototype.screenshot = function (name, threshold) {
this.queue.push({ type: 'screenshot', name: name, threshold: threshold || this.errorThreshold })
return this
}
function* runScreenshot (type, name, threshold) {
var pathBase = imgfilepath('base', name);
var pathTest = imgfilepath('test', name);
var pathDiff = imgfilepath('diff', name);
/**
* Screenshot
*/
this.startProfile('screenshot');
yield this.nightmare.wait(5000).screenshot( type === 'base' ? pathBase : pathTest )
this.stopProfile('screenshot');
yield timeout(250);
if ( type !== 'test' ) {
return
}
/**
* Diff
*/
this.startProfile('diff');
var diff = yield diffImages(pathBase, pathTest, pathDiff);
this.stopProfile('diff');
diff.percentage = diff.differences / diff.total * 100;
/**
* Compare
*/
var pct = '' + Math.floor(diff.percentage * 10000) / 10000 + '%';
var failMessage = sprintf('%s different (> %s threshold), open %s', pct, threshold+'%', pathDiff);
var absolutePct = Math.abs(diff.percentage);
debug('"%s" diffed %s (%s threshold)', name, diff.percentage+'%', threshold+'%');
if (diff.percentage > threshold) {
throw new Error(failMessage);
}
};
Niffy.prototype.execute = co.wrap(function * () {
var types = ['base', 'test'];
for (var t=0; t < 2; t++) {
for (var i=0; i < this.queue.length; i++) {
var action = this.queue[i];
if ( action.type === 'goto' ) {
yield runGoto.call(this, types[t], action.path);
}
else if ( action.type === 'navigate' ) {
yield runNavigate.call(this, types[t], action.fn);
}
else if ( action.type === 'screenshot' ) {
yield runScreenshot.call(this, types[t], action.name, action.threshold);
}
}
}
this.queue = []
})
/**
* End the session.
*/
Niffy.prototype.end = function* () {
yield this.nightmare.end();
debug(
'profile\n\tgoto %s\n\tscreenshot %s\n\tdiff %s',
this.profiles.goto,
this.profiles.screenshot,
this.profiles.diff
);
};
/**
* Mark an execution start time.
*
* @param {String} name
*/
Niffy.prototype.startProfile = function (name) {
var start = new Date().getTime();
this.starts[name] = start;
};
/**
* Mark an execution stop time.
*
* @param {String} name
*/
Niffy.prototype.stopProfile = function (name) {
var end = new Date().getTime();
if (!this.starts[name]) return;
if (this.profiles[name]) this.profiles[name] += (end - this.starts[name]);
else this.profiles[name] = (end - this.starts[name]);
};
/**
* Utils
*/
function imgfilepath(type, name) {
var filepath = '/tmp/niffy';
if (filepath.slice(-1) !== '/') filepath += '/';
mkdirp(filepath);
return (filepath + name + '-' + type + '.png');
}
function* timeout(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
})
}