Skip to content

Commit eb82c3b

Browse files
author
jasbraun
committed
segmentioGH-9 added dynamic screenshot folder
moved default folder to local project
1 parent 229cf1d commit eb82c3b

File tree

6 files changed

+1599
-32
lines changed

6 files changed

+1599
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/*
2+
.DS_Store

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ Niffy is built on [Nightmare](https://github.com/segmentio/nightmare) and used i
1818
To create a new Niffy differ:
1919

2020
```js
21-
let niffy = new Niffy(basehost, testhost, nightmareOptions);
21+
let niffy = new Niffy(basehost, testhost, options);
2222
```
2323

2424
* `basehost` is the url that is assumed "good"
2525
* `testhost` is the url that you are comparing to the base
26-
* `nightmareOptions` can be seen [here in the Nightmare docs](https://github.com/segmentio/nightmare#nightmareoptions)
27-
* `.threshold` is the maximum percentage difference for a passing test (default: 0.2%)
26+
* `options` aside from the few specific niffy options, all nightmare options can be used. They can be seen [here in the Nightmare docs](https://github.com/segmentio/nightmare#nightmareoptions)
27+
* `pngPath` is the folder the screenshots will be saved. It defaults to `./niffy`
28+
* `threshold` is the maximum percentage difference for a passing test (default: 0.2%)
2829

2930
### .test(url[, fn])
3031
This method instructs niffy to go to a `url` (and optionally take additional actions like clicking, typing or checkboxing via the `fn` argument), and test `basehost` vs. `testhost` screenshots for pixel differences, and output the diff-highlight image. Typically you'll use `.test(url, fn)` in the body of a mocha test, like this:

index.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var thunkify = require('thunkify');
88
var defaults = require('defaults');
99
var sprintf = require('sprintf-js').sprintf;
1010
var diff = require('./lib/diff');
11+
var path = require('path');
1112

1213
/**
1314
* Export `Niffy`
@@ -25,8 +26,9 @@ module.exports = Niffy;
2526

2627
function Niffy(base, test, options) {
2728
if (!(this instanceof Niffy)) return new Niffy(base, test, options);
28-
options = defaults(options, { show: false, width: 1400, height: 1000, threshold: .2 });
29+
options = defaults(options, { show: false, width: 1400, height: 1000, threshold: .2, pngPath: './niffy' });
2930
this.nightmare = new Nightmare(options);
31+
this.pngPath = options.pngPath;
3032
this.basehost = base;
3133
this.testhost = test;
3234
this.starts = {};
@@ -37,12 +39,12 @@ function Niffy(base, test, options) {
3739
/**
3840
* Generate a test function.
3941
*
40-
* @param {String} path
42+
* @param {String} pngPath
4143
* @param {Function} fn
4244
*/
4345

