API Test Automation using POSTMAN
By Akshay Mayekar in QA
Nowadays when we talk about automation in software project everybody jumps into automating functional test cases of their application and writing GUI tests. In addition, there is one important aspect of “API Testing” which we can run right after unit tests as part of the process to provide an initial level of confidence.
In this article, we will talk about what is API Testing and how can we automate that using a tool called “Postman”.
API testing can be one of the most challenging parts of the software and QA testing because APIs can be complicated.
While developers tend to test only the basic functionality they are working on, testers are in charge of testing functionality, performance and security of APIs, discovering how all components work together from end to end.
As the role of APIs is increasing, customers too want to make sure that the API should work properly in all aspects. Hence companies use various tools to test API and use different methods too for testing them.
How to go about these API testing so here are common types of Tests in API Testing :
- Verify if API doesn’t return any response
- Fails to handle error conditions gracefully
- Input Parameter Validations
- Sequencing of the calls – an output of one can be input to the other type of conditions
- Validation of return codes based on the correctness of the input
- Performance of APIs – Delayed in API Response time
All above tests should be done in an automated way and there are various tools available in the market but one that stands out from the rest and we used for all our projects is “POSTMAN”.
We will go step by step from setting up Postman to automating the tests using postman and executing them using Jenkins.
Postman Set Up
- Open Google Chrome
- Go To Tools > Extensions
- Search for Postman in the chrome store
- Click on Add to Chrome button
- Click on Open
When you open Postman, you should see the following Interface
Automation Tests using Postman
Under “Test” section in request editor area, you can write your automation scripts.
Also to the right-hand side of the test editor, there is a list of snippets. You can click on any snippet title and the corresponding code snippet will be appended to the test script in the editor. This makes writing simple tests extremely fast.
First Automation Test
Postman tests are written in Javascript. It expects the special Tests object to be populated with a description and the result of the test. Postman renders the results in the “Tests” tab as well as in the Collection runner
For our first test, we are going to use sample OpenWeatherMap APIs and check if the status code is 200, and if the Content-Type header is present.
- Set the URL to http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22
- Scroll down in the snippets editor and click on the “Status code: Code is 200” snippet and Response Header check
Check the “Test Results” tab. We see that this both the tests have passed as the correct status code is returned and a Content-type header is present.
Now if we want to add one custom test here, for example, we might want to make sure that the Content-Type header exists and it is equal to “application/json”.
Postman tests are just Javascript code, we can add conditionals like If to our code if needed. We’ll first check if contentTypeHeaderExists is true. If yes, we’ll check if it’s equal to “application/json”
As you can see in our Test Results tab, Everything seems to be fine with this API as all our test cases have passed.And that’s all it takes. Even if you are new to programming, it’ll just take you a few minutes to write tests with Postman.
Setting up Environment for Automation
While working with APIs, you often need different setups for your local machine, the development server, or the production API. Environments let you customize requests using variables so you can easily switch between different setups without changing your requests
In above example, we have created an environment as OpenWeatherMap and set one variable “url”.So every time when we execute a request for openWeatherMap API there is no need to add entire URL.Select the environment in the environment list to activate it.
Extracting data from responses and chaining requests
Another advantage of an environment variable is extracting values from the response and saving it inside an environment variable. And that variable is used for subsequent API requests.Some common examples of such variables you would use with an API are session tokens and user IDs.
Let’s see an example of chaining requests in OpenWeather APIs. In our last example, we used API which gets weather information by city name and in response, we got “lat” (latitude) and “long” (longitude). In next example, we will use API which gets weather information by lat and long where we have to send lat and long values in request parameters.
For above scenario, we will first send the API request which gets weather information by city name and then from that response, will store lat and long values in environment variables. In next API request, we will use those environment variables instead of manually adding values to it.
In the response, you can see the lat and long. Now we have to extract these two values and set it as environment variables.
To extract the lat and long, we need the following code.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable(“longitude“, jsonData.coord.lon);
postman.setEnvironmentVariable(“latitude“, jsonData.coord.lat);
Add this to the test editor and hit send. Hover over the quick look window (q) to check that the variables “longitude” and “latitude” has the value extracted from the response
Now you can see long and lat values are stored in our variables longitude and latitude respectively and will use these variables in next request.
In above request, we have used the environment variables instead of manually entering values every time.
All you have to do is call postman.setEnvironmentVariable(key, value) to set a variable with values you have extracted from the response. You can even add something dynamically generated through Javascript.
Postman Collection Runner
Chaining requests are extremely handy and are needed for most API testing scenarios. With Postman’s collection runner, you do not need to run each of these requests one after the other though. Using the collection runner, you can execute all requests in a collection with one click.
Running collections is a simple task. All you need to do to is select a collection, an environment (optional), and set the number of iterations.
Once the run is over, you can analyze the run results.Ultimately, your goal would be that 100% of your tests pass. A test which was passing earlier but fails now would be easy to track
Command Line integration with Newman
Newman is a command line collection runner for Postman. It allows you to run and test a Postman Collection directly from the command line. It is built with extensibility in mind so that you can easily integrate it with your continuous integration servers and build systems.
Newman maintains feature parity with Postman and allows you to run collections just the way they are executed inside the collection runner in the Postman app.
Newman is built on Node.js. To run Newman, make sure you have Node.js installed
Once Node.js is installed, install Newman from npm globally on your system allowing you to run it from anywhere, below is the command to use.
$ npm install -g newman
The easiest way to run Newman is to run it with a collection. You can run any collection file from your file system.
$ newman run mycollection.json
Integrating Automated API Tests using Jenkins
You can run your postman collection inside your continuous Integration tool like Jenkins.
All you need to do is run your Newman command under shell command section of your Jenkins Setup.And using Jenkins you can send the reports generated by newman to your project team members via Email
Conclusion
Postman helps companies supercharge their API workflow. It is the most efficient way to test, develop and document APIs. Up to this point, this is one of the best tools to test APIs. Of course new tools will come in future but in the end what we want is a test suite which is easy to run and maintain as part of our project build. And with new features of postman and integration with CI tools, these tools are going to be used more extensively in the future.
References
https://pages.apigee.com/rs/apigee/images/api-design-ebook-2012-03.pdf
https://www.getpostman.com/docs/v6/