API Testing 'A Beginners View': Environment and Variables In POSTMAN

In this post letā€™s try to have a better understanding of the Environment and Variables in POSTMAN

Letā€™s start with Variables: A variable is a bucket that holds values that can be changed. Suppose you are developing a mobile application. There are some values that will be used throughout the application or some values might be unique to a functionality. Hardcoding of values everywhere in the design is not a good practice in such situations variables can be used. Define a variable as per your need i.e. locally or globally and use its referenced name wherever needed. If you are required to update the value of a variable, just update the value where you have declared. Everywhere updated values will be used.

The same concept applies to Postman. We have many usages of variables in Postman. Letā€™s check out a few of them:

  1. In our day-to-day work life, we might need to test systems in different environments like QA, Dev, Stage, etc which would have a different set of domains. Here variables make our job easier. We can store the domain name as per environment in a variable and can call that variable here.
  2. We can use variables at several places to make it parameterized such as Query- parameters, headers, authorization, request body.
  3. Variables can be used to chain requests in the collection. Extracting data from response, storing, and using it for another request as input are possible by using variables.
  4. Variables can also be used in Pre-request and Tests Scripts.

Next, Letā€™s Understand Environment: Environment in POSTMAN is a set of variables that can be assigned a value and used during your work. The environment is tied to the current operating environment: each operating environment has its own POSTMAN environment. To visualize this, I have created an ā€œAPI Testing ā€˜A Beginners Viewā€™ā€ environment with some variables.

There are two columns for the variableā€™s value. The only difference is that if you share your environment with your team, then the value from the first column will be shared.

Letā€™s check out some usage of Environments:

  1. We can create, share, duplicate, export, and delete an environment.
  2. You can download it as JSON.
  3. Variables defined in an Environment are Local variables. It means you cannot use variables of an environment in another environment. To solve this we have another type of variable called Global variables . Global variables do not belong to specific environments. It is available in all scopes. You can not select multiple environments at the same time. In that case, Global variables will be helpful.

Since that now we know what is variable and environment, Letā€™s understand how to create and manage them;

Steps to create an Environment and define the variable in it:

  1. Click on the ā€œManage Environmentā€ icon at the right top corner of the POSTMAN tool

  1. You will see the below screen:

  1. Click on the ā€œAddā€ button. You will see the below screen:

  1. Give an environment name in the ā€œEnvironment Nameā€ folder. Add a new variable and pass the initial value. You will notice that the same value given in the Initial value will be copied in the ā€œCurrent valueā€ field. The ā€œCurrent valueā€ field keeps the current value of the variable.
  2. Click on the ā€œAddā€ button. You will see an environment created with named ā€œTestā€.

  1. Select a created environment in the dropdown in the workspace and click on the ā€˜Eye iconā€™ near the environment. It will show you added variable details.

Accessing variable of Environment: Now letā€™s see how to use the defined variable in our requests. We just need to use the variable name as placeholder {{variableName}} This placeholder will be replaced with its current value. For example: ā€“ {{Link}} will be replaced by ā€œhttps://postman-echo.com ā€ from the environment ā€œTestā€.

Points to note:-

  • Variable declared in an Environment is local to that environment.
  • Multiple environments cannot be selected at the same time.
  • Duplicate variables cannot be defined in an environment but it can be defined in another environment.

Letā€™s see some more interesting facts on these variables: Until now we saw how manually we can add variables with value. Sometimes we may need to update or add variables at runtime. For example:- If I want to get the response value and store in a variable while the request is hit. This needs to be done at runtime only. We use a global function named ā€œpm.*ā€ of the Postman Sandbox environment. We can do it in both Pre-request Script and Tests Scripts. Below I have shared the syntax and example of setting, getting, and clearing variables at runtime in Tests Scripts.

Syntax:

Managing environment variable using scripts:

//Setting an environment variable:

pm.environment.set(ā€œvariable_nameā€, ā€œvariable_valueā€);

//Getting an environment variable:

pm.environment.get(ā€œvariable_nameā€);

//Clear or unset an environment variable:

pm.environment.unset("variable_name ");

Managing global variable using scripts:

//Setting a global variable:

pm.globals.set(ā€œvariable_nameā€);

//Getting a global variable:

pm.globals.get(ā€œvariable_nameā€);

//Clear or unset a global variable:

pm.globals.unset(ā€œvariable_nameā€);

Example:

Response Body:

[ { ā€œidā€: 1, ā€œfirst_nameā€: ā€œCaryā€, ā€œlast_nameā€: ā€œWentworthā€, ā€œemailā€: ā€œcwentworth0@netscape.comā€, ā€œgenderā€: ā€œMaleā€, ā€œip_addressā€: ā€œ173.152.146.25ā€ } ]

Test Scripts:

var resp = JSON.parse(responseBody);

console.log(resp);

//Setting an environment variable:

pm.environment.set(ā€œfirst_nameā€, resp[0].first_name);

//Getting an environment variable:

console.log(pm.environment.get(ā€œfirst_nameā€));

// //Clear or unset an environment variable:

pm.environment.unset(ā€œUnshift_Valuesā€);

// //Setting a global variable:

pm.globals.set(ā€œip_addressā€, resp[0].ip_address);

// //Getting a global variable:

console.log(pm.globals.get(ā€œip_addressā€));

// //Clear or unset a global variable:

pm.globals.unset(ā€œnowā€);

Screenshot:

Hope I shared something of use to the beginners like me out there :innocent:

Happy Learning!!! :innocent:

4 Likes