0
0
Postmantesting~15 mins

Variable assignment in flows in Postman - Build an Automation Script

Choose your learning style9 modes available
Assign and use variables in Postman request flow
Preconditions (2)
Step 1: Open the first request in the collection
Step 2: Send the request and capture a value from the JSON response (e.g., userId)
Step 3: Assign the captured value to a collection variable named 'userId'
Step 4: Open the second request in the collection
Step 5: Use the collection variable 'userId' in the URL or headers of the second request
Step 6: Send the second request
✅ Expected Result: The first request runs successfully and assigns the 'userId' variable. The second request uses the assigned 'userId' variable correctly and runs successfully.
Automation Requirements - Postman test scripts
Assertions Needed:
Verify the first request response status is 200
Verify the 'userId' variable is assigned and not empty
Verify the second request response status is 200
Best Practices:
Use pm.response.json() to parse JSON response
Use pm.collectionVariables.set() to assign variables
Use pm.collectionVariables.get() to retrieve variables
Add assertions to verify response status and variable assignment
Keep variable names clear and consistent
Automated Solution
Postman
/* 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.

Common Mistakes - 4 Pitfalls
Using environment variables instead of collection variables without updating the environment
Not checking if the response contains the expected key before assigning variable
Hardcoding variable values instead of capturing from response
Not adding assertions to verify variable assignment
Bonus Challenge

Now add data-driven testing by running the first request with 3 different user credentials and verify variable assignment each time

Show Hint