0
0
Postmantesting~20 mins

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

Choose your learning style9 modes available
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.