In this presentation, you will get the knowledge about scripting in Postman. Apart from this, you will get the execution order & different terminologies. Furthermore, you will read different methods of request and response on Postman object & how to implement pre-scripts and post scripts on Postman.
2. Points to discuss
● Scripting in Postman
● Execution order of scripts
● Hierarchy of scripts
● Script globals, Scope
● Cred operations for local scope
● Cred operations for global scope
● Cred operations for collections scope
● Cred operations for env scope
● Dynamic variables & debugging
● Scripting with request and response data
3. Scripting in Postman
Scripting in Postman will help you to automate the process to test the output of
given REST API, HTTP API or SOAP API.
● Postman supports a powerful runtime platform “Node.js” that allows you to add dynamic behavior to
requests and collections.
● This allows you to write test suites, build requests that can contain dynamic parameters, pass data
between requests, and a lot more.
● We can add scripts to pre-request scripts tab and tests tab under request.
4. Execution order of scripts
As discussed, we can add script to automate testing in Pre-request script & tests tabs of request window.
Pre-request script associated with any request in postman
● Always execute before request is sent.
● Perform the operation and add variable or env variable to request if any.
Test script associated with any request in postman
● Always execute after request is sent.
5. Hierarchy of scripts
Pre-request script & tests script can be add into 3 levels.
Collections level
● A pre-request script associated with a collection will run prior to every request in the collection.
● A test script associated with a collection will run after every request in the collection.
Folder level
● A pre-request script associated with a folder will run prior to every request in the folder.
● A test script associated with a folder will run after every request in the folder.
Request level
● A pre-request script associated with a request will run before every request is sent.
● A test script associated with a request will run after to every request is sent.
6. Script globals, Scope
pm object
It is postman object which provides to functionality for testing your request and response data. The pm
object provides methods for accessing global, collection, and environment variables specifically, and
pm.variables methods for accessing variables at different scopes as well as setting local variables.
pm.variables: dictionary or associative variable of pm object for local scope
It helps to store variables with its value within request scope & access in request scope.
pm.globals: dictionary or associative variable of pm object for global scope
It helps to store variables with its value. This scope will use when it needs to pass variables to other
requests.
7. Script globals, Scope
pm.collectionVariables: dictionary or associative variable of pm object for collections scope
It is good alternative to global or environment variables and store credentials & pass to requests within
postman collection.
pm.environment: dictionary or associative variable of pm object for environment specific
Environment variables are tied to the selected environment(test, dev or production). It is good alternative
to global variables as they have a narrower scope. It can be used to storing environment specific
information, URLs, authentication credentials & passing data to other requests.
pm.iterationData: dictionary or associative variable of pm object for iteration only
It can be used during execution of iteration or multiple data-sets are needed.
8. Cred operation for local scope
1. Set or create variable to local scope or request scope itself: in this case, we will set scope named
variable with its value…
pm.variables.set(“scope”,”request”)
To store variable, it needs to provide key & value pair.
1. Get or read value of specific variable:
pm.variables.get(“scope”)
1. Check specific variable exists in local scope
pm.variables.has(“scope”)
Return value can be true or false
1. Delete specific variable from local scope
pm.variables.unset(“scope”)
9. Cred operation for Global scope
1. Set or create variable to global scope: in this case, we will set scope named variable with its value…
pm.globals.set(“scope”,”global”)
To store variable, it needs to provide key & value pair.
1. Get or read value of specific variable:
pm.globals.get(“scope”)
1. Check specific variable exists in global scope
pm.globals.has(“scope”)
Return value can be true or false
1. Delete specific variable from global scope
pm.globals.unset(“scope”)
1. Delete all variable from global scope
pm.globals.clear()
10. Cred operation for Collections scope
1. Set or create variable to collection(can be access in all requests of specific collections) scope: in this
case, we will set scope named variable with its value…
pm.collectionVariables.set(“scope”,”collection”)
To store variable, it needs to provide key & value pair.
1. Get or read value of specific variable:
pm.collectionVariables.get(“scope”)
1. Check specific variable exists in collection scope
pm.collectionVariables.has(“scope”)
Return value can be true or false
1. Delete specific variable from collection scope
pm.collectionVariables.unset(“scope”)
11. Cred operation for environment scope
1. Set or create variable to environment(can be access in all requests of specific environment) scope: in
this case, we will set scope named variable with its value…
pm.environment.set(“scope”,”env”)
To store variable, it needs to provide key & value pair.
1. Get or read value of specific variable:
pm.environment.get(“scope”)
1. Check specific variable exists in environment scope
pm.environment.has(“scope”)
Return value can be true or false
1. Delete specific variable from environment scope
pm.environment.unset(“scope”)
1. Delete all variable from global scope
pm.environment.clear()
12. Dynamic variables & Debugging
Postman provides the faker library to generate dynamic dummy data. We can generate random names,
addresses, email addresses, and much more. You can use these pre-defined variables multiple times to
return different values per request.
Dynamic variables’ values are generated at the time of execution and their names start with a $ symbol e.g.
$guid, $timestamp etc.
To use dynamic variables in pre-request or test scripts, you need to use pm.variables.replaceIn(), e.g.
pm.variables.replaceIn('{{$randomFirstName}}').
Common variables are:
$guid: A uuid-v4 style guid
$timestamp: The current UNIX timestamp in seconds
$isoTimestamp: The current ISO timestamp at zero UTC
$randomUUID: A random 36-character UUID
More reference: https://learning.postman.com/docs/writing-scripts/script-references/variables-list/
13. Dynamic variables & Debugging Continued...
To debugging in pre-request script or tests tab just use console.log. Let's take some examples of some code
snippets
1. To check variable in exists in local scope
console.log(pm.variables.has(“scope”)) //output will be true
1. To get the value of scope variable in local scope
console.log(pm.variables.get(“scope”)) // output will be request
Let’s define one variable with local scope and debugging value:
pm.variables.set(“scope”,”request”);
console.log(pm.variables.get(“scope”)) // output will be request
Let’s update the value of defined variable of local scope
pm.variables.set(“scope”,”updated request”);
console.log(pm.variables.get(“scope”)) // output will be updated request
Note: to check console window go to view menu from top→ show console
14. Scripting with request and response data
To send the request or get the response data or process the response data, postman scripts provides a
variety of methods like
pm.request : it provides the pre-defined variables & methods to send or perform ops on postman request
Properties & methods of pm.request
● pm.request.url : request url
● pm.request.headers: list of request headers which stores various data in key, value regarding the
requests.
● pm.request.method : HTTP request like PUT, GET, DELETE, POST, HEAD etc.
pm.response: it provides the pre-defined variables & method to work or get the data of returned data of
sent request.
Properties & methods of pm.response
● pm.response.code : HTTP response code which can be in range of 100-599
15. Scripting with request and response data
Variables of pm.response
● pm.response.status : HTTP response text string
● pm.response.headers: list of headers returned by sent request
● pm.response.responseTime : The time the response took to receive in milliseconds
● pm.response.responseSize : The size of the response received
● pm.response.text() : Get the text of response
● pm.response.json() : Get the text of response in json format
pm.info : it provides the information related to request including event name, ID, iteration count etc.
Properties & methods of pm.info
● pm.request.eventName: the event which can be pre-request or tests depending on where the script is
executing within the request.
● pm.info.iteration : The total number of iterations that are scheduled to run
● pm.info.requestName: The saved name of the request running
● pm.info.requestId: A unique id of request in GUID format
16. Scripting with request and response data
pm.cookies : A objects provide the access the list of cookies associated with request.
Properties & methods of pm.cookies
● pm.cookies.has(cookiename): to check whether the cookie exists in request or not. Just provide the
name cookie as parameter in method.
● pm.cookies.get(cookiename) : To get the value of specified cookie as parameter
● pm.cookies.toObject(): To get a copy of cookies with values in object format.
● pm.cookies.jar() : To get or set cookies with specific domain name and associate with request.
Example:
const jar = pm.cookies.jar()
jar.set("https://reqres.in", "session-id", "abc123", (error, cookie) => {
if (error) {
console.error(`An error occurred: ${error}`);
} else {
console.log(`Cookie saved: ${cookie}`);
}
});
(Note: To work with cookies jar, need to add domain to whitelisted domains under Cookies)
17. Thank you
For more details,
Mail us @ training@itlearn360.com
Call us @ +1-800-543-5571
https://www.itlearn360.com