Make Selenium Easy

Postman Tutorial Part 47 – Local or Temporary Variables in Postman

We have seen several types of variables in Postman already in previous posts. In this post, we will learn about Local or Temporary variable in Postman.

A Local or temporary variable is created in to memory only during run time and removed once execution is done. As scope of local variable is limited to duration of execution, we can only create and update it in Scripts of Postman.

We can use local variables for below scenarios:-

  1. Temporarily store a value to be used/reused in other calculation/requests.
  2. Printing/Debugging the request execution
  3. Override the global, environment, data and collection variables value as local variable will have highest priority always.

Creating local variables in Postman:-

We can create local variables in Postman in two ways:-

  1. Using JavaScript way ( var or let)
  2. Using pm sandbox APIs

Using JavaScript :-

See the JavaScript syntax below:-

This way is good if we want to store some variable temporary. Like you would like to add prices of all books. You need to have some variable to store current addition. You can create local variables in Pre-request Script and Tests.

There is an important points about local variables created through JavaScript. You can not use this variable outside of current scope. It means if we create a local variable in “Pre-request Script” of a request, we can not reuse it in Tests tab of same request and in Pre-request and Tests script of any other request and vice – versa. It will give reference error. It restricts scope of local variable again.

Try to use local variable created named “localVar” in Tests tab:-

ReferenceError:-

Using Postman Sandbox api:-

If we want to have some temporary variable which can be used within collection or shared temporary variables across all request of collection, we can go for this way.

Syntax to create local variable using postman sandbox APIs:-

pm.variables.set(“<varName>”,”<varvalue>”)

Syntax to get value of created local variable:-

pm.variables.get(“<varName>” )

Unlike JavaScript way of creating local variable, postman sandbox API way gives wider scope to local variable. We can use these local variables within collection.

We can use this local variable in other request as well. I just made a copy of above request.

But remember, if local variables are shared with another request, you must need to run using Collection runner. Otherwise it will give you undefined value not reference error like JavaScript way.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Postman Tutorial Part 47 – Local or Temporary Variables in Postman

by Amod Mahajan time to read: 2 min
0
%d bloggers like this: