Postman Tutorial Part 21 – Scripts In Postman

If you think Postman is only for static or manual testing, you are not correct. We can do automation testing of API using postman to a good extent. We can add dynamic behavior to Postman requests and collections.

Postman contains a NodeJS based runtime environment. This NodeJS environments allows us to add dynamic behaviour to Postman requests and collections.

Postman provides two events where we can write JavaScript codes to execute in the flow:-

  1. Pre-request Script :- It is executed before a request is sent to server or end points.

2. Test Script :- It is executed once response is received from server.

You can add pre-request and test scripts to a:

  1. Collection
  2. Folder
  3. A request within a collection
  4. A request not saved to a collection.

Execution order of scripts:-

As we know now that Pre-request script will be executed before request is hit to server and tests script will be executed once response is received.

When we create a Collection we can add Pre-request and test scripts in it.

When we create a folder within a Collection, we can add Pre-request and test scripts in it at folder level as well.

When we add a request in a collection or in a folder of collection, we can add Pre-request and test scripts in it at particular request level as well.

As per official document of Postman:-

For every request in a collection, scripts will execute in the following order:

  • A pre-request script associated with a collection will run prior to every request in the collection.
  • A pre-request script associated with a folder will run prior to every request in the folder.
  • A test script associated with a collection will run after every request in the collection.
  • A test script associated with a folder will run after after request in the folder.
workflow for request in collection

We will see above execution flow in details with proper examples 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 *