How Pre-requisite scripts in Postman Help to Test API faster
By Aman Kumar, CoffeeBeans consulting
What is Postman?
Postman is an application used for API testing. It is an HTTP client that tests HTTP requests through which we obtain different types of responses that need to be subsequently validated.
It can help the backend developer to test the API without UI and it offers all the HTTP methods to test the endpoint.
What are Pre-requisite Scripts in Postman?
Pre-requisite scripts are the JavaScript code that will run before the execution of a request using which can make the request dynamic.
For Example: Suppose you have one Authentication API which gives you a Bearer token and that Bearer token is used by other requests in the collection.
Code:
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable(“token”, jsonData.token);
This code in the Pre-request script will fetch the token from the Authentication API response and store it in a variable named ‘token’ in Environment, so the token will be available to all the APIs.
Now without Pre-requisite Script, one needs to first run the Authentication API, then copy the Bearer token from the response to all APIs in a collection which is a tedious task. Suppose you have hundreds of APIs in a collection, then you need to copy the token to all APIs.
How Pre-request Script helps us?
With Pre-requisite Script, we can write the logic to store the Bearer token value in some variable and this variable can be used directly in all the APIs or Postman Environment.
Syntax for using the variable values is: {{variable_name}} Here variable_name is defined in Pre-request Script.
pm.variables.set(‘variable_name’,’variable_value’)
We can apply the pre-request scripts at a request level, a collection level, or a folder level, which means that a pre-request script will apply to all the requests that are part of that collection or folder.
- A pre-request script associated with a collection will run before any request in the collection.
- A pre-request script of a folder will run before any request in the folder.
Pre-requisite Scripts can be used for a pre-processing task like setting parameters, body data, headers. It can also be used for debugging purposes like logging the timestamp or correlation ID to trace the request by using console.log() in the pre-request script.
Execution flow with Collection Level Scripts -
Steps to Setup Pre-requisite Script at Collection Level
Suppose we want to have application/json as Content-Type in the header of every request of collection. Then we can do that simply by right-clicking on Collection > Edit > Pre-Request Script
Add the following lines of code and update.
pm.request.headers.add({
key :'Content-Type',
value : 'application/json'
})
In the console, we can see that Content-Type: ”application/json” is added in the Request Header. Likewise, we can add multiple values to the request dynamically.
We can also write the pre-request script at an individual request level.
Here the {{name}} will be substituted by ‘michael’ dynamically when the request is being sent.
Note: In Collection level Pre-request Script, add only those values which can be applicable to all the APIs in that collection.
Steps to Setup Post-requisite Script at Collection Level
In Post Request Script, we can validate the response received from API and assert it based on the expected result.
Steps to Setup Post Request Script
- Open the request and go to the Tests tab.
- Write the JavaScript code for assertion based on the expected result.
- In the snippet above, the code is checking for status 200. If the status of the response is 200 then the test will pass. If not, it will fail.
Here the assertion fails as the code is modified to validate the status as 201 but the actual status is 200.
This is the reason why scripting in postman can help to reduce the manual effort to a great extent and also makes the developer or tester’s life, a breeze.