I'd like to chain several Newman collection test runs. In my particular case, the first test run needs to pass some data to the second test run. How would I go about this?
In my current (not working) solution, I set a object in the global variables used by newman and pass that data to a variable accessible to the second test run. I noticed the newman run only got triggered after running through all the code. I figured I could try using a Promise
to make the code wait until the first test run is finished before doing the second test run.
This is what I currently have:
var output
let run1 = new Promise(function (onResolve, onReject){
let out
let error
newman
.run({ /* options like collection etc go here */ })
.on('done', function (err, summary) {
if(err) {
error = err
}
out = summary.globals.values.members.find(item => /* some query here */).value
})
if(out){
onResolve(out);
} else{
onReject(error);
}
})
run1.then(
//when "onreslove" got triggered
function(value) {
console.log('test run successful')
output = value
},
//when "onReject" got triggered
function(error){
console.log('test run failed with error!\n')
console.log(error)
return
}
)
if (output){
let run2 = new Promise(function(onReslove, onReject) {
let error
let testData = require(/* path to json test data file*/)
//some logic here that adds the data from the output variable to the "testData" object.
newman.run({ /* some options here */})
.on('done', function (err, summary) {
if (err) {
console.log(err)
error = err
}
})
if(!error){
onResolve();
} else{
onReject(error);
}
})
run2.then(
//when onResolve got triggered
function() { console.log(`test run successful`)},
//when onReject got triggered
function(error) {
console.log('test run failed with error!\n')
console.log(error)
return
}
)
} else{
console.log(`Expected output value, received ${output} instead.`)
}
I'm currently stuck, out
will always be undefined
because the "done" event didn't trigger yet at the time my debug session reached if(output){
.