This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
197 lines (164 loc) · 4.26 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
/*jshint esnext:true*/
var debug = require('debug')('niffy');
var Nightmare = require('nightmare');
var mkdirp = require('mkdirp');
var fs = require('fs');
var thunkify = require('thunkify');
var defaults = require('defaults');
var sprintf = require('sprintf-js').sprintf;
var diff = require('./lib/diff');
/**
* 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.nightmare = new Nightmare(options);
this.basehost = base;
this.testhost = test;
this.starts = {};
this.profiles = {};
this.errorThreshold = options.threshold;
}
/**
* Generate a test function.
*
* @param {String} path
* @param {Function} fn
*/
Niffy.prototype.test = function* (path, fn) {
var diff = yield this.capture(path, fn);
var pct = '' + Math.floor(diff.percentage * 10000) / 10000 + '%';
var failMessage = sprintf('%s different, open %s', pct, diff.diffFilepath);
var absolutePct = Math.abs(diff.percentage);
if (diff.percentage > this.errorThreshold) {
throw new Error(failMessage);
}
};
/**
* goto a specific path and optionally take some actions.
*
* @param {String} path
* @param {Function} fn
*/
Niffy.prototype.goto = function* (path, fn) {
this.startProfile('goto');
yield this.gotoHost(this.basehost, path, fn);
yield this.gotoHost(this.testhost, path, fn);
this.stopProfile('goto');
};
/**
* goto for a specific host, optionally take some actions.
*
* @param {String} host
* @param {String} path
* @param {Function} fn
*/
Niffy.prototype.gotoHost = function* (host, path, fn) {
yield this.nightmare.goto(host + path);
if (fn) {
yield timeout(1000);
yield fn(this.nightmare);
yield timeout(1000);
}
};
/**
* capture a specific path after optionally taking some actions.
*
* @param {String} path
* @param {Function} fn
*/
Niffy.prototype.capture = function* (path, fn) {
/**
* Capture the screenshots.
*/
yield this.captureHost('base', this.basehost, path, fn);
yield this.captureHost('test', this.testhost, path, fn);
/**
* Run the diff calculation.
*/
this.startProfile('diff');
var pathA = imgfilepath('base', path);
var pathB = imgfilepath('test', path);
var pathDiff = imgfilepath('diff', path);
var result = yield diff(pathA, pathB, pathDiff);
this.stopProfile('diff');
/**
* Prep the results.
*/
result.percentage = result.differences / result.total * 100;
result.diffFilepath = imgfilepath('diff', path);
return result;
};
/**
* capture for a specific host name + host, and optionally take some actions.
*
* @param {String} name
* @param {String} host
* @param {String} path
* @param {Function} fn
*/
Niffy.prototype.captureHost = function* (name, host, path, fn) {
this.startProfile('goto');
yield this.gotoHost(host, path, fn);
this.stopProfile('goto');
this.startProfile('capture');
yield this.nightmare.wait(1000).screenshot(imgfilepath(name, path));
this.stopProfile('capture');
yield timeout(250);
};
/**
* End the capture session.
*/
Niffy.prototype.end = function* () {
yield this.nightmare.end();
debug(
'profile\n\tgoto %s\n\tcapture %s\n\tdiff %s',
this.profiles.goto,
this.profiles.capture,
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(name, path) {
var filepath = '/tmp/niffy' + path;
if (filepath.slice(-1) !== '/') filepath += '/';
mkdirp(filepath);
return (filepath + name + '.png');
}
function* timeout(ms) {
var to = function (ms, cb) {
setTimeout(function () { cb(null); }, ms);
};
yield thunkify(to)(ms);
}