We can handle infinite loops in the Postman workflow. The requests in a Collection get executed in the order in which they appear. However, we can change the workflow with the help of the function - postman.setNextRequest().
It is used to direct the next request to be executed. The name of the next request to be executed is passed as a parameter to this function.
postman.setNextRequest("next request name")
Let us take an example of a Collection having four requests -
We would like Postman to change to a workflow such that the below requests are executed from top to bottom in the order listed below −
To achieve this, let us follow the below steps −
Step1 − Write the below script for the Create User request in the Tests tab,
Step2 − Write the below script for the Update Request in the Tests tab,
Step3 − Click on the arrow to the right of the Collection name in the sidebar. Then click on Run.
Step4 − Collection Runner window opens up. Click on Run Collection1.
Step5 − After the execution has been triggered, we find that the Update Request and Delete Request are executing in an infinite loop and we have to click on the Stop Run button to explicitly stop the execution.
Step6 − To handle this infinite loop via a script, we have to write the below script for the request – Delete Request in the Tests tab.
Step7 − Repeat Steps 3 and 4 to run the requests in the Collection again.
Step8 − The execution results in the Collection Runner now display no request running in an infinite loop and the sequence of the execution is Get Request, Create User, Update Request and Delete Request which as per the expectation.