Almost everyone who works into web development, front-end or back-end, has in some part of their development career used "Postman" or some other API client to test the app we are building. But, do we all know that Postman provides so much more than what we usually use it for.
Let's see how we can be more productive and use Postman to its full potential.
*Since this is not an introduction, I am assuming that everyone here is familiar with basic Postman features such as sending requests, saving requests, creating collections, blah blah blah. *
Time to deliver some letters. 😛
Setting up environments can boost up your productivity if your application is deployed in different systems. With environments, we can switch between different versions in no time.
It's also better to create different environments for the different applications we are working on so that we don't mix up the variables from one application into another.
Variables provide a dynamic nature to the values we use in our application. We can set variables in different scopes, thus we not have to carry the values from the requests manually from place to place.
Postman supports the following variable scopes:
Scripts can be of two types:
- Pre-request Script
- Test Script
Pre-request Scripts gives us the option to execute something right before sending a request. This can be anything such as setting a variable.
Say, we want to set the date variable to the current date in every request, we can do it by adding the following in the Pre-Request Scripts section
pm.environment.set("time", new Date());
The "Test" section serves a generic purpose of running test cases as well as using it to perform some other tasks such as setting up variables dynamically, chaining requests, etc., right after a request has finished running.
All this happens due to the Postman Sandbox.
Postman also provides the console option where we can debug side-by-side the tests that we are writing.
console.log(), console.error(), console.info()
Variables are not only useful in switching between the environments, but they also come really handy when we want to set the values from one request and use it into another. We can do this using the scripts we just learned about.
Let's see how we can do this:
Suppose we want to fetch the current weather of a particular place.
We are using two APIs to fulfill our purpose:
We can run a sequence of tests on the completion of a request.
Postman provides us a lot of sample test scripts that we can simply add by clicking on it.
Here the first argument in the method test gives the name to that test, and the following argument is the function that executes the test.
Postman sandbox provides us a variety of options to test the desirability of the response. We can write them as:
pm.response.to.be.<condition> pm.response.to.not.be.<condition> pm.response.to.have.<condition> pm.response.to.not.have.<condition>
Say we have to check, in our weather example, that we get an OK response and the response always contains the current weather. We can do it as below:
And then we can see if our test cases have passed in the "Test Results" section
Postman provides us a feature called the Collection Runner. It helps us run all the APIs within a collection one after the other.
The Collection Runner then gives us the details of all the APIs that ran successfully with all their details.
With respect to the option we choose, we are given further options corresponding to the option we have selected.
Say, if we choose OAuth 1.0, we will be required to fill out the details such as "Consumer Key", "Consumer Secret", "Access Token" and "Token Secret".
If we choose Bearer Token, then we simply have to input the JWT token which would be appended to the header of our requests, hence authenticating it.
Remember the example in the scripts section? No? Scroll up.
There we had set the token value from the response as an environment variable, we can simply use that value from the environment into our corresponding request, this sparing us of the hassle of copying the token everywhere to each one of our requests.
That was all for this article. I have some more features in mind that I may write in future posts.
If you want to learn more about Postman features then refer to the following links:
Enjoy building APIs