POSTMAN Workflow

Workflow in POSTMAN is the order of execution of request inside a collection. We have seen earlier that when we run a collection directly, the requests are either executed serially or are executed randomly. But it is also possible to set the priority of the request and also set which request should be executed when. This is called Workflow in POSTMAN.

When the collection runner runs a request, then all the requests get executed which are inside the collection, but there is a pattern in which all the requests get executed. POSTMAN's inbuilt algorithm takes the decision, that in which pattern the request has to be run.

How Collection Runner Runs the Request

Generally, in the Collection runner will run the request serially. There are APIs that needs to send request one by one. That is the second request that can occur only when the first request has completed. And the third request depends on the response of the second request. And so on.

In such cases, the default execution of the requests with the collection runner is useful, as no two requests have to be run parallelly or no random request has to be run.

But there are cases where one APIs request can be dependent on two or more different API response. Or like, there can be a situation where a particular APIs which is somewhere in the middle of the APIs listing, have to get executed for different request randomly.

In such cases, serial execution of the request in collection runner will not help, and we must create a workflow and tell the POSTMAN's collection runner, then which request to execute when. This is done by calling an API request. So we call it as API call.

POSTMAN has its own code snippet to set the next request inside another request. We shall see it in detail later.
API calling

An API call is nothing, but calling an API for its execution in a user-defined pattern.

An example where API calls may be necessary

Consider a website, where customers can purchase different items. Now when the lists of things are displayed, there is an option where customers can check if he can avail of any coupons. Based on the coupons, he can get a discount on the item he wants to purchase.

Now consider, that the username of the customer is registered with the website and so all the credit points are also recorded by the website. So, that even if the customer does not look for the coupon by chance, then also the coupon should be applied in his purchase in default.

Now, the scenario is when the customer is making a purchase, he may use the coupon at his will to see how much discount is possible. And the other scenario is, even if he does not check the coupon and directly purchase it, he will be asked to log in into his account so that the coupons will be automatically applied in his purchase, but here the customer will not be able to see how much discount is applied.

Another scenario can be if the customer adds all his selected items into his cart and keeps it for making a purchase later. Now, suppose the coupons for his account has been exhausted, but then also at the time of final purchase, the coupons will be applied(maybe no discount will be applied, but it the coupon-request will be sent no doubt).

Therefore, the request for checking coupons for the customer can be executed in different ways. If the customer is logged in, he can check for the coupons at his will. If not, he will be asked to log in so that coupons can be directly applied.

Therefore, the coupon-check-request will be executed, all the cases the customer wants to make a purchase. Thus it is necessary to set the coupon check request(or the API) at the end of different other requests so that no route in the website that allows a final purchase avoids the coupon-request.

Default Order in Workflow of POSTMAN

Before customizing the order of request execution, it is necessary to know how the request workflow happens in default in default. To check that, we shall create three different requests and run the collection directly without setting any workflow:

The first step is to create a separate collection so that you can easily use that for customizing workflows. I shall create a new collection called- Workflow:

Visit the link: https://reqres.in/ to select any three different rest API and create three different request inside the collection- Workflow.

Ist Request : copy the URL with the request end-point. https://reqres.in/api/users?page=2 and create a request inside the collection Workflow and name it as request1.





2nd Request: Copy the request URL with the request end-point: https://reqres.in/api/users/2 and create a request inside the same collection Workflow and name it as request2.

3rd Request : copy the request URL with the request end-point: https://reqres.in/api/users and create a request inside the same collection and name it as request3.

postman-workflow-demo-request

Running the collection in the default workflow

Now, that you have created three different requests as request1, request2, and request3. Run the collection runner now, and see the workflow:

postman-collection-runner-button-demo

Click on the play button as shown in the above image to start the collection runner.

postman-collection-runner-options

Now click on the Run button to see all the iterations, orders, environments, etc for the particular collection.

postman-collection-runner-settings

We have named our collection as Workflow, so the button created has the name Run Workflow.

Now, click on the Run Workflow button.

postman-collection-runner-output

Now as you can in the above image, that the request is sent serially, request1, request2 and then request3.

Customizing the Workflow

Customizing the Workflow means, writing some scripts inside any request to redirect the request execution into a different request execution when the current request execution is over.

This can be easily done in POSTMAN, just by using the inbuilt function setNextRequest(), in the Text tab of any request. Let us start with our case, we have three different requests: request1, request2, and request3.

Open the first request request1 and go to the Tests tab and edit the code:

postman.setNextRequest("request3");

This code can be easily understood, it means that set request3 to execute the next, that is after the completion of the current test.

That means, our second request request2 will not be executed after request1, instead, request3 will be executed.

Again, go inside the request3 Tests tab, and edit the below code:

pm.setNextRequest("request2");

The above code says, that set the next request as request2, which is after the completion of the current request request3.

For clarification, I shall explain what happens here :

Earlier by default the order of execution was: request1 ->request2 -> request3.

Now, after customizing, the order of execution will be request1 ->request3 ->request2.

Now, go to the collection- Workflow and then run the collection runner.

postman-collection-runner-changing-order

In the above image, it is clear that the order of request execution has been changed.

Creating loops in POSTMAN request

When at the end of one request another request is attached or set and that request triggers the next request and so on. And when the last request triggers the first one again, that is called looping request.

Looping of requests may be required when one test has to be automated. For example, we have to create a test that checks an API for a number of times. For example, there are different test scripts written for an API response and the response changes from time to time.

In such cases, the test has to run for several iterations, to make sure that all the possible responses and all the test scripts have got a chance to execute on the responses for which they were written.

To create a loop, for all request there should be some script that defines some functions that create a link for the execution of a different request. As we studied earlier, if there is no customization in the workflow, the request will be executed serially one by one and will stop.

Loops when the customized workflow is not necessary:

There can be scenarios when we do not have to create a customized workflow, and all the request has to be sent serially to check all the possible test cases. In such a case, to iterate it, again and again, we just have to use setNextRequest() at the end of the last request script and set the parameter as the name if the first request to initiate the first request again. And so on.

Loops when we require customized workflow:

When we customize the workflow, in whatever way we want. Just we just have to use setNextRequest() at the end of the last request script and set the parameter as the name if the first request to initiate the first request again. And so on.

How to stop loop iterations?

When the collection runner continues to send the requests continuously, there will be two active buttons called Pause and another one is Stop. So, you can use the buttons to pause or permanently stop the looping.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions