Interview #105: Postman - What are environmental variables?
Software Testing Studio | WhatsApp 91-9606623245
Looking for Job change? WhatsApp 91-9606623245
Postman is a powerful API testing tool that allows users to create, test, and automate API requests. One of its key features is environment variables, which help manage dynamic values in API requests efficiently.
Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-9606623245
Environment variables are key-value pairs that can be used across multiple requests, collections, and test scripts in Postman. They enable flexibility, reduce duplication, and facilitate testing in different environments (e.g., development, staging, production) without manually changing request details.
1. Why Use Environment Variables in Postman?
Using environment variables in Postman offers several benefits:
? Dynamic Value Management – Easily change values such as API URLs, authentication tokens, and request parameters.
? Multiple Environments – Seamlessly switch between development, staging, and production environments.
? Reduce Hardcoding – Avoid hardcoding API keys, credentials, and other sensitive information.
? Improved Collaboration – Share environment variables with teams while keeping sensitive data secure.
? Enhanced Automation – Automate tests with dynamic variables in Postman scripts.
2. Types of Variables in Postman
Postman provides different types of variables to suit various use cases:
a) Environment Variables
baseUrl = https://api.dev.example.com
token = 12345xyz
{{baseUrl}}/users
Authorization: Bearer {{token}}
b) Global Variables
globalTimeout = 5000
pm.globals.set("globalTimeout", "5000");
c) Collection Variables
pm.collectionVariables.set("collectionVar", "value");
d) Local Variables
var tempVar = "temporary";
console.log(tempVar);
e) Data Variables (from CSV/JSON Files)
3. How to Create and Use Environment Variables in Postman
Step 1: Creating an Environment
Step 2: Using Environment Variables in Requests
GET {{baseUrl}}/users
Step 3: Setting Variables Dynamically in Scripts
a) Pre-Request Script (Before Request Execution)
pm.environment.set("authToken", "Bearer " + pm.variables.replaceIn("{{token}}"));
b) Test Script (After Request Execution)
let response = pm.response.json();
pm.environment.set("userId", response.id);
4. Managing and Overriding Variables
Variable Scope Hierarchy
If a variable exists in multiple scopes, Postman follows this priority order (highest to lowest):
For example, if both global and environment variables have the same name, the environment variable will take precedence.
5. Best Practices for Using Environment Variables
? Use Separate Environments – Define different environments (Dev, Staging, Production) with appropriate values.
? Do Not Store Sensitive Data in Global Variables – Use environment variables instead of global ones for API keys and tokens.
? Use Dynamic Variable Setting in Scripts – Store response values dynamically for better automation.
? Organize Variables Properly – Use clear naming conventions (baseUrl, authToken) for easy identification.
? Leverage Postman Workspaces – Share environment variables securely with team members.
6. Real-World Example of Environment Variables
Scenario: Testing an API with Different Environments
Environments
Request URL in Postman
GET {{baseUrl}}/users
Authorization: Bearer {{authToken}}
7. Automating Tests Using Postman Environment Variables
Example: Automating User Creation and Retrieval
Step 1: Create a User
POST {{baseUrl}}/users
Content-Type: application/json
Body:
{
"name": "John Doe",
"email": "[email protected]"
}
Step 2: Capture User ID in Test Script
let response = pm.response.json();
pm.environment.set("userId", response.id);
Step 3: Retrieve User Details Using Stored userId
GET {{baseUrl}}/users/{{userId}}
This automation eliminates manual intervention and enhances testing efficiency.
Conclusion
Environment variables in Postman provide a powerful, flexible, and reusable approach for managing API requests efficiently. They enhance automation, reduce redundancy, and enable seamless testing across different environments. By leveraging environment variables correctly, testers and developers can optimize their API testing workflow and ensure smooth integrations across multiple environments. ??