Skip to content

Instantly share code, notes, and snippets.

@MikaelSoderstrom
Last active May 14, 2018 03:33
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MikaelSoderstrom/4842a97ec399aae1e024 to your computer and use it in GitHub Desktop.
Save MikaelSoderstrom/4842a97ec399aae1e024 to your computer and use it in GitHub Desktop.
Testing with mocha, chai and nightmare.js
var path = require('path');
var Nightmare = require('nightmare');
var should = require('chai').should();
describe('Nightmare demo', function () {
this.timeout(15000); // Set timeout to 15 seconds, instead of the original 2 seconds
var url = 'http://localhost:3000';
describe('Start page', function () {
it('should show form when loaded', function (done) {
new Nightmare()
.goto(url)
.evaluate(function () {
return document.querySelectorAll('form').length;
}, function (result) {
result.should.equal(1);
done();
})
.run();
});
});
describe('Send form', function () {
it('should print the posted string on submit', function (done) {
var expected = 'Hello, world!';
new Nightmare()
.goto(url)
.type('input[name="sometext"]', expected)
.click('input[type="submit"]')
.wait()
.evaluate(function () {
return document.querySelector('#result');
}, function (element) {
element.innerText.should.equal(expected);
done();
})
.run();
});
it('should print "nothing" on submit if no string were provided', function (done) {
var expected = 'nothing';
new Nightmare()
.goto(url)
.click('input[type="submit"]')
.wait()
.evaluate(function () {
return document.querySelector('#result');
}, function (element) {
element.innerText.should.equal(expected);
done();
})
.run();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment