Till now we have seen how to run tests on a request and how to run many requests with many tests simultaneously in Collection runner. A collection runner is a very important feature in Postman and without it, it becomes very hard for a person to execute more than one request and perform different tests on them at once. But, if you noticed there is a pattern on how collection runner runs the requests. If you did not, we will do it in this tutorial and consider modifying it in detail. In this chapter we will focus on:
- Execution order of collection runner
- What is Workflow and default workflow.
- How to change the workflow
- Infinite loops in workflow
- Placement of setNextRequest in Postman
Execution order of Collection Runner
As we discussed above, collection runner runs the requests in a particular order. If you noticed earlier, it is a sequential order run.. We need a sequential run to test end to end api flow to check whether everything is correct or not. End to end testing is done to check the flow of an application from start to finish, just as it is designed to work. Most of the user journeys are not simple one or two API calls. Usually a user has to go through a set of actions, in turn set of API calls, to complete the user journey. Taking an example of an ecommerce website, there will be a good number of steps that you have to take in order to purchase an item. A generic flow can be outlined as:
- Logging into the website
- Searching a product
- Adding the product to cart
- Checking out
- Adding personal details such as address
This is where collection runners come into picture and is a really useful tool. We can define sequence of steps (API calls) in the collection runner. Collection runner will run the calls in a sequential order thereby completing the user journey as it is defined in the business requirements.
Default workflow in Postman
The following short example will highlight the way Postman runs requests
1.Make a new collection named No Workflow (How to create Collections in Postman)
2.Enter three requests in it, the ones we have used till now in previous tutorials. Name them Google Api, Weather Api and Customer Api .
NOTE: Remember how we used Environment and Variables, We are using url as a global variable here whose value is http://restapi.demoqa.com. .
3.Run through runner (Collection Runner) and see the output.
NOTE: We have used the same tests for customer api as we did in POST Chapter. You can use it or leave it but it would be good for your practice.
It starts with the first request and then continues to second and then to third. It is running sequentially. But what if after some time there is no need for second request? Let us say you have a collection with thirty meaningful requests. After some days, we found that request no. 16 needs to be run after request no. 19 otherwise we won't get correct results. Now, this creates a problem. You cannot move the requests up and down because there is a chance some other request might move in the process. Moving requests up and down becomes a time consuming process.
What is a workflow?
A workflow is a sequence of things In order to achieve a particular task. A workflow is a set of well defined steps that you have to take to complete a task. In postman workflow is the flow of requests in a defined sequence. The above problem we discussed has a predefined pattern that is running the requests sequentially.
How to change the workflow in Postman.
In postman it is very easy for you to arrange the running of request according to your needs. When you create multiple requests, the default workflow is sequential i.e. all the requests will run as you created them (considering you did not move any). Workflow can be changed easily in Postman. For this follow the following steps.
1.Make a new Collection named With Workflow
2.Enter all the three requests in it that we mentioned above and in the same order as we mentioned.
NOTE: Instead of doing all the work again, try to Duplicate the collection No Workflow and Rename it to With Workflow (Collection Chapter)
3.Go to Google Api request and enter the following in the tests tab
This is a self descriptive statement I believe. You are telling Postman to set next request. In the braces, you need to always write the name of the request.
NOTE: Name of the request should be exactly same as you have written while naming the request. They are case sensitive.
4.Click on Runner and run the request
Notice the order in which requests are run now. This has changed the workflow of the collection and hence now we can run the collection in an order that we want. Observe that one request has not run this time which is weather api. This has happened because in Google Api we mentioned about the next request but what to run after customer api? We did not mention it so Postman tries to run it sequentially as without workflow but since customer api is our last request, it stops there.
Workflows are of significant and powerful feature in Postman. It is a very appreciated feature among engineers who use Postman. While using it inside Postman, you will find it very handy when there is a need for change in the request. However, we need to take care while setting the next request in the tests tab. Sometimes, it may happen that you are stuck in infinite loop. To show this let us create an new Workflow, as mentioned in the next section
Infinite loops in Workflow
To learn about how sometimes we can get in an infinite loop just because of a simple mistake, we will take three search requests on google namely ToolsQA, Postman and Calculator which will search ToolsQA, Postman, Calculator respectively.
1.Create a new Collection called Infinite Workflow and save the following three api requests in the collection we just created
- https://www.google.co.in/search?q=toolsqa and name it ToolsQA
- https://www.google.co.in/search?q=postman and name it Postman
- https://www.google.co.in/search?q=calculator and name it Calculator
2.Go to ToolsQA request and set the next request as Calculator
3.Go to Calculator and set the next request as Postman
- Go to Postman and set the next request as ToolsQA
- Run the collection Infinite Workflow in the collection runner and see the Runner (How to run a collection in Collection Runner)
It is now running in an infinite loop. Go on and observe the tests tab of each request and see where we set the loop to run infinitely.
Although if you are stuck here, then definitely something is wrong with your workflow. There is no point in looking at the runner as it will run forever and ultimately hangs. It is always better to stop the runner to save memory and other resources such as server load. The runner can be stop by just clicking the stop button
Placement of setNextRequest
You might be wondering what if there is a lot of test code? Where should you put the setNextRequest to run it successfully? Placement of setNextRequest is not important while writing it in tests. You can write it anywhere you like and it will still run. It is just a method of saying to the Postman that once you are done with all the test and other stuff, when you are leaving this request you have to run this request. It doesn't matter where you put it since it acts just like a command.
So let us conclude this tutorial here. We hope till now you must have learned a lot about Postman and how amazing it is. We will now move on to the next tutorial and remember, always keep practicing.