0

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();
}

1 Answers1

0

You've already posted how to store a routine in a variable, which is one of the solutions for this use case. The other thing you can do is writing Tests atthe collection or folder level, from the docs:

You can add tests to individual requests, collections, and folders in a collection. Postman includes code snippets you add and then change to suit your test logic.

These tests would run after every request in the collection or folder:

enter image description here

bitoiu
  • 6,893
  • 5
  • 38
  • 60
  • Thank you for your answer. I am not interested in running the tests after each requests because for some requests they will fail. My goal is to have these tests running for some requests only and to be easily maintained. Like if something changes in the status due to Dev, I want to spend 10 seconds not 15 minutes editing x requests. – Bogdan Buciu Aug 25 '22 at 13:49
  • You can group the requests you want to filter in a folder, or a collection. Alternatively the solution you posted is having a shared library in a folder or remote address and work with that. – bitoiu Aug 25 '22 at 13:49
  • Where can I get some info on how to set up shared libraries in Postman? – Bogdan Buciu Aug 25 '22 at 13:51
  • You've linked the answer in your question. You basically write a library and store it in a variable. You then `eval` the variable everytime you want to use it. If you have the library hosted somewhere you can download it at runtime. https://blog.postman.com/adding-external-libraries-in-postman/ – bitoiu Aug 25 '22 at 13:55
  • Thanks. Made it work. Did not use the method where eval is mentioned I've used a different approach from that post. – Bogdan Buciu Aug 25 '22 at 14:18