Lesson 05 Test Script Creation
Lesson 05 Test Script Creation
John has been assigned a task to add some validations to REST APIs.
He is exploring the Postman tool's test script capability for this task.
After this session, John can add validations to the API responses and
update variables before sending the requests via Postman.
Learning Objectives
Postman has a powerful integration of script creation that is based on Node.js. Users can create API
tests, pass data between requests, and build requests with dynamic parameters. They can add
JavaScript code to execute these things. A script is run:
Test scripts run after receiving the response from the server. Various tests are accessible inside
the Postman Snippet.
Postman scripts allow changes in the behavior of the requests. Users can perform the following
tasks using the scripts:
Setting or obtaining variable values, parameters, headers, body data, and so on directly in the request
• Pre-request scripts are lines of code • Additionally, users can get a functional
that run Collection requests before the output, such as date, time, timestamp,
request is executed. and so on, by using the pre-request
script concept.
Example:
Users may need to generate any key in Example:
the URL or transmit some random An Environment variable can be set with
variables as a parameter. Pre-request the value returned from an internal
scripts are necessary here. Postman function to include a timestamp
in the request headers.
How to Create Quick Scripts?
Postman provides snippets to create basic scripts, such as getting a variable, setting a variable,
sending a request, clearing a variable value, and so on quickly.
Users can find the Snippets pane when moving to the pre-request Script or Tests tab.
How to Debug Scripts in Postman?
Debugging Scripts in Postman
Postman logs every request it sends in the console. It lets the users know the changes' details
when they send a request. The users can use the Postman console to help troubleshoot the
issue when an API is not behaving as it should.
It is best to keep the console open when debugging, as it will enable network calls and make log
messages more visible.
Debugging Scripts in Postman
The Postman
Network information, including IP addresses, ciphers, and
Console logs the
protocols
following data:
Users can implement different statements mentioned below to debug tests and check the
unexpected behaviors:
Scripts can carry out assertions on the response received and pass data between requests.
Advanced JavaScript codes can perform complex operations that are not readily available with snippets.
How to Create the First Test?
Users can create the first script in Postman by creating a Get request and then accessing the Tests tab
to write the JavaScript code and perform any validation.
How to Create Test Scripts in Postman?
Script Hierarchy in Postman