Explain the use of scripting languages like JavaScript in Postman tests.
Last Updated :
24 Jul, 2024
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see the use of scripting languages like JavaScript in Postman tests.
Prerequisites:
What are scripts?
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.
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.
Steps to use JavaScript with Postman Tests
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.

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.

We will write 2 snippets in Javascript from the already available snippets from the right-hand side pane.
First Test: 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 Test: 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"

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.

Output
Similar Reads
How to use Native JavaScript Promises in Postman ?
Postman is a popular tool used by developers for testing APIs. While Postman primarily offers a graphical user interface (GUI) for making HTTP requests, it also supports scripting capabilities using JavaScript. One common use case for scripting in Postman is making asynchronous requests and handling
3 min read
What is the scripting in Postman, and what languages are supported?
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
5 min read
Assertions in Postman and How to Use that in Scripting Window
Assertions are checks or validations we can include in our API requests to ensure that the response from the server meets certain criteria. These criteria could be based on the response's status code, headers, body content, or any other aspect of the response. Postman provides a variety of built-in
5 min read
Manage API data and workflows using Postman JavaScript objects
Managing API data and workflows efficiently is very important for developers. Postman, a popular API development environment, provides powerful tools to streamline this process. One such tool is Postman JavaScript objects, which allow you to manipulate data and automate workflows effectively. In thi
2 min read
Getting started with Scripting in the Postman
Postman is a powerful tool for testing and automating APIs, and one of its most valuable features is the ability to write and execute scripts. With scripting in Postman, you can add dynamic behavior, automate repetitive tasks, and extract data from responses. In this article, we'll go through the ba
6 min read
Explain the use of environment variables in Postman requests.
Postman is a powerful API development tool which offers a feature known as environment variables. These variables are used for efficiently testing and development of APIs by allowing users to manage dynamic values across requests easily. Table of Content What are Environment Variables in Postman?Adv
2 min read
How to use JavaScript in Postman ?
Postman is an API(application programming interface) development tool that helps to build, test and modify APIs. In this tutorial, we will see how to use JavaScript in Postman. Prerequisites:Basic HTTP conceptsKnowledge of REST APIWe will discuss the following two methods to use JavaScript in Postm
3 min read
How to Count Records in JSON array using JavaScript and Postman ?
In this article, we are going to explore how JavaScript and Postman are used to count the number of records returned in a JSON array. Send a Request: Send a request to fetch the JSON array from your API endpoint in Postman.Access JSON Response: In the Postman test scripts tab, you can access the JSO
2 min read
How To Speed Up Your API Tests Using The Advanced Features Of Postman?
The Postman is a popular tool for API development and Testing and It offers a range of features that can help you speed up your API tests, making your development process more efficient. In this article, we explain about advanced features of the Postman tool with related examples for your understand
3 min read
How to use postman for testing express application
Testing an Express app is very important to ensure its capability and reliability in different use cases. There are many options available like Thunder client, PAW, etc but we will use Postman here for the testing of the Express application. It provides a great user interface and numerous tools whic
3 min read