Postman Tutorial Part 29 -Postman Sandbox API reference – Global functions (pm.*) – pm.info Object

We can write scripts in Pre-request and Tests sections of a request in Postman. These scripts are run in a JavaScript execution environment called Postman Sandbox.

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:-

  1. pm.info
  2. pm.sendRequest
  3. pm.variables
  4. pm.environment
  5. pm.globals
  6. pm.request
  7. pm.response
  8. pm.iterationData
  9. pm.cookies
  10. pm.test
  11. pm.expect

In this post , we will see about pm.info in this post.

pm.info: Object

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:-

Postman Console:-

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.

Postman Console:-

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.



Postman Console:-

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.
#ThanksForReading
#HappyApiTesting

1 thought on “Postman Tutorial Part 29 -Postman Sandbox API reference – Global functions (pm.*) – pm.info Object”

Leave a Reply

Your email address will not be published. Required fields are marked *