In Postman, variables can exist at different scopes: Global, Collection, Environment, and Local (within a request). Which variable scope has the highest precedence when resolving a variable name during a request execution?
Think about which variable is closest to the request itself.
Local variables defined within a request have the highest precedence because they are the closest to the request execution context. Postman resolves variables starting from local, then collection, environment, and finally global.
Given the following Postman pre-request script and variable scopes, what will be the value of pm.variables.get('token') during the request execution?
Global variable token = "global123"
Environment variable token = "env456"
Collection variable token = "col789"
Pre-request script:
pm.variables.set('token', 'local000'); console.log(pm.variables.get('token'));
Variables set in the script override other scopes.
Setting a variable with pm.variables.set creates a local variable for the request, which has the highest precedence. Therefore, pm.variables.get('token') returns "local000".
You want to assert that the variable userId used in your request is the environment variable value, not a local or global one. Which assertion correctly verifies this in the test script?
Remember that pm.variables.get() returns the variable value with precedence applied.
pm.variables.get('userId') returns the variable value considering all scopes with precedence. To verify it matches the environment variable, you compare pm.variables.get('userId') to pm.environment.get('userId'). Option A does this correctly.
You have a collection variable apiKey set to "colKey" and an environment variable apiKey set to "envKey". Your request script uses pm.variables.get('apiKey') but always returns "colKey" instead of "envKey". What is the most likely reason?
Check the Postman variable precedence order.
Postman resolves variables in this order: local, collection, environment, global. Since collection variables have higher precedence than environment variables, pm.variables.get('apiKey') returns the collection variable value "colKey".
Arrange the following Postman variable scopes in the order Postman uses to resolve a variable name, from highest precedence to lowest.
Think about which variables are closest to the request execution context.
Postman resolves variables starting from local (set in scripts), then collection, environment, and finally global variables. This order ensures the most specific variable is used.