0
0
Postmantesting~10 mins

Extracting data from responses in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sends a GET request to a sample API endpoint and extracts a specific value from the JSON response. It verifies that the extracted value matches the expected result.

Test Code - Postman
Postman
pm.test("Extract user ID from response", function () {
    pm.sendRequest('https://jsonplaceholder.typicode.com/users/1', function (err, res) {
        pm.expect(err).to.be.null;
        pm.expect(res).to.have.property('status', 200);
        const jsonData = res.json();
        const userId = jsonData.id;
        pm.expect(userId).to.eql(1);
        pm.environment.set('extractedUserId', userId);
    });
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and sends GET request to 'https://jsonplaceholder.typicode.com/users/1'Postman sends HTTP GET request to the API endpoint-PASS
2Receives HTTP response with status code 200 and JSON bodyResponse contains user data JSON with id, name, username, etc.Check that response status code is 200PASS
3Parses JSON response and extracts 'id' field valueExtracted userId = 1Verify extracted userId equals 1PASS
4Stores extracted userId in environment variable 'extractedUserId'Environment variable 'extractedUserId' set to 1-PASS
5Test completes successfullyTest result marked as passed in Postman test runner-PASS
Failure Scenario
Failing Condition: Response status code is not 200 or 'id' field is missing or incorrect
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after receiving the response?
AThat the response body is empty
BThat the request method is POST
CThat the response status code is 200 and 'id' equals 1
DThat the environment variable is deleted
Key Result
Always verify the response status code before extracting data, and store extracted values in environment variables for reuse in subsequent requests.