Home » Intro to Scripts

Intro to Scripts

Postman is not only used for manual testing, but we can also do automation testing of API. Postman requests and collections can be applied to dynamic behavior.

In Postman Scripts are the lines of code that allow you to automate an API test. It offers you to write pre-request and test scripts.

  • Before sending a request, a pre-request script will run and,
  • After receiving a response, test scripts will run.

In Postman, we can write the tests, pass the data between the requests, and change the parameters. It allows adding test script and pre-request script to a collection, a folder, a request, or a request not attached to a group.

Postman Sandbox

To write the script in Postman, we use Postman Sandbox.

Postman sandbox has been written in Javascript. It is an execution environment. Since the sandbox has written in Javascript, therefore, it will only receive a script written in Javascript.

Hence, we have to write the script in javascript to make the code executable in Postman.

Execution Order of Scripts in Postman

In Postman, the order of script execution for a single request is as follows:

Intro to Scripts

  • A pre-request script associated with a request will execute before sending the request.
  • Test script associated with a request will execute after sending the request.

The order of script execution for every request in a collection is as follows:

Intro to Scripts

  • Before every request in the collection, a pre-request script associated with a collection will run.
  • Before every request in the folder, a pre-request script associated with a folder will run.
  • After every request in the collection, a test script associated with a collection will run.
  • After a request in the folder, a test script associated with a folder will run.

The scripts should always run according to the following hierarchy for every request in a collection:

  • Collection level script
  • Folder level script
  • Request level script

The same order will be applied for both pre-request and test scripts.


You may also like