In this post, we will do a quick introduction to Postman Scripts. Learning scripting will give you the power to automate your API collections, so it’s really important you understand how Scripting works if you want to do API automation using Postman.
What are Postman Scripts?
Postman Scripting allows you to add dynamic behavior to requests and collections by executing JavaScript code. Essentially, what you are doing is using JavaScript code to manipulate your API requests.
There are 2 types of scripting you can do with Postman –
- Pre-requests scripts
- Test scripts
Pre-request Scripts – manipulate the data of the request before Postman sends the request
Test Scripts – validate the response of the request by writing tests (gets executed after Postman receives the response)
What can you do with the Scripts?
Postman Scripts allow you to change the behavior of the requests, let’s understand what that means. Using Scripts you can do some of the following:
- Set or Get the variable values / parameters / headers / body data etc… in the request directly
- Debugging (log output to the console)
Let’s take a look at an example –
![image 3](https://sdetunicorns.com/wp-content/uploads/2020/12/image-3.png)
Let’s assume you are creating a user using the POST request and then you need to fetch the id of that user in your GET request. Instead of manually copy/pasting the id, you can take advantage of scripts to do that for you.
First, you can set a variable called ‘id’ in the Tests script of the POST request. Then, get the variable in the Pre-request script of the GET request and set it in the parameter.
This allows us to automate this flow using Postman collection without having to worry about relying on existing data.
Check out the video below to learn more about Postman Scripts –
To learn about API Test Automation using JavaScript, check out my free tutorial series here –
JavaScript API Test Automation Tutorial Series
I hope this post helped you out, let me know if you have any questions in the comments below!
Happy testing!