Postman Sandbox environment contains many APIs which makes it easier to write scripts for requests in Postman.
pm : Object
Postman sandbox has a global function called “pm”. This
pm object keeps track of all information related to the script being executed. We can also access information of request being sent and response received using pm object. pm contains some objects in it, which can be used for different purposes:-
In this post , we will see about pm.info in this post.
Using “pm.info” we can get useful information such as the request name, request Id, iteration count , scheduled iteration count etc. which are stored inside of this object.
To get the type of script being executed:-
We know that we can have two types of scripts in Postman:- Pre-request and Tests. If we need to know which script type is currently running, we can use “eventName” property of pm.info object. It is helpful to add or implement logic based on type of script currently running. We can print value in Postman Console. It returns a String.
Example:- We add a console.log() statement in both Pre-request Script and Tests as shown below:-
Postman Console:- We know that first Pre-request script runs followed by request and tests. Observe the output in console below:-
To get the count of iteration set:-
We can schedule/set count of iteration of request/s to be run. If we are running without Collection runner, default value of iteration is set to 1. We can set desired count of iteration using collection runner.
If we need to know the count of iteration scheduled to run, we can use “iterationCount” of pm.info object. It returns a whole number. It will return 1 if we are not running using Collection runner. If we are running using collection runner, it will return the number of iteration set.
Without Collection Runner:-
With Collection Runner:-
Set the iteration count to any number say 3.
Postman Console:- Collection will be run three times.
To get the currently running iteration:-
Above, we saw that how can we get total count of iteration set to run. If we need to do some different stuff based on iteration, we need to know which iteration is currently running.
We can get the currently running iteration using “iteration” of pm.info. It returns a number starting from zero. It means for first iteration, it will give 0, 1 for second iteration and so on.
Set iteration count as 3 and run the collection:-
To get the name of currently executing request:-
This is a frequently asked interview question. We can get the name of currently running request using “requestName” of pm.info. It returns a string.
To get the id of currently executing request:-
We give the name to the request but an unique auto generated id is also assigned to each request. We can get that ID as well. We need to use “requestId” of pm.info object. It returns a string.
As of now I have never seen specific use of request ID but will update here if I get to know.
More about API Testing and Postman Tutorials in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.