0
0
Postmantesting~10 mins

Setting variables from response in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sends an API request and sets a Postman environment variable from the JSON response. It verifies that the variable is correctly set for later use.

Test Code - Postman
Postman
pm.test("Set variable from response", function () {
    const jsonData = pm.response.json();
    pm.environment.set("userId", jsonData.id);
    pm.expect(pm.environment.get("userId")).to.eql(jsonData.id);
});
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test starts and sends API requestPostman sends HTTP request to the API endpoint-PASS
2Receives JSON response with user dataResponse body contains JSON like {"id": 123, "name": "John"}-PASS
3Extracts 'id' from response JSON and sets environment variable 'userId'Environment variable 'userId' is set to 123Check environment variable 'userId' equals response 'id'PASS
4Assertion verifies 'userId' variable matches response 'id'Variable 'userId' value is 123, matching responsepm.expect(pm.environment.get("userId")).to.eql(jsonData.id);PASS
Failure Scenario
Failing Condition: Response JSON does not contain 'id' field or variable is not set
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do after receiving the API response?
ASets an environment variable from the response JSON
BSends another API request immediately
CDeletes all environment variables
DCloses the Postman app
Key Result
Always verify that the response contains the expected data before setting variables, and assert the variable value to catch errors early.