0
0
Postmantesting~10 mins

Default and conditional responses in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sends a request to an API endpoint and verifies the response status code and body content. It checks for a default response and a conditional response based on a query parameter.

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

const jsonData = pm.response.json();
const url = pm.request.url.toString();

pm.test("Response contains default message", function () {
    if (!url.includes("param=hello")) {
        pm.expect(jsonData.message).to.eql("Default response");
    } else {
        pm.expect(true).to.be.true; // Skip if condition not met
    }
});

pm.test("Conditional response when param=hello", function () {
    if (url.includes("param=hello")) {
        pm.expect(jsonData.message).to.eql("Hello response");
    } else {
        pm.expect(true).to.be.true; // Skip if condition not met
    }
});
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Send GET request to API endpoint without query parameterAPI server is running and ready to respondCheck response status code is 200PASS
2Parse JSON response bodyResponse body received: {"message": "Default response"}Verify response message equals 'Default response'PASS
3Send GET request to API endpoint with query parameter param=helloAPI server is running and ready to respondCheck response status code is 200PASS
4Parse JSON response bodyResponse body received: {"message": "Hello response"}Verify response message equals 'Hello response' when param=helloPASS
Failure Scenario
Failing Condition: API returns a status code other than 200 or response message does not match expected
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify when no query parameter is sent?
AResponse status code is 404
BResponse message is 'Default response'
CResponse message is 'Hello response'
DResponse body is empty
Key Result
Always verify both the status code and the response body content to ensure the API behaves correctly for default and conditional cases.