Is there any way to prevent the request from sending due to a failed test in the pre-request script?
I’ve tried making the test a function and returning false, postman.setNextRequest(null) (which didn’t seem to even set the request to null anyway) and throwing an error. Nothing seemed to work.
@kunagpal - thanks. I’ve actually managed to get it to work - maybe not as gracefully as I wanted - by doing the following:
if (pm.variables.get("STR_ENTITY").toLowerCase() !== pm.variables.get("ENTITY").toLowerCase()) {
console.error("Entity values do not match");
throw new Error("Entity values do not match");
}
This throws an error for each iteration in the run so my users will need to go and look in the console to see what’s going on, but I’ve achieved the result I wanted.
@cocovan: this doesn’t work for me. My request is still sent even if the pre-request fails and throws the error. Anything else you did, to get it to work?
I did not want to raise an error, if you also do not want to raise an error, then you can try this. I put the check in the preceding request’s tests. Pseudo code.
In Request2’s tests section
if var is x postman.setNextRequest(“Request3”);
else postman.setNextRequest(“Request4”);
In Request3’s and in Request4’s tests section, to reset things, I put
postman.setNextRequest();
Example code:
if($region === $uk) {
console.log("running the request, region: " + $region);
postman.setNextRequest(“NuReq”);
} else {
console.log("skipping this request: " + $region);
postman.setNextRequest(“StuffReq”);
}