programmable livetest workflows when running mocha tests in nwjs window
npm i livetest-program --save-dev
$ node node_modules/.bin/livetest
$ node livetest --config=./test/config.json
Press F5 in dev tools window to repeat all tests.
If you want to run multiple instances, add the --user-data-dir
option to start nw with different profiles:
$ node livetest --user-data-dir=first-profile &
$ node livetest --user-data-dir=second-profile
$ npm test
;(function () {
const program = require('livetest-program');
describe('...', function () {
...
it('...', (done) => {
assert.strictEqual($('#result').text(), '', 'result text should be empty');
program(this, 5000)
.await('#btn-clickme')
.click('#btn-clickme')
.wait(500)
.capture('livetest-program.clicktest.png')
.run(function () {
assert.strictEqual($('#result').text(), 'yay!', 'result text should have value');
done();
});
});
});
}());
see test/*Spec.js files for working examples
{
"files": [
"test/test1Spec.js",
"./test/**/*Spec.js"
]
}
start program description
describe('suite', function () {
it('test', (done) => {
program(this, 5000)
...
.run(done)
});
});
also works in before():
describe('suite', function () {
before((done) => {
program(this)
...
.run(done)
});
});
returns the current test filename;
program(mochaTest)
.do(function (next) {
this.log(this.getTestFilename());
..
returns the test window object
program(mochaTest)
.do(function (next) {
var testWindow = this.getTestWindow();
testWindow.$('body').empty();
..
returns the main window object
program(mochaTest)
.do(function (next) {
this.getMainWindow().console.log(42);
..
returns the NW-Window Object
see nw docs
get the main window console
program(mochaTest).getConsole().log(42);
program(mochaTest)
.do(function (next) {
this.getConsole().warn(43);
..
get the test window $
const $ = program(mochaTest).getJQuery();
program(mochaTest)
.wait(500)
.log('delayed')
.run();
program(mochaTest)
.await(function () {
// if returns truthy, wait ends
return $('.msgbox').length;
})
.log('message box element available now')
.run();
with argument as a selector string
program(mochaTest)
.await('.controls .desired-classname')
.log('desired element in controls is available now')
.run();
var n = 0;
program(mochaTest)
.log('message %d', ++n)
.do(function (next) {
this.log('message %d', ++n);
next();
})
.run();
// => message 1
// => message 2
position/resize main window
program(mochaTest)
.moveTo(0, 0)
.resizeTo(50, 50)
.wait(1000)
.do(function (next) {
this.moveTo(100, 100);
this.resizeTo(420, 700);
this.wait(100, next);
})
.run();
captures the page to png file
program(mochaTest)
.resizeTo(320, 240)
.capture('/tmp/livetest-programm-xs.png')
.wait(1000)
.do(function (next) {
this.resizeTo(1600, 800)
.capture('/tmp/livetest-programm-xl.png')
.wait(100, next);
})
.run();
attach a handler to an event for the selector elements
program(mochaTest)
.on('click', '#btn-continue', function (ev) {
triggered = true;
})
Use one() to execute the handler at most once.
trigger an event
program(mochaTest)
.trigger('#btn-continue', 'click')
simulate typing a text
program(mochaTest)
.type('.email', '[email protected]')
.do(function (next) {
this.type('.password', 'secret', next);
})
.run();
when finished, this also triggers the 'change' event
simulate click
program(mochaTest)
.click('.reset') // selector string
.do(function (next) {
this.click($('.send')); // jquery
next();
})
.run();
run the program
early exit program execution
program(mochaTest)
.do(function (next) {
this.exit(() => {
this.log('bye');
});
})
.run();
MIT © kruemelo