Postman is an excellent tool for testing APIs. It can be used by backend developers, web developers, mobile developers, and QA testers. Postman is not only used to test APIs manually but also is a great automation tool.
Writing Test Scripts
- Test Script: You can use the Test tab for writing test scripts. Your test scripts can use dynamic variables, carry out test assertions on response data, and pass data between requests. The test script will execute after the request runs. You will be able to see the output in the Test Results tab alongside the response data.
You can add pre-request and test scripts to a collection, a folder, a request within a collection, or a request not saved to a collection.
We will consider the following request and response while automating.
The API needs a different id(Integer), name(String), displayName(String) every time the request is triggered. Let us see how we can pass random values to these fields.
We can generate random/dynamic data in requests using the following functions/libraries.
Math.random(): This method returns a pseudorandom double greater than or equal to 0.0 and less than 1.0.
_.random (min,max)generates a random integer between a given range.
In the above script, we are first using
Math.random() to generate random strings viz. name and displayName. Next, we create an environment variable cargroupName and set its value to name and variable cargroupDisplayName and set its value to displayName. For setting the value of environment variable id we are using the function
$randomInt provided by
Alternatively, we can use
pm.environment.set(“id”,_.random(1000,10000)) for setting the value of environment variable id. Its returns a value between 1000 and 10000.
All the values set in environment variables will be used in the request body.
Before execution Pre-request script will run and the variable values will be set. These values will be used in the request body. Now let us see how the test script will look like.
We have written assertions using
chai.js. The above test script contains three tests defined by
pm.test(). We can also log onto the postman console using
console.log . Let's see how a failed test looks like
Now we want to read the created resource using a GET request and the id should be passed from the POST response. We simply need to put the id in the API endpoint since it's already set as an environment variable.
Here the after the response is received from GET request, it is updated and the priority field in the JSON is updated to 10. The updated JSON is saved to the environment variable to use in PUT Request.
Environment variable savedData can be used directly in the PUT request and you can see in the response that the priority field is changed to 10.
Again we can call the delete request with the environment variable id to perform the delete operation.
GET request to check the status after delete is performed. Here you can see that the status is changed to 0.
Postman allows us to run the entire collection. The APIs can be arranged in the required sequence and upon executing the collection the APIs will run in the defined order.
Here you can select which APIs to include, define the number of Iterations, save responses and other things. Click on Run Automation Example button to run the collection. Automation Example is the name of the collection.
Pros and Cons of Automation in Postman
- You do not need to create or maintain an automation framework.
- Easy to use, no code required to connect to REST client or generating reports.
- Code snippets provided makes script development fast.
- Test Scripts/pre-request scripts can not be reused and hence no code reusability.
- Cannot integrate with reporting frameworks such as allure, extent reports etc.
- Does not provide support to connect to a database or excel file.
- Difficult to test scripts on multiple data sets.
Postman is a great tool if you want to automate CRUD APIs for small microservices since it is simple and you do not need to create any framework.
Full projects and more repositories can be found on my github.