In the last tutorial we learnt about the testing process in Postman. We also learnt about the collection runner and how we can use it to test many request at the same time. Now with this tutorial we will move ahead to Environment Variables in Postman without which sending requests becomes a very hectic and time consuming tasks.
In this tutorial, we will follow the following topics
- Environment and Environment Variables
- Local Environment Variables
- Scope of Variables
- Global Variables
- Precedence of variables in Postman
Problem Statement
We often encounter different servers in our company or team. These can be either development server, a production server or maybe a testing server. Every server has different types of request APIs. Since we know that a collection can include many requests within it, what if the URL changes? For example, they change their server request URL. If the team managing the server changes the request API and inform us, we have a lot of trouble in front of us. For running the requests successfully now we have to make the changes to each and every request. For 300 requests, we will have to change 300 times. This is purely a waste of time and resources. But since this happens a lot, Postman has a feature to deal with this in a few seconds and we will be good to go to use the requests again.
What is an Environment in Postman?
An environment in Postman is a set of key-value pairs. An environment helps us to differentiate between the requests. When we create an environment inside Postman, we can change the value of the key value pairs and the changes are reflected in our requests. An environment just provides boundaries to variables. When we create different environment we can make track of all the variables and how to use them inside our requests. There can be many variables inside one environment. At once, we can work only in one environment although we can create any number of environments in Postman. The below screenshot shows three environments that we created.
What is an Environment Variable in Postman?
A variable in the Postman is same as in any programming language. A variable is an entity whose value can be changed. The key part in the key-value set in environment is called variable. This variable can have any value and in place of the key we can use the variable name in every request. This will be clear with an example shown below and steps shown thereafter.
The above image shows three variables inside environment Test Environment 1
Environment Variables in Postman
Now, we will use Postman to create an Environment and Environment Variable and it is very easy to do that but it has three steps involved in the process:
- Create an Environment
- Create Environment Variables
- Use an Environment Variable in the Request
Step 1: How to Create an Environment in Postman
1.Create a new Collection and name it as EnvironmentChapter.
- Add Weather Api Request in the collection used in the Get Request chapter.
- Click on the gear icon which says Manage Environment.
- Click on Add.
- Name the environment as Weather API
Step 2: How to Create Environment Variables in Postman
1.Now in the same window, enter the following key-value pair. Where Key is the name of the variable and Value is the text string.
Key : URL
Value : http://restapi.demoqa.com
Click on Add and close the panel.
Step 3: How to Use Environment Variables in Postman
- Select the dropdown which says No Environment and select Weather API environment in that.
Now we can access all the variables of this environment.
- In the address bar change http://restapi.demoqa.com to {{url}}
- Click on Send.
Now, we have created an environment and used a variable called URL here. This variable can be now used instead of the actual URL. You can see the response which is same as before we were using the full URL.
So, if by any chance the URL changes, we can just go to environment and change the URL value and it will be reflected in every request.
NOTE: Remember to save the request by clicking Save button. In the future chapters, we will use this modified request only.
Scope of Variables in Postman
A scope of anything is the boundary in which that thing can be accessed and perform. For example, if you are an engineer and do not have a passport, your scope is limited to India since you cannot go outside. While having a passport changes your scope to the world. Similarly, variables in Postman have two scopes
- Local Scope
- Global Scope
Local Scope
Local Scope Variables can only work inside the environment in which it was created. Changing the environment will stop the access to that variable and we will encounter an error.
The variable URL that we just created above is the local variable because it has a scope only till the environment Weather API. In the following steps we will explore the limitations of local variable by accessing local variable in other environment, where it is not present.
1.Go to the dropdown where we selected Weather API and select any other value (if you have) or No Environment.
- Click Send.
This error occured because Postman does not know about URL variable because we have changed the environment. Therefore, URL is a local variable having scope only till the Weather API environment.
Global Scope
Global Scope Variables can work outside the environment also. They are global and it does not matter which environment is selected. In the following image you can see three global variables by clicking the Eye icon.
Global Variables in Postman
Now, we will use Postman to create Global Variables.
Create an Environment: Just because global variables are not associated with any particular environment, there is no need to create an environment for global variables.
- Create Global Variables
- Use Global Variable in the Request
Step 1: How to create a Global Variable in Postman
1.Go to the same gear icon to open the environment panel which we did at the time of creating Local Variable.
- Select Globals to add a global variable.
- Add the following key-value pair
Key : URL
Value : http://restapi.demoqa.com
- Save and close the panel.
Step 2: How to use Global Variable in Postman Request
1.The request which we created above, just select the No environment from the environment dropdown.
- Press Send and now see the result.
It works now because we have created a global variable which can be used with every environment.
NOTE: Global scope cannot have duplicate/same names while variables having local scope can have the same name in different environments.
For convenience Postman also has a feature which lets you see all the current variables and environment. Just click on Eye icon and it list down all the Environments and Global variables.
and you can see the global variable under Globals written. We have not selected any environment therefore there is no information about the environment. You can try it out yourself.
Precedence in Variables
As we discussed, two global variables cannot have same name while two local variables can have same name provided they are in different environments. But what if one local variable and one global variable has same name? For example, you name a local variable ABC and a global variable ABC. Now when you select that respective environment both the variables will be activated. So, which will show its value? This confusion is solved by precedence.
Precedence generally means priority. While two or more things strike together, the one with higher priority (precedence) is preferred. In Postman for same name of environment specific variable and global variable, environment specific variable or local variable has higher precedence. It will overwrite the global one.
1.Now in the dropdown panel select Weather API instead of No Environment
Now we have two variables of same name accessible. One in Weather API environment and one which is global.
- Click on Eye icon to have a look
Here we have a problem, both the variables have same values. But if you look in the image above, global URL has been sliced off with a line. This has happened because both the variables have the same name and the precedence will be given to the local variable so global variable will not be used.
3.Go to the Manage Environment (gear icon) and click on Weather API environment
- Change the url value to anything you like. Here we have changed it to anonymous.
- Close the panel and look at the current environments again by selecting the Eye icon
Both the variables are now accessible and can be used. If you press send now, you will get the correct response from global variable which you would get from local variable if they had same name. This is how precedence works.
This is all from the Environment and variable chapter. Now you can use Postman efficiently and manage request like a professional. We will move on to the next chapter now.