I'm pretty new to POSTMAN and also looking for other ways to write this function. Any additional ways is much appreciative.
Below is a 200 Status OK test in POSTMAN. If the status does not come back as 200 Status OK, I'd like to tally/count that failure as a value(+1). In a later request, an Else/if condition (below) will check how many failures are there. And if failures >0 then that will trigger a new request.
BUT If this test were to have a status(400) and not a 200, meaning it would fail how can I tell it to count that failure(+1) as a value?
pm.collectionVariables.set("failures", []);
let failures = pm.collectionVariables.get("failures")
pm.test("Status test", function () {
pm.response.to.have.status(200);
});
Below is a pre-script request in POSTMAN. As I mentioned above, if there is a value greater than 0 for failures, then run a "New Request". If not, do nothing. I think I have this else/if statement correct. Just including it for your reference.
let failures = pm.collectionVariables.get("failures")
console.log(failures)
if (failures.length >0) {
console.log('failures found')
pm.sendRequest("New Request")
} else {
console.log('NO failures')
postman.setNextRequest(null);
return null
}
(Screen shots - Unable to stop request from executing)
You can count the failures
like this:
let failures = pm.environment.get("failures");
if (failures === undefined || failures === null){
failures = 0;
pm.environment.set("failures", failures);
}
pm.test("Status test", function () {
if (pm.response.code !== 200){
failures += 1;
pm.environment.set("failures", failures)
}
pm.response.to.have.status(200);
});
Code to check would be:
let failures = pm.environment.get("failures")
if (failures > 0) {
console.log('failures found')
postman.setNextRequest("New Request")
//you can reset failures if needed.
pm.environment.set("failures", 0)
} else {
console.log('NO failures')
postman.setNextRequest(null);
}