0
0
Postmantesting~10 mins

Condition block in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if the API response status is 200 and then verifies the presence of a specific field in the JSON body using a condition block in Postman test script.

Test Code - Postman
Postman
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

if (pm.response.code === 200) {
    pm.test("Response has userId field", function () {
        var jsonData = pm.response.json();
        pm.expect(jsonData).to.have.property('userId');
    });
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsPostman app is open with the API request ready-PASS
2Sends API requestRequest sent to server, waiting for response-PASS
3Receives response with status 200Response body contains JSON data with userId fieldpm.response.to.have.status(200)PASS
4Checks if response code is 200 (condition block)Condition true, enters block to check JSON body-PASS
5Verifies response JSON has 'userId' propertyJSON body includes userId fieldpm.expect(jsonData).to.have.property('userId')PASS
Failure Scenario
Failing Condition: Response status is not 200 or JSON body lacks 'userId' field
Execution Trace Quiz - 3 Questions
Test your understanding
What does the condition block check before running the second test?
AIf the response status code is 200
BIf the response body is empty
CIf the userId field is missing
DIf the request method is POST
Key Result
Using a condition block in Postman tests helps avoid errors by running certain checks only when the response status is as expected.