Bird
Raised Fist0
Postmantesting~20 mins

Setting variables in scripts in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Variable Mastery in Postman
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the value of the environment variable after this script runs?
Consider this Postman test script that sets an environment variable. What will be the value of the environment variable userId after execution?
Postman
pm.environment.set('userId', 12345);
pm.environment.set('userId', Number(pm.environment.get('userId')) + 5);
Aundefined
BError: Cannot add number to undefined
C12350
D12345
Attempts:
2 left
💡 Hint
Remember that environment variables are stored as strings and you may need to convert them to numbers before arithmetic.
assertion
intermediate
2:00remaining
Which assertion correctly verifies a global variable value in Postman?
You want to check if the global variable token equals abc123 in a test script. Which assertion is correct?
Apm.test('Token is correct', () => pm.expect(pm.variables.get('token')).to.eql('abc123'));
Bpm.test('Token is correct', () => pm.expect(pm.globals.get('token')).to.eql('abc123'));
Cpm.test('Token is correct', () => pm.expect(pm.environment.get('token')).to.eql('abc123'));
Dpm.test('Token is correct', () => pm.expect(pm.collectionVariables.get('token')).to.eql('abc123'));
Attempts:
2 left
💡 Hint
Global variables are accessed via pm.globals.get()
🔧 Debug
advanced
2:00remaining
Why does this script fail to set the collection variable?
This Postman script tries to set a collection variable but it does not appear in the collection variables list after running. What is the issue?
Postman
pm.collectionVariables.set('sessionId', 7890);
console.log('Session ID set to:', pm.collectionVariables.get('sessionId'));
AThe collection variable is set but changes are not saved persistently until the collection is saved manually.
BCollection variables cannot be set dynamically in scripts; they must be set manually.
CThe script runs in the wrong context; collection variables can only be set in pre-request scripts, not test scripts.
DThe variable name 'sessionId' is reserved and cannot be used for collection variables.
Attempts:
2 left
💡 Hint
Check how Postman handles collection variable persistence after setting them in scripts.
🧠 Conceptual
advanced
2:00remaining
Which variable scope has the highest priority when accessing variables in Postman scripts?
In Postman scripts, variables can exist in multiple scopes: global, collection, environment, and local. Which scope is checked first when accessing a variable by name?
ALocal variables
BGlobal variables
CEnvironment variables
DCollection variables
Attempts:
2 left
💡 Hint
Think about the closest scope to the script execution context.
framework
expert
2:00remaining
Which Postman script snippet correctly sets a variable only if it does not already exist?
You want to set an environment variable userRole to admin only if it is not already set. Which script snippet achieves this correctly?
Aif (pm.environment.get('userRole') === '') { pm.environment.set('userRole', 'admin'); }
Bif (pm.environment.get('userRole') === undefined) { pm.environment.set('userRole', 'admin'); }
Cif (pm.environment.get('userRole') == null) { pm.environment.set('userRole', 'admin'); }
Dif (!pm.environment.get('userRole')) { pm.environment.set('userRole', 'admin'); }
Attempts:
2 left
💡 Hint
Check how Postman returns undefined or null for unset variables and how JavaScript treats falsy values.

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

  1. 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.
  2. Step 2: Differentiate from other actions

    Sending requests, deleting variables, or logging are done by other methods, not pm.variables.set.
  3. Final Answer:

    To store a value in a local variable for later use within the same request -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    pm.variables.set('token', 'abc123'); -> Option D
  4. 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

  1. Step 1: Understand variable setting and getting

    The script sets 'userId' to the number 42, then retrieves it with pm.variables.get.
  2. Step 2: Check the console output

    The retrieved value is 42 (a number), so console.log(id); prints 42 without quotes.
  3. Final Answer:

    42 -> Option A
  4. Quick Check:

    Set and get return the same stored value [OK]
Hint: Get returns exactly what was set, including type [OK]
Common Mistakes:
  • Assuming get returns string always
  • Expecting undefined if variable not set
  • Confusing quotes in console output
4. You wrote this Postman script:
pm.variables.set('session', 'abc');
pm.variables.get('session');

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

  1. Step 1: Understand variable scopes in Postman

    pm.variables.set sets a local variable only for the current script execution, not across requests.
  2. Step 2: Use environment variables for persistence

    To keep variables accessible across requests, use pm.environment.set instead.
  3. Final Answer:

    You should use pm.environment.set to make it accessible across requests -> Option B
  4. 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

  1. 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.
  2. 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.
  3. 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.
  4. Final Answer:

    Use pm.environment.set('authToken', pm.response.json().token); in the Tests script of the previous request -> Option C
  5. 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]
Common Mistakes:
  • Trying to access response in Pre-request Script
  • Using pm.variables.set for cross-request data
  • Setting variables in wrong script phase