A global variable in Postman is a value stored that can be accessed by any request or script within the Postman workspace. It helps share data across multiple requests.
Click to reveal answer
beginner
How do you set a global variable in a Postman test script?
You use pm.globals.set('variableName', 'value') to create or update a global variable in Postman.
Click to reveal answer
beginner
How can you retrieve a global variable's value in Postman scripts?
Use pm.globals.get('variableName') to get the value of a global variable in Postman scripts.
Click to reveal answer
intermediate
What is the difference between global variables and environment variables in Postman?
Global variables are accessible across all environments and requests, while environment variables are specific to a selected environment and help manage different setups like dev, test, or prod.
Click to reveal answer
intermediate
Why should you be careful when using global variables in Postman?
Because global variables are shared across all requests and environments, accidental changes can affect many tests. It's best to use them only when needed and clean up after tests.
Click to reveal answer
Which Postman method sets a global variable?
Apm.globals.set('name', 'value')
Bpm.environment.set('name', 'value')
Cpm.variables.set('name', 'value')
Dpm.request.set('name', 'value')
✗ Incorrect
pm.globals.set() is used to set global variables accessible across all requests.
How do you access a global variable's value in Postman?
Apm.collection.get('variableName')
Bpm.environment.get('variableName')
Cpm.request.get('variableName')
Dpm.globals.get('variableName')
✗ Incorrect
pm.globals.get() retrieves the value of a global variable.
What is a key difference between global and environment variables in Postman?
AGlobal variables cannot be changed during runtime.
BEnvironment variables are shared globally; global variables are environment-specific.
CGlobal variables are shared across all environments; environment variables are environment-specific.
DEnvironment variables are only for authentication.
✗ Incorrect
Global variables are accessible everywhere; environment variables depend on the selected environment.
Why might overusing global variables be risky in Postman?
AThey slow down the Postman app.
BThey can cause unexpected test failures if changed unintentionally.
CThey are not saved between sessions.
DThey cannot store string values.
✗ Incorrect
Because global variables affect all tests, accidental changes can cause failures.
Which of these is NOT a valid way to clear a global variable in Postman?
Apm.globals.remove('variableName')
Bpm.globals.unset('variableName')
Cpm.globals.set('variableName', null)
Dpm.globals.clear()
✗ Incorrect
pm.globals.remove() is not a valid method; use unset() or clear() instead.
Explain what global variables are in Postman and how they differ from environment variables.
Think about scope and sharing of variables.
You got /4 concepts.
Describe how to set, get, and clear a global variable in Postman scripts.
Focus on the Postman scripting API methods.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of global variables in Postman?
easy
A. To store data accessible across all requests and tests
B. To store data only for the current request
C. To create temporary variables that disappear after one test
D. To define environment-specific settings
Solution
Step 1: Understand global variable scope
Global variables in Postman are accessible everywhere, across all requests and tests.
Step 2: Compare with other variable types
Unlike environment or local variables, global variables persist and are shared globally.
Final Answer:
To store data accessible across all requests and tests -> Option A
Quick Check:
Global variables = accessible everywhere [OK]
Hint: Global variables are shared everywhere in Postman [OK]
Common Mistakes:
Confusing global variables with environment variables
Thinking global variables are temporary
Assuming global variables are request-specific
2. Which of the following is the correct syntax to set a global variable named token with value abc123 in Postman?
easy
A. pm.globals.add('token', 'abc123');
B. pm.global.set('token', 'abc123');
C. pm.globals.set('token', 'abc123');
D. pm.globalVariables.set('token', 'abc123');
Solution
Step 1: Recall correct Postman global variable syntax
The correct method to set a global variable is pm.globals.set(key, value).
Step 2: Identify the correct object and method
Only pm.globals.set('token', 'abc123'); uses pm.globals.set correctly with proper quotes and parameters.
Final Answer:
pm.globals.set('token', 'abc123'); -> Option C
Quick Check:
Use pm.globals.set to set global variables [OK]
Hint: Use pm.globals.set('key', 'value') to set globals [OK]
Common Mistakes:
Using pm.global instead of pm.globals
Using .add instead of .set method
Incorrect object names like pm.globalVariables
3. Consider this Postman test script:
pm.globals.set('userId', '42');
const id = pm.globals.get('userId');
console.log(id);
What will be printed in the Postman console?
medium
A. undefined
B. 42
C. userId
D. null
Solution
Step 1: Set global variable 'userId' to '42'
The first line stores the string '42' in the global variable named 'userId'.
Step 2: Retrieve and log the global variable
The second line gets the value of 'userId' which is '42', then logs it.
Final Answer:
42 -> Option B
Quick Check:
pm.globals.get('userId') returns '42' [OK]
Hint: pm.globals.get returns the stored value [OK]
Common Mistakes:
Expecting variable name instead of value
Confusing undefined with null
Forgetting to set the variable before getting
4. You wrote this Postman script to set a global variable:
pm.globals.set(userId, '100');
But it throws an error. What is the problem?
medium
A. The variable name userId should be a string in quotes
B. pm.globals.set cannot set numeric values
C. pm.globals.set requires three arguments
D. Global variables cannot be set inside scripts
Solution
Step 1: Check the syntax of pm.globals.set
The first argument must be a string key, so it should be quoted like 'userId'.
Step 2: Identify the error cause
Without quotes, userId is treated as an undefined variable causing an error.
Final Answer:
The variable name userId should be a string in quotes -> Option A
Quick Check:
Keys must be strings in pm.globals.set [OK]
Hint: Always quote variable names in pm.globals.set [OK]
Common Mistakes:
Not quoting the key name
Thinking pm.globals.set needs more arguments
Believing global variables can't be set in scripts
5. You want to share a session token between multiple requests in Postman using global variables. Which sequence correctly sets and retrieves the token?