Last active
May 14, 2018 03:33
-
-
Save MikaelSoderstrom/4842a97ec399aae1e024 to your computer and use it in GitHub Desktop.
Testing with mocha, chai and nightmare.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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