在nodejs模块中运行newman
我有一个带有类的nodejs模块。
在类的内部,有一个调用newman(Postman cliRunner)的方法
无法找出如何返回纽曼运行结果数据的方式。newman调用本身(在模块外部)可以正常工作。
mymodule.js
var newman = require('newman');
module.exports = function (collection, data) {
this.run = function () {
newman.run({
collection: require(this.collection + '.postman_collection.json'),
environment: require(this.environment + '.postman_environment.json')
}, function () {
console.log('in callback');
}).on('start', function (err, args) {
}).on('beforeDone', function (err, data) {
}).on('done', function (err, summary) {
});
return 'some result';
}
}
index.js
var runNewman = require('./mymodule');
var rn = new runNewman(cName, cData);
var result = rn.run(); // never returns any variable
cosole.log(result);
回答如下:您可以使用事件和回调看到newman
。如果需要数据,则需要从done
事件回调中发送数据。您可以在此处执行的操作是将代码转换为使用Promise api
。
请参考以下代码段
var newman = require('newman')
module.exports = function (collection, data) {
this.run = function () {
return new Promise((resolve, reject) => {
newman.run({
collection: require(this.collection + '.postman_collection.json'),
environment: require(this.environment + '.postman_environment.json')
}, function () {
console.log('in callback')
}).on('start', function (err, args) {
if (err) { console.log(err) }
}).on('beforeDone', function (err, data) {
if (err) { console.log(err) }
}).on('done', function (err, summary) {
if (err) { reject(err) } else { resolve(summary) }
})
})
}
}
为此的调用代码是
var runNewman = require('./mymodule');
var rn = new runNewman(cName, cData);
var result = rn.run().then(console.log, console.log); //then and catch
在nodejs模块中运行newman
我有一个带有类的nodejs模块。
在类的内部,有一个调用newman(Postman cliRunner)的方法
无法找出如何返回纽曼运行结果数据的方式。newman调用本身(在模块外部)可以正常工作。
mymodule.js
var newman = require('newman');
module.exports = function (collection, data) {
this.run = function () {
newman.run({
collection: require(this.collection + '.postman_collection.json'),
environment: require(this.environment + '.postman_environment.json')
}, function () {
console.log('in callback');
}).on('start', function (err, args) {
}).on('beforeDone', function (err, data) {
}).on('done', function (err, summary) {
});
return 'some result';
}
}
index.js
var runNewman = require('./mymodule');
var rn = new runNewman(cName, cData);
var result = rn.run(); // never returns any variable
cosole.log(result);
回答如下:您可以使用事件和回调看到newman
。如果需要数据,则需要从done
事件回调中发送数据。您可以在此处执行的操作是将代码转换为使用Promise api
。
请参考以下代码段
var newman = require('newman')
module.exports = function (collection, data) {
this.run = function () {
return new Promise((resolve, reject) => {
newman.run({
collection: require(this.collection + '.postman_collection.json'),
environment: require(this.environment + '.postman_environment.json')
}, function () {
console.log('in callback')
}).on('start', function (err, args) {
if (err) { console.log(err) }
}).on('beforeDone', function (err, data) {
if (err) { console.log(err) }
}).on('done', function (err, summary) {
if (err) { reject(err) } else { resolve(summary) }
})
})
}
}
为此的调用代码是
var runNewman = require('./mymodule');
var rn = new runNewman(cName, cData);
var result = rn.run().then(console.log, console.log); //then and catch