Postman Tutorial Part 23 – Introduction to Postman Sandbox

We know that we can write Pre-request scripts and Tests scripts for a request, collection and folder in a collection to add dynamic behaviour to it. All these scripts run in an environment which is called Postman Sandbox.

The Postman Sandbox is a JavaScript execution environment based on NodeJS that is available to you while writing pre-request and test scripts for requests both in Postman and Newman. Do not worry about newman now. We will see that in upcoming posts. Whatever code you write in these sections is executed in this sandbox.  

Postman Sandbox has many built in JavaScript libraries which we can use while writing scripts. It makes Postman a very powerful tool to write automated scripts for API. Below is just a list of built in libraries in Postman Sandbox:-

Postman sandbox also consists of some NodeJs modules:-

You need to use require() function to use the sandbox built-in library modules. But to use it properly you need to have basic ideas of JavaScript and NodeJS. You can find usage of built in libraries and references here.

Postman’s PM API:-

Postman’s PM API (pm.*) or pm object is replacing “postman.*”. By using “pm.*” , We can do more with scripting, like easily access request and response elements, assert a set of pre-defined rules for testing to enable better and cleaner tests, and manage environments and variables. You can find all methods available in pm apo here.

When we write tests under “Tests” tab we see a test code like below:-

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

Now you can understand that “pm” is a global object in Postman which has a method called “test” which has below syntax:

pm.test(testName:String, specFunction:Function):Function

This function is used to write test specifications inside the sandbox. Writing tests inside this function allows one to name the test accurately as well as ensure that in case of any errors inside this function, the rest of the script is not blocked.

We will see some basics functions of pm in upcoming posts.

More about API Testing 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

Leave a Reply

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