Open In App

What is the scripting in Postman, and what languages are supported?

Last Updated : 25 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. It can make various types of HTTP requests(GET, POST, PUT, PATCH), save environments for later use, and convert the API to code for various languages(like JavaScript, and Python). In this article, we will learn What is the scripting in Postman, and what languages are supported.

Prerequisites

What is the scripting in Postman?

Scripts are the pieces of code that can be executed at a specific point of time in the Postman testing environment. There are two types of scripts –

  • Pre-request Scripts: As the name suggests, these scripts are used to execute JavaScript before the request runs.
  • Test Scripts: These scripts are executed during the testing environment.

Languages Supported?

Postman’s runtime is based on Node JS and lets you add dynamic behavior to requests and collections. You can use pre-request and test scripts in various languages like Javascript/NodeJS. Postman provides JavaScript APIs that you can use in your request scripts. The pm object provides functionality for testing your request and response data, with the postman object providing additional workflow control.

Use of scripting languages

1. Automated Testing: Postman enables you to write check scripts that execute after sending an API request. JavaScript, being the primary scripting language supported through Postman, enables the creation of computerized checks to affirm API responses, data integrity, and server conduct.

2. Response Validation: JavaScript scripting in Postman is extensively used to validate responses from API endpoints. Test scripts can parse JSON or XML responses, extract specific information, and carry out assertions to ensure the correctness of the received facts.

3. Environment and Data Manipulation: Postman enables using variables and environments. JavaScript lets in manipulation of those variables, permitting customers to set, update, or extract values dynamically. This is specially useful for managing authentication tokens, dynamic endpoints, and statistics-driven attempting out.

4. Custom Workflow and Logic: With JavaScript, users can introduce custom good judgment and workflows into their exams. This consists of conditional statements, loops, and custom abilties, permitting complex test situations that mimic real-worldwide use times.

Execution order of Scripts

The sequence is Pre-request Script, Request, and then Tests. This order ensures that any necessary modifications to the request are made before it is sent, and validations are performed after receiving the response.

1. Pre-request Script: This script runs before the actual request is sent. It allows you to modify the request before it is sent, such as setting variables or headers.

2. Request: The actual HTTP request is executed.

3. Tests: After the request is sent and a response is received, the test script is executed. This script is used to validate the response, perform assertions, and capture data for future requests.

Post-Request Scripts

In postman, we can write test scripts to test API requests in Javascript. These are called Post-request scripts. Test script is used to test whether your API is working accordingly or not, to establish that integrations between the services are functioning properly, and to check that new developments have not affected any functionality of existing requests.

Steps to Execute Post-Request Scripts in Postman:

Step 1: After downloading and installing the Postman, open the software. Add a new Collection and give it a name like “GFG”. Here, we can see, multiple tabs like Authorization, Pre-request scripts, Tests , Variables.

postman2

Step 2: Click on Tests. Now in the right side pane, we can multiple snippets arrived. We can use any of the available snippet or create our own code based on our requirement. Code is written in Javascript.

z165

We will write 2 snippets in Javascript from the already available snippets from the right-hand side pane.

First Script : This will check whether the response time is less then 200ms

pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

Second Script: This will check whether status code is 200

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

Step 3: Click on Save

Step 4: When you hover, on the name of your collection, 3 dots will appear. Click on those 3 dots, and then click on “Add new request”

z159

Step 5: Now You can simply paste the API in the space provided and select the API type you are requesting from the dropdown like GET,POST, PUT, DELETE etc. Output will be shown in the body with the status code.

API Used:

https://jsonplaceholder.typicode.com/posts/1

Step 6: You can see the Post-execution scripts results in tab Test Results; as shown below

z166

Output

Recording-2023-12-03-at-101007

Pre-request Scripts:

A pre-request script associated with a request will execute before the request is sent. You can write pre-request scripts for your Postman API requests in JavaScript. The Pre request tab allows for any pre- request processing before a request is sent.

Steps to use Javascript with pre-request scripts

Step 1: After downloading and installing the Postman, open the software. Add a new Collection and give it a name like “GFG”. Here, we can see, multiple tabs like Authorization, Pre-request scripts, Tests , Variables.

postman2

Step 2: Click on Pre-request scripts. Now in the right side pane, we can multiple snippets arrived. We can use any of the available snippet or create our own code based on our requirement. Code is written in Javascript.

z167

Step 3: We will add a pre-request script that will generate random numbers. We will use those random numbers to set a variable ‘id’ that is already stored as a global variable in our environment as shown below

z168

Pre-request Script:

var random=Math.floor(Math.random()*10);
pm.variables.set('id',random)

Step 4: Click on Save

Step 5: When you hover, on the name of your collection, 3 dots will appear. Click on those 3 dots, and then click on “Add new request”

z159

Step 6: Now You can simply paste the API in the space provided and select the API type you are requesting from the dropdown like GET,POST, PUT, DELETE etc. Output will be shown in the body with the status code. We use global variable in our request using {{id}}

API USED:

https://reqres.in/api/user/{{id}}

Output:

Recording-2023-12-13-at-114735



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads