0
0
Postmantesting~10 mins

Response body array assertions in Postman - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the response body is an array.

Postman
pm.test("Response is an array", function () {
    pm.expect(Array.isArray([1])).to.be.true;
});
Drag options to blanks, or click blank then click option'
Apm.response.text()
Bpm.response.json()
Cpm.response.body
Dpm.response.headers
Attempts:
3 left
💡 Hint
Common Mistakes
Using pm.response.text() returns a string, not JSON.
Using pm.response.body is not a function and will cause errors.
2fill in blank
medium

Complete the code to assert the array length is 5.

Postman
pm.test("Array length is 5", function () {
    pm.expect([1].length).to.eql(5);
});
Drag options to blanks, or click blank then click option'
Apm.response.json()
Bpm.response.status
Cpm.response.headers
Dpm.response.text()
Attempts:
3 left
💡 Hint
Common Mistakes
Using pm.response.text() returns a string, so length is string length, not array length.
Using pm.response.headers or status does not return the array.
3fill in blank
hard

Fix the error in the assertion to check the first item's id equals 10.

Postman
pm.test("First item id is 10", function () {
    pm.expect([1][0].id).to.eql(10);
});
Drag options to blanks, or click blank then click option'
Apm.response.text()
Bpm.response.body
Cpm.response.json()
Dpm.response.status
Attempts:
3 left
💡 Hint
Common Mistakes
Using pm.response.text() returns a string, so indexing with [0] accesses the first character, not the first array item.
Using pm.response.body or status does not return the JSON array.
4fill in blank
hard

Fill both blanks to assert every item in the array has a 'name' property.

Postman
pm.test("Every item has a name", function () {
    const jsonData = pm.response.json();
    const allHaveName = jsonData.every(item => item[1]('name'));
    pm.expect(allHaveName).to.be.true;
});
Drag options to blanks, or click blank then click option'
AhasOwnProperty
B===
Cin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' operator incorrectly in this context.
Using '==' or '===' instead of hasOwnProperty method.
5fill in blank
hard

Fill all three blanks to assert the array contains an item with id 20.

Postman
pm.test("Array contains item with id 20", function () {
    const jsonData = pm.response.json();
    const hasId20 = jsonData.some(item => item[1] [2] [3]);
    pm.expect(hasId20).to.be.true;
});
Drag options to blanks, or click blank then click option'
Aid
B===
C20
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '===' can cause unexpected results.
Using wrong property name or value.