API Testing Tutorial Part 35 – 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:-

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

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.
%d bloggers like this: