How to use Postman for API Testing Automation
In today’s development world, the importance of APIs is known to almost all.
APIs make it possible for any two separate applications to transfer and share data between them. They also make it easier for an application's users to execute actions without having to use the application’s GUI. From the developers' POV, it's an easy way to execute certain functionalities of their app and test it as well.
Using APIs on a daily basis might become cumbersome, as one might have dozens or even hundreds of APIs that he or she needs to use or test. That makes it difficult to keep up with their exact request’s address(es), header(s), authorization credential(s) etc., and by that make it harder to test the API for functionality, security and exception handling.
What is Postman?
Postman is a popular API client that makes it easy for developers to create, share, test and document APIs. This is done by allowing users to create and save simple and complex HTTP/s requests, as well as read their responses. The result - more efficient and less tedious work.
In this blog post we will go over how to use Postman to execute APIs for your daily work, an ability that is available in their free version. We will also show you how to use Postman when using BlazeMeter.
In case you don’t have Postman installed, you’ll need to download it and install it.
How to use Postman to execute APIs
Postman is very convenient when it comes to executing APIs. Once you’ve entered and saved them, you can simply use them over and over again, without having to remember the exact endpoint, headers, API keys, etc.
Launch Postman by clicking on the logo. Its "Welcome" panel shows different “Building blocks” available in the Postman application. For now, skip this panel by clicking on the “x” at the top right corner.
Here is a detailed example explaining how to enter a new API request using CA BlazeMeter’s ‘test create’ API, but you can do this for the product you are developing:
1. Enter the API endpoint where it says ‘Enter request URL’ and select the method (the action type) on the left of that field. The default method is GET but we will use POST in the example below.
2. Add authorization tokens/credentials according to the server side requirements.
3. Enter headers in case they are required.
4. Enter a POST body in case it is required. In this example, we are creating a BlazeMeter test that requires a JSON payload with relevant details.
5. If you wish to execute this API now, hit the ‘Send’ button, which is located to the right of the API request field. You can also click on the ‘Save’ button beside it to save that API request to your library.
That’s it! Now you know how to enter your API request to Postman and save it to your library.
How Postman helps you share BlazeMeter’s API
One of Postman’s fantastic features is ‘Collections’. ‘Collections’ allow you to group together several APIs that might be related, or perhaps should be executed in a certain sequence.
For example, in the screenshot below, you can see a collection that includes 4 APIs that are all required to create and run a BlazeMeter test. The first two APIs create the test object - the first of the two applies the necessary configuration, and the following API uploads the script file needed to run it. The last two APIs start and stop the test we created previously. Obviously, they should be executed in that sequence, hence the collection will be sorted accordingly.
Running a Postman Collection
In order to run a Postman Collection, you will need to use a feature called ‘Collection Runner’.
1. In the Postman GUI, in the top left corner of the screen, click the ‘Runner’ button.
2. Select the relevant Collection. In our case, it will be the one called ‘BlazeMeter API’.
3. There are additional configuration parameters that you may define. However, this is not mandatory. For example, you can specify the number of iterations you wish to run the collection for, as well as add delays between each API request. There is also an option to choose your ‘Environment’. Environments allow you to customize requests using variables that might include environment specific values. This is extremely helpful when you are testing against several environments, like the development environment, the production environment, etc. To set up a new environment, click on the gear icon on the top right side of the Postman GUI. Select ‘Manage Environments’. You will be able to add a new one as well as its respective variables.
How Postman helps you use APIs within your own app or script
Postman also has a feature called ‘Snippets’. You can use it generate code snippets in a variety of languages and frameworks, such as Java, Python, C, cURL and many others.
This is a huge time saver. A developer can easily integrate APIs with his or her own code without too much hassle. To use it, click on the ‘Code’ link below the ‘Save' button on the top right section of Postman’s GUI.
Below is an example for our 'Create Test API' in a python snippet.
You can now run API testing through BlazeMeter! Simply put your URL and assertion into the box below to start testing.
This blog post was originally published in December 2016, and was updated in March 2019 for accuracy.
Ready to get started with BlazeMeter? Sign up here.