Postman - Workflows


Advertisements

In a Postman Collection, the requests are executed in the order in which they appear. Every request is run first by the order of the folder followed by any request at the Collection root.

Let us create a Collection (Collection1) with four requests. The details on how to create a Collection is discussed in detail in the Chapter about Create Collections.

Step 1 − Click on the arrow appearing to the right of the Collection name in the sidebar. Then, click on Run button to trigger execution of requests within the Collection.

Run button

Step 2 − The Collection Runner pop-up comes up. The RUN ORDER section shows the order in which the requests shall get executed from top to the bottom. (GET-<POST-<DEL-<PUT). Click on the Run Collection1 button.

Run Collection

Step 3 − Execution Results show the GET request executed first, followed by POST, then DEL and finally PUT, as mentioned in the RUN ORDER section in the Step 2.

Run Order

If we want to change the order of the request to be executed (for example, first the Get Request shall run, followed by Create User, then Update Request and finally the Delete Request). We have to take the help of the function postman.setNextRequest().

This function has the feature to state which request shall execute next. The request name to be executed next is passed as a parameter to this function. As per the workflow, we have to add this function either in the Tests or Pre-request Script tab under the endpoint address bar in Postman.

The syntax for execution of a request in Postman is as follows −

postman.setNextRequest("name of request")

Implementation of a Workflow

The implementation of a workflow in Postman is explained below in a step wise manner −

Step 1 − Add the below script under the Tests tab, for the request – Create User.

postman.setNextRequest("Update Request")

The following screen will appear −

Workflow

Step 2 − Add the below script under the Tests tab, for the request – Update Request.

postman.setNextRequest("Delete Request")

The following screen will appear −

Workflow1

Output of Workflow

Given below is the output of the workflow −

Workflow2

The output shows that Update Request and Delete Request are running in an infinite loop until we stop it by clicking the Stop Run button.

Infinite Workflow Loop

If we want to stop the infinite Workflow loop via script, we have to add the below script for the request – Delete Request.

postman.setNextRequest(null)

The following screen will appear −

Loop

Again run the same Collection and output shall be as follows −

Loop1

The output shows the order of execution as Get Request, Create User, Update Request and finally Delete Request.

Advertisements