Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
We use pm.response.json() to parse the response body as JSON. Then Array.isArray() checks if it is an array.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
We get the JSON array with pm.response.json() and check its length property.
3fill in blank
hardFix 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'
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.
✗ Incorrect
We must parse the response as JSON to access array elements and their properties.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' operator incorrectly in this context.
Using '==' or '===' instead of hasOwnProperty method.
✗ Incorrect
We use hasOwnProperty method to check if each item has the 'name' property.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '===' can cause unexpected results.
Using wrong property name or value.
✗ Incorrect
We check if some item has id === 20 using the some method.