0
0
Postmantesting~10 mins

Response body array assertions in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sends a GET request to an API endpoint that returns a JSON array in the response body. It verifies that the response status is 200, the response body is an array, and that the array contains at least one object with a specific property value.

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

pm.test("Response body is an array", () => {
    const jsonData = pm.response.json();
    pm.expect(Array.isArray(jsonData)).to.be.true;
});

pm.test("Array contains an object with 'id' equal to 3", () => {
    const jsonData = pm.response.json();
    const found = jsonData.some(item => item.id === 3);
    pm.expect(found).to.be.true;
});
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Send GET request to API endpointRequest sent, waiting for response-PASS
2Receive response with status 200 and JSON bodyResponse received: status 200, body is JSON arrayCheck that response status is 200PASS
3Parse response body as JSONResponse body parsed into JavaScript arrayVerify that parsed response is an arrayPASS
4Check if any object in array has 'id' equal to 3Iterate over array itemsAssert that at least one item has id === 3PASS
Failure Scenario
Failing Condition: Response status is not 200, or response body is not an array, or no object with 'id' equal to 3 is found
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the response body?
AThat the response body is a JSON array
BThat the response body is a string
CThat the response body is empty
DThat the response body is a number
Key Result
Always verify the response status before checking the response body. When the response is expected to be an array, assert its type explicitly. Use array methods like 'some' to check for specific objects inside the array efficiently.