Perceptual diffing suite
built on Nightmare by Segment
Niffy is a library to help you build end-to-end tests that are augmented with automatic visual change detection.
Niffy does not need to store files beyond a tmp directory. Instead, it makes live requests to two different domains of your choosing, and reports an error if they differentiate above a certain threshold.
npm install --save niffy
global.Promise
- Generator functions
var Niffy = require('niffy');
var baseUrl = 'https://staging.example.com';
var testUrl = 'http://localhost:3000';
describe('End-to-End Test', function () {
var userCreds = {}
before(function () {
var nightmareOptions = { /* ... */ };
niffy = new Niffy(baseUrl, testUrl, nightmareOptions);
userCreds.base = { email: '[email protected]', password: 'abc123' };
userCreds.test = { email: '[email protected]', password: 'abc123' };
});
after(function () {
return niffy.end();
});
it('Signs in and clicks The Button', function () {
return niffy
.goto('/')
// This will compare across your base and test domains :)
.screenshot('home-page')
// Alternatively: .goto('/', 'home-page')
.navigate(function * (nightmare, type) {
//
// Sign Up
//
yield nightmare
.type('[name=email]', userCreds[type].email)
.type('[name=password]', userCreds[type].password)
.click('button[type=submit]')
.wait('.signed-in-page', 2000)
})
// Take a screenshot with a custom diff threshold (default is 0.2%).
.screenshot('sign-in-form', 0.75)
//
// .capture() is both .navigate() and .screenshot() in one!
//
.capture('after-button-click', function * (nightmare, type) {
//
// Click The Button
//
yield nightmare.click('button.the')
})
.execute(); // Release the hounds!
});
});
To create a new Niffy differ:
var niffy = new Niffy(basehost, testhost, nightmareOptions);
basehost
is the url that is assumed "good"testhost
is the url that you are comparing to the basenightmareOptions
can be seen here in the Nightmare docs.threshold
is the maximum percentage difference for a passing test (default: 0.2%)
This method queues an instruction to go to a url
. If screenshotName
is provided, then niffy will also take and compare screenshots.
This method queues fn
, which is expected to be a generator function that takes two parameters:
niffy.navigate(function * (nightmare, type) {
return nightmare.type(...).click(...);
})
where nightmare
is a nightmare instance, and type
is either "base"
or "test"
, in that order.
This method queues a screenshot to be taken, typically after a .navigate()
. It will store a temporary file under /tmp/niffy/
with the given name
, so be sure to choose a name that is unique within the scope of your single test!
If threshold
is given, it will override the default (0.2%
).
A handy convenience function that queues both .navigate(fn)
and .screenshot(name)
.
Runs all queued actions twice – first against your base domain, then against your test domain. Also the queue.
This method shuts down the underlying Nightmare instance (e.g. freeing up memory). Typically you'll use .end()
in the after
method of a mocha test suite, like this:
WWWWWW||WWWWWW
W W W||W W W
||
( OO )__________
/ | \
/o o| MIT \
\___/||_||__||_|| *
|| || || ||
_||_|| _||_||
(__|__|(__|__|
Copyright (c) 2017 Segment.io, Inc. [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.