44-
Niffy.prototype.test = function* (path, fn) {
45-
var diff = yield this.capture(path, fn);
46+
Niffy.prototype.test = function* (pngPath, fn) {
47+
var diff = yield this.capture(pngPath, fn);
4648
var pct = '' + Math.floor(diff.percentage * 10000) / 10000 + '%';
4749
var failMessage = sprintf('%s different, open %s', pct, diff.diffFilepath);
4850
var absolutePct = Math.abs(diff.percentage);
@@ -54,27 +56,27 @@ Niffy.prototype.test = function* (path, fn) {
5456
/**
5557
* goto a specific path and optionally take some actions.
5658
*
57-
* @param {String} path
59+
* @param {String} pngPath
5860
* @param {Function} fn
5961
*/
6062

61-
Niffy.prototype.goto = function* (path, fn) {
63+
Niffy.prototype.goto = function* (pngPath, fn) {
6264
this.startProfile('goto');
63-
yield this.gotoHost(this.basehost, path, fn);
64-
yield this.gotoHost(this.testhost, path, fn);
65+
yield this.gotoHost(this.basehost, pngPath, fn);
66+
yield this.gotoHost(this.testhost, pngPath, fn);
6567
this.stopProfile('goto');
6668
};
6769

6870
/**
6971
* goto for a specific host, optionally take some actions.
7072
*
7173
* @param {String} host
72-
* @param {String} path
74+
* @param {String} pngPath
7375
* @param {Function} fn
7476
*/
7577

76-
Niffy.prototype.gotoHost = function* (host, path, fn) {
77-
yield this.nightmare.goto(host + path);
78+
Niffy.prototype.gotoHost = function* (host, pngPath, fn) {
79+
yield this.nightmare.goto(host + pngPath);
7880
if (fn) {
7981
yield timeout(1000);
8082
yield fn(this.nightmare);
@@ -85,27 +87,27 @@ Niffy.prototype.gotoHost = function* (host, path, fn) {
8587
/**
8688
* capture a specific path after optionally taking some actions.
8789
*
88-
* @param {String} path
90+
* @param {String} pngPath
8991
* @param {Function} fn
9092
*/
9193

92-
Niffy.prototype.capture = function* (path, fn) {
94+
Niffy.prototype.capture = function* (pngPath, fn) {
9395

9496
/**
9597
* Capture the screenshots.
9698
*/
9799

98-
yield this.captureHost('base', this.basehost, path, fn);
99-
yield this.captureHost('test', this.testhost, path, fn);
100+
yield this.captureHost('base', this.basehost, pngPath, fn);
101+
yield this.captureHost('test', this.testhost, pngPath, fn);
100102

101103
/**
102104
* Run the diff calculation.
103105
*/
104106

105107
this.startProfile('diff');
106-
var pathA = imgfilepath('base', path);
107-
var pathB = imgfilepath('test', path);
108-
var pathDiff = imgfilepath('diff', path);
108+
var pathA = this.imgfilepath('base', pngPath);
109+
var pathB = this.imgfilepath('test', pngPath);
110+
var pathDiff = this.imgfilepath('diff', pngPath);
109111
var result = yield diff(pathA, pathB, pathDiff);
110112
this.stopProfile('diff');
111113

@@ -114,7 +116,7 @@ Niffy.prototype.capture = function* (path, fn) {
114116
*/
115117

116118
result.percentage = result.differences / result.total * 100;
117-
result.diffFilepath = imgfilepath('diff', path);
119+
result.diffFilepath = this.imgfilepath('diff', pngPath);
118120
return result;
119121
};
120122

@@ -123,18 +125,18 @@ Niffy.prototype.capture = function* (path, fn) {
123125
*
124126
* @param {String} name
125127
* @param {String} host
126-
* @param {String} path
128+
* @param {String} pngPath
127129
* @param {Function} fn
128130
*/
129131

130-
Niffy.prototype.captureHost = function* (name, host, path, fn) {
132+
Niffy.prototype.captureHost = function* (name, host, pngPath, fn) {
131133

132134
this.startProfile('goto');
133-
yield this.gotoHost(host, path, fn);
135+
yield this.gotoHost(host, pngPath, fn);
134136
this.stopProfile('goto');
135137

136138
this.startProfile('capture');
137-
yield this.nightmare.wait(1000).screenshot(imgfilepath(name, path));
139+
yield this.nightmare.wait(1000).screenshot(this.imgfilepath(name, pngPath));
138140
this.stopProfile('capture');
139141
yield timeout(250);
140142
};
@@ -181,11 +183,15 @@ Niffy.prototype.stopProfile = function (name) {
181183
/**
182184
* Utils
183185
*/
184-
185-
function imgfilepath(name, path) {
186-
var filepath = '/tmp/niffy' + path;
186+
Niffy.prototype.imgfilepath = function (name, pngPath) {
187+
var filepath = this.pngPath + pngPath;
187188
if (filepath.slice(-1) !== '/') filepath += '/';
188-
mkdirp(filepath);
189+
if (filepath[0] === '.') {
190+
filepath = path.join( __dirname, filepath.slice(1));
191+
}
192+
mkdirp(filepath, function (err) {
193+
if (err) console.error(err.toString());
194+
});
189195
return (filepath + name + '.png');
190196
}
191197

lib/diff.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module.exports = diff;
1717
*/
1818

1919
function* diff(pathA, pathB, pathDiff) {
20-
2120
debug('starting img diffing %s', new Date());
2221

2322
var imgA = yield thunkify(readPNG)(pathA);

0 commit comments

Comments
 (0)