Branching and Looping
In Postman, we can also work on branching and looping. We can branch and loop across API requests with the help of setNextRequest() function.
This function is used to set the request to be executed next. This function takes one argument, i.e., the name of the request, which will execute next.
In the above example, “My API” is the name of the request.
Some features of setNextRequest():
- Provide the request name or request ID, and the collection runner will handle the rest.
- We can use the setNextRequest() in the test script as well as in the pre-request script. The last set value takes precedence when you have more than one assignment.
- The collection runner will follow the linear execution settings from default settings and moves to the next request if Postman.setNextRequest() isn’t given in a request.
Loop Over the Current Request
If you give the currently running request name in the argument of setNextRequest() function, then Postman will run the same request continuously.
But when you are continuously looping along with the same request, we have to apply some logic on setNextRequest() so that the request won’t run endlessly; the collection runner will otherwise have to be force closed.
Stop Workflow Execution
To stop the execution of workflow pass the null argument in setNextRequest() function.