Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of setting variables in Postman scripts?
Setting variables in Postman scripts helps store and reuse data like tokens, IDs, or responses across requests, making tests dynamic and efficient.
Click to reveal answer
beginner
How do you set an environment variable in a Postman test script?
Use pm.environment.set('variableName', 'value'); to set an environment variable.
Click to reveal answer
intermediate
What is the difference between pm.environment.set() and pm.collectionVariables.set()?
pm.environment.set() sets a variable for the current environment only, while pm.collectionVariables.set() sets a variable that is available throughout the collection regardless of environment.
Click to reveal answer
beginner
How can you access a variable value in a Postman script?
Use pm.variables.get('variableName'); to get the value of a variable from any scope (environment, collection, globals).
Click to reveal answer
intermediate
Why is it useful to set variables dynamically during test execution?
Setting variables dynamically allows tests to adapt to changing data, like capturing tokens or IDs from responses and using them in later requests, making tests realistic and maintainable.
Click to reveal answer
Which method sets a global variable in Postman scripts?
Apm.globals.set('varName', 'value');
Bpm.environment.set('varName', 'value');
Cpm.collectionVariables.set('varName', 'value');
Dpm.variables.set('varName', 'value');
✗ Incorrect
Global variables are set using pm.globals.set(). Environment and collection variables use their respective methods.
How do you retrieve a variable value in a Postman script?
Apm.variables.set('varName');
Bpm.environment.get('varName');
Cpm.variables.get('varName');
Dpm.collectionVariables.get('varName');
✗ Incorrect
pm.variables.get() retrieves the value from any scope, making it the most flexible method.
What is the scope of a variable set with pm.environment.set()?
AEnvironment scope
BLocal scope only
CCollection scope
DGlobal scope
✗ Incorrect
pm.environment.set() sets variables only for the active environment.
Why would you set a variable in a test script after receiving a response?
ATo delete the response
BTo store data for use in later requests
CTo change the request URL
DTo pause the test execution
✗ Incorrect
Storing data like tokens or IDs from responses allows chaining requests dynamically.
Which Postman object is used to set variables in scripts?
Apm.response
Bpm.request
Cpm.test
Dpm
✗ Incorrect
The pm object provides methods to set and get variables in scripts.
Explain how to set and use an environment variable in a Postman test script.
Think about storing data from a response to use in the next request.
You got /4 concepts.
Describe the difference between environment, global, and collection variables in Postman.
Consider where the variable is available and how it affects multiple requests.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of using pm.variables.set in Postman scripts?
easy
A. To store a value in a local variable for later use within the same request
B. To send a request to the server
C. To delete a variable from the environment
D. To log information to the console
Solution
Step 1: Understand the function of pm.variables.set
This function stores a value in a local variable that can be used later in the same request or script.
Step 2: Differentiate from other actions
Sending requests, deleting variables, or logging are done by other methods, not pm.variables.set.
Final Answer:
To store a value in a local variable for later use within the same request -> Option A
Quick Check:
pm.variables.set stores local variables [OK]
Hint: Remember: set means save value locally in script [OK]
Common Mistakes:
Confusing variable setting with sending requests
Thinking it deletes variables
Assuming it logs output
2. Which of the following is the correct syntax to set a variable named token with value abc123 in a Postman test script?
easy
A. pm.variables.set['token', 'abc123'];
B. pm.variables.set(token, 'abc123');
C. pm.variables.set('token' = 'abc123');
D. pm.variables.set('token', 'abc123');
Solution
Step 1: Check the correct method signature
The correct syntax uses pm.variables.set('variableName', 'value'); with the variable name as a string and value as the second argument.
Step 2: Identify syntax errors in other options
pm.variables.set(token, 'abc123'); misses quotes around the variable name. pm.variables.set('token' = 'abc123'); uses an invalid assignment inside the method. pm.variables.set['token', 'abc123']; uses incorrect bracket notation.
Final Answer:
pm.variables.set('token', 'abc123'); -> Option D
Quick Check:
Use quotes for variable name in pm.variables.set [OK]
Hint: Variable names must be strings in quotes [OK]
Common Mistakes:
Omitting quotes around variable names
Using assignment inside set method
Using wrong brackets for method call
3. Consider this Postman test script snippet:
pm.variables.set('userId', 42);
const id = pm.variables.get('userId');
console.log(id);
What will be printed in the Postman console?
medium
A. 42
B. undefined
C. '42'
D. null
Solution
Step 1: Understand variable setting and getting
The script sets 'userId' to the number 42, then retrieves it with pm.variables.get.
Step 2: Check the console output
The retrieved value is 42 (a number), so console.log(id); prints 42 without quotes.
Final Answer:
42 -> Option A
Quick Check:
Set and get return the same stored value [OK]
Hint: Get returns exactly what was set, including type [OK]
But the variable session is not accessible in later requests. What is the likely problem?
medium
A. You must call pm.variables.save() to save variables
B. You should use pm.environment.set to make it accessible across requests
C. Variable names cannot be strings
D. You need to restart Postman to apply changes
Solution
Step 1: Understand variable scopes in Postman
pm.variables.set sets a local variable only for the current script execution, not across requests.
Step 2: Use environment variables for persistence
To keep variables accessible across requests, use pm.environment.set instead.
Final Answer:
You should use pm.environment.set to make it accessible across requests -> Option B
Quick Check:
Local variables are temporary; environment variables persist [OK]
Hint: Use environment.set for cross-request variables [OK]
Common Mistakes:
Expecting pm.variables.set to persist across requests
Thinking a save method is needed
Believing variable names can't be strings
5. You want to set a variable authToken in a Pre-request Script that depends on the response of a previous request stored in pm.response.json(). Which approach correctly sets authToken for use in the next request?
hard
A. Use pm.variables.set('authToken', pm.response.json().token); in the Pre-request Script
B. Use pm.environment.set('authToken', pm.response.json().token); in the Pre-request Script
C. Use pm.environment.set('authToken', pm.response.json().token); in the Tests script of the previous request
D. Use pm.variables.set('authToken', pm.response.json().token); in the Tests script of the previous request
Solution
Step 1: Identify when response data is available
The response data from pm.response.json() is only available in the Tests script after the request completes, not in the Pre-request Script.
Step 2: Set variable for next request
To use authToken in the next request, set it as an environment variable in the Tests script of the current request using pm.environment.set.
Step 3: Why not local variables?
Local variables set with pm.variables.set do not persist across requests, so they won't be available in the next request.
Final Answer:
Use pm.environment.set('authToken', pm.response.json().token); in the Tests script of the previous request -> Option C
Quick Check:
Set environment variables in Tests to share data between requests [OK]
Hint: Set environment vars in Tests script to share between requests [OK]