Variable scope and precedence in Postman - Build an Automation Script
// Pre-request Script pm.globals.set('var1', 'globalValue'); pm.environment.set('var1', 'envValue'); pm.collectionVariables.set('var1', 'collectionValue'); pm.variables.set('var1', 'localValue'); // Request URL example: https://example.com/api?param={{var1}} // Tests Script const varValue = pm.variables.get('var1'); console.log('Value of var1:', varValue); pm.test('Variable scope precedence test', function () { pm.expect(varValue).to.eql('localValue'); });
First, we set the variable 'var1' in all scopes: global, environment, collection, and local (Pre-request Script).
Local variables set with pm.variables.set() have the highest precedence and override others.
In the request URL, {{var1}} will resolve to the local variable value 'localValue'.
In the Tests script, we retrieve the variable using pm.variables.get('var1'), which respects the scope precedence and returns 'localValue'.
The assertion checks that the retrieved value matches the expected local variable value, confirming the precedence order.
Logging to the Postman Console helps verify the actual value during test execution.
Now add data-driven testing by setting 'var1' to three different values in local scope and verify the precedence each time.
Practice
Solution
Step 1: Understand Postman variable scopes
Postman has multiple variable scopes: local, environment, collection, and global.Step 2: Identify precedence order
Local variables override environment, collection, and global variables when accessing a variable.Final Answer:
Local variables -> Option DQuick Check:
Highest precedence = Local variables [OK]
- Confusing environment variables as highest
- Thinking global variables override local
- Ignoring local scope existence
Solution
Step 1: Recall methods to set variables in Postman
Local variables are set using pm.variables.set(). Environment, collection, and global variables use their respective methods.Step 2: Match method to local variable scope
pm.variables.set() sets a local variable for the current request or script.Final Answer:
pm.variables.set('varName', 'value'); -> Option AQuick Check:
Local variable set method = pm.variables.set() [OK]
- Using pm.environment.set() for local variables
- Confusing collection and global variable setters
- Forgetting to use pm.variables for local scope
pm.environment.set('token', 'envToken');pm.collectionVariables.set('token', 'colToken');pm.variables.set('token', 'localToken');What will
pm.variables.get('token') return?Solution
Step 1: Identify variable scopes and values
Environment variable 'token' = 'envToken', collection variable 'token' = 'colToken', local variable 'token' = 'localToken'.Step 2: Understand variable precedence when getting value
pm.variables.get() returns the local variable value if set, ignoring environment and collection variables.Final Answer:
'localToken' -> Option BQuick Check:
Local variable value returned by pm.variables.get() [OK]
- Assuming environment variable is returned
- Confusing collection variable with local
- Expecting null if multiple scopes exist
pm.environment.set('user', 'Alice');
pm.variables.set('user', 'Bob');
console.log(pm.environment.get('user'));But the console logs 'Alice' instead of 'Bob'. What is the issue?
Solution
Step 1: Analyze variable setting and getting methods
pm.environment.set() sets environment variable; pm.variables.set() sets local variable. But pm.environment.get() reads environment variable only.Step 2: Identify correct method to get local variable
To get local variable, use pm.variables.get(), not pm.environment.get().Final Answer:
pm.variables.get() should be used to get local variable -> Option CQuick Check:
Use matching get method for variable scope [OK]
- Using pm.environment.get() to read local variables
- Assuming set method affects all scopes
- Expecting pm.variables.set() to overwrite environment
pm.variables.get('count') and why?Solution
Step 1: Review variable scopes and precedence
Global variable 'count' = '5', collection variable 'count' = '10', local variable 'count' = '15'. Local variables override collection and global.Step 2: Determine which value pm.variables.get() returns
pm.variables.get() returns the local variable value if it exists, ignoring collection and global variables.Final Answer:
'15' because local variables have highest precedence -> Option AQuick Check:
Local variable value returned by pm.variables.get() [OK]
- Thinking global variables override local
- Assuming collection variables override local
- Believing variable conflicts cause null
Ready to prove it?
Pass the test and this pattern moves unknown
