require('newman').run({
// pass a collection that fetches HTML
collection: {
item: [{
request: 'http://httpbin.org/html',
event: [{
listen: 'test',
script: {
type: 'text/javascript',
// add a script that checks whether heading H1 is present in HTML
exec: [
'tests["response body received"] === !!responseBody;',
'var html = cheerio(responseBody);',
'console.log("Heading H1: " + html.find("h1").text());', // log it
'tests["html has h1 heading"] = !!html.find("h1").text();'
]
}
}]
}]
},
abortOnFailure: true // ensures run stops abruptly when any error occurs.
})
// log all runtime console to real console
.on('console', function (e, args) {
console.log.apply(console, args.messages);
})
// on complete mark the same
.once('done', function (e) {
console.log('run complete ' + (e ? 'with' : 'without') + ' errors');
e && console.log(e); // log error
});