0
0
Postmantesting~15 mins

Variable scope and precedence in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify variable scope and precedence in Postman scripts
Preconditions (2)
Step 1: Set a global variable named 'var1' with value 'globalValue'
Step 2: Set an environment variable named 'var1' with value 'envValue'
Step 3: Set a collection variable named 'var1' with value 'collectionValue'
Step 4: In the Pre-request Script of a request, set a local variable 'var1' with value 'localValue'
Step 5: In the request URL or headers, use {{var1}} to reference the variable
Step 6: Send the request
Step 7: In the Tests script, retrieve the value of 'var1' using pm.variables.get('var1')
Step 8: Log the retrieved value to the Postman Console
✅ Expected Result: The value logged in the Postman Console should be 'localValue', showing that local variables have the highest precedence over collection, environment, and global variables.
Automation Requirements - Postman Sandbox JavaScript
Assertions Needed:
Assert that pm.variables.get('var1') returns 'localValue' in the Tests script
Best Practices:
Use pm.variables.set() to set local variables in Pre-request Script
Use pm.collectionVariables.set(), pm.environment.set(), pm.globals.set() for other scopes
Use pm.variables.get() to retrieve the variable value considering scope precedence
Log outputs to Postman Console for debugging
Keep variable names consistent to test precedence clearly
Automated Solution
Postman
// 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.

Common Mistakes - 3 Pitfalls
{'mistake': 'Setting variables only in global or environment scope and expecting local scope precedence', 'why_bad': "Local variables override other scopes, so if not set, the expected precedence won't be demonstrated.", 'correct_approach': 'Always set the local variable in the Pre-request Script using pm.variables.set() to test precedence.'}
Using pm.environment.get() or pm.globals.get() to retrieve variables ignoring scope precedence
Not clearing or resetting variables before the test, causing stale values
Bonus Challenge

Now add data-driven testing by setting 'var1' to three different values in local scope and verify the precedence each time.

Show Hint