Variable assignment in flows in Postman - Build an Automation Script
/* First request test script */ pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); const jsonData = pm.response.json(); if (jsonData.userId) { pm.collectionVariables.set('userId', jsonData.userId); pm.test('userId variable is set', () => { pm.expect(pm.collectionVariables.get('userId')).to.not.be.empty; }); } else { pm.test('userId exists in response', () => { pm.expect.fail('userId not found in response'); }); } /* Second request test script */ pm.test('Status code is 200', () => { pm.response.to.have.status(200); });
The first request test script checks that the response status is 200, meaning the request succeeded.
It then parses the JSON response using pm.response.json() and checks if userId exists.
If userId is found, it assigns it to a collection variable named userId using pm.collectionVariables.set().
It also asserts that the variable is not empty to confirm assignment.
If userId is missing, the test fails with a clear message.
The second request test script simply verifies that the response status is 200, assuming the request URL or headers use the {{userId}} variable from the collection variables.
This approach ensures the variable is assigned and used correctly across requests in the flow.
Now add data-driven testing by running the first request with 3 different user credentials and verify variable assignment each time
Practice
pm.variables.set('token', 'abc123') do in a Postman test script?Solution
Step 1: Understand the method
This method assigns a value to a variable in Postman scripts.pm.variables.setStep 2: Analyze the parameters
The first parameter is the variable name 'token', the second is the value 'abc123' to store.Final Answer:
It saves the value 'abc123' to a variable named 'token' for later use. -> Option AQuick Check:
pm.variables.set saves data [OK]
- Confusing set with get method
- Thinking set deletes variables
- Assuming set sends data in requests
Solution
Step 1: Recall the correct method for assigning variables
The method to assign a variable ispm.variables.set(name, value).Step 2: Check each option's syntax
pm.variables.set('userId', 42); usespm.variables.set('userId', 42);which is correct syntax.Final Answer:
pm.variables.set('userId', 42); -> Option DQuick Check:
Use pm.variables.set to assign [OK]
- Using get instead of set to assign
- Incorrect method names like assign or store
- Wrong parameter syntax with equals sign
pm.variables.set('count', 5);
pm.variables.set('count', pm.variables.get('count') + 3);
console.log(pm.variables.get('count'));What will be printed in the console?
Solution
Step 1: Analyze the first assignment
The variable 'count' is set to 5 initially.Step 2: Analyze the second assignment
It gets the current 'count' value (5), adds 3, and sets 'count' to 8.Step 3: Check the console.log output
It prints the updated value of 'count', which is 8.Final Answer:
8 -> Option BQuick Check:
5 + 3 = 8 [OK]
- Assuming the first value stays unchanged
- Thinking get returns undefined
- Confusing set and get usage
pm.variables.set('status', 'active')
pm.variables.get('status')But the test fails because the variable 'status' is undefined. What is the likely cause?
Solution
Step 1: Understand variable scopes in Postman
Variables set withpm.variables.setare local to the current request script and do not persist across requests.Step 2: Identify why 'status' is undefined
If you try to get 'status' in a different request without using environment or global variables, it will be undefined.Final Answer:
Variables set in one request are not available in another without environment or global scope. -> Option AQuick Check:
Local variables don't persist across requests [OK]
- Thinking semicolons affect variable persistence
- Believing get waits for set automatically
- Assuming variable names can't be strings
Solution
Step 1: Identify variable scope for cross-request use
To reuse data across requests, environment variables must be used, not local variables.Step 2: Match correct set and get methods
In first request test script:pm.environment.set('token', pm.response.json().token);
In next request pre-request script:const token = pm.environment.get('token');usespm.environment.setandpm.environment.get, which correctly share data between requests.Final Answer:
Use environment variables to save and retrieve token across requests. -> Option CQuick Check:
Environment variables persist across requests [OK]
- Using pm.variables for cross-request data
- Mixing environment and local variable methods
- Not retrieving variables in the correct scope
Ready to prove it?
Pass the test and this pattern moves unknown
