Postman Tutorial Part 27 – Building Workflows in Collection Runner Using setNextRequest() in Postman

Hello Folks,

We already know that “A Postman collection allows you to group individual pre-built requests together. You can even group by creating folders in a Collection“. Read more about collection here.

Suppose you have 10 requests in a Collection. When you run this collection using Collection Runner, you will see execution of requests happens in the same order as it appears in Collection.

What will be the sequence if your collection has folder/s in it?

By default folder is shown first in Collection than any requests in the root of the collection. So all requests of folders are executed first as they appear in Collection and then any requests in the root of the collection. Within folder of a collection, request are executed in same order as it appears in folder. Note here that you can drag request or folder in Collection to change order.

For example:-

Import below Postman Collection:-

https://www.getpostman.com/collections/603df4d90a8e6d4de49b

In the above Collection, I have two folders and four individual requests in root of Collection. In Collection runner, it will appear in same order as shown below:-

It will be run in same order as well:-

Above is the default behaviour of Collection run.

But you may need to:-

  1. Dynamically decide the next request to be executed at run time.
  2. Skip/Jump requests in Collection

To achieve above customization, we need to use built-in function postman.setNextRequest().

setNextRequest() is a function which takes single argument as the name of request which we want to execute next. It always gets executed at end of the current request. We can use this method in pre-request or test scripts.

Let’s do an example:-

I am using above function to execute request “Get All Bookings” after request “Restful Booker Token Generator API“. As a default run, “Create Booking” should run after “Restful Booker Token Generator API” which I am overriding. Refer below image:-

When you run above collection, you see result as:-

Did you notice that request “Create Booking” did not run at all? It was skipped in flow.

Functionality of setNextRequest() is as below:-

  1. setNextRequest() always runs in the last of current request even you write it in pre-request script tab of request.
  2. We can jump to any request in Collection including any request in folder of collection as well if you are running whole Collection.
  3. If you set next request in forward flow, request/s in between will be skipped. For example:- If we have requests R1, R2, R3 and R4 and we set next request as R4 in R1, only R1 and R4 will be executed. R2 and R3 will be skipped. But if we set next request as R3 in Request R1, then R1, R3 and R4 will be executed and only R2 will be skipped. So if setNextRequest() is not found, default order is assumed from next requests in Collection.
  4. If we set next request in backward direction (Already executed or skipped request), collection run may go in infinite loop. For example:- If we have requests R1, R2, R3 and R4 and we set next request as R1 in R2, then run will be as:- R1, R2, R1, R2, R1…..
  5. If you set next request as the same request, Collection run will go in infinite loop.
  6. You can terminate execution of Collection run in between by setting next request as null i.e. postman.setNextRequest(null);
  7. This method has scope as well. Scope defines the visibility of requests to set as next. If you run entire collection, you can jump to any request in the collection including requests inside folders. But if you run a folder of Collection, you can jump to any request within that folder only, not outside of the folder. It includes requests inside other folders, and also root-level requests in the collection.
  8. If you set a next request which does not exist, execution of Collection will be terminated at that point. It is same as postman.setNextRequest(null);
  9. This is applicable only if you run using Collection Runner. Not as individual request.

It is a very helpful method to create custom workflow but use it carefully. One mistake can take you in infinite loop.

More about API Testing in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyApiTesting

Leave a Reply

Your email address will not be published. Required fields are marked *