I am have the following loop which triggers a the same Get Request until an order is processed and the correct status is set.
The problem is that I have to use the same snippet on different scenarios and each time there is a change i have to change the snippet in multiple places.
What I would like to do is to set the snippet at collection level and then call it in the requests Tests area.
Expected behavior: The loop checks the response body and if a certain status is not set then it retriggers the same request until the status is set after that it retriggers the request again and then moves on to the next request.
There is also a counter implemented to avoid infinite loops in case something happens.
My question is how can this be set as a method at collection level. For other functions I used THIS approach and it worked but for some unknown reason for this function it does not work it either goes into a wrong branch or does not loop the same request when called.
Code snippet used:
var jsonData = pm.response.json()[0];
var READY_STATUS = jsonData.deliveryGroups[0].history.find(elem => elem.status === "Preparing_Status");
pm.environment.get("retryCounter")===undefined ? pm.environment.set('retryCounter',10):null
retryCount = pm.environment.get("retryCounter");
if (!READY_STATUS && retryCount > 0){
retryCount = retryCount - 1;
pm.environment.set('retryCounter',retryCount);
console.log(retryCount);
pm.test("Delivery group not in ready status", function(){});
postman.setNextRequest(pm.info.requestId);
setTimeout(() => {}, 5000);
} else if (jsonData.deliveryGroups[0].status === "READY_STATUS" && retryCount > 0) {
retryCount = retryCount - 1;
console.log(retryCount);
pm.environment.set('retryCounter',retryCount);
pm.test("Delivery group and items in READY_STATUS_Status state", function () {
pm.expect(jsonData.deliveryGroups[0].status).to.eql("READY_STATUS");
pm.expect(jsonData.deliveryGroups[0].items[0].status).to.eql("READY_STATUS");
pm.expect(jsonData.deliveryGroups[0].items[1].status).to.eql("READY_STATUS");
pm.expect(jsonData.deliveryGroups[0].items[2].status).to.eql("READY_STATUS");
});
postman.setNextRequest(pm.info.requestId);
setTimeout(() => {}, 10000);
} else if(retryCount === 0){
pm.test("Test failed", function(){
throw Error("Test failed")
});
postman.setNextRequest(null);
}
else {
pm.test("Moving on to next test", function(){});
postman.setNextRequest();
}