0
0
Postmantesting~20 mins

Response body array assertions in Postman - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Array Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Check if response array contains an object with a specific property value
Given a JSON response body containing an array of user objects, which Postman test script correctly asserts that at least one user has the username 'alice'?
Postman
pm.test('Response has user with username alice', () => {
    const jsonData = pm.response.json();
    // Write assertion here
});
Apm.expect(jsonData.includes('alice')).to.be.true;
Bpm.expect(jsonData.some(user => user.username === 'alice')).to.be.true;
Cpm.expect(jsonData.filter(user => user.username === 'alice')).to.have.lengthOf(0);
Dpm.expect(jsonData.find(user => user.username === 'alice')).to.equal(true);
Attempts:
2 left
💡 Hint
Use an array method that returns true if any element matches the condition.
assertion
intermediate
2:00remaining
Assert the length of an array in response body
How do you write a Postman test to assert that the response body array has exactly 5 items?
Postman
pm.test('Response array length is 5', () => {
    const jsonData = pm.response.json();
    // Write assertion here
});
Apm.expect(jsonData.length).to.equal(5);
Bpm.expect(jsonData).to.have.length(5);
Cpm.expect(jsonData.size).to.equal(5);
Dpm.expect(jsonData.count).to.equal(5);
Attempts:
2 left
💡 Hint
Use the correct property to get array length in JavaScript.
Predict Output
advanced
2:00remaining
What is the result of this Postman test script?
Consider the following response body and test script. What will be the test result (pass or fail)?
Postman
/* Response body JSON */
[
  {"id": 1, "active": true},
  {"id": 2, "active": false},
  {"id": 3, "active": true}
]

/* Test script */
pm.test('All users are active', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData.every(user => user.active)).to.be.true;
});
AFail - response is not an array
BSyntaxError - incorrect use of every method
CPass - all users have active true
DFail - not all users have active true
Attempts:
2 left
💡 Hint
Check if every user object has active set to true.
locator
advanced
2:00remaining
Choose the correct JSON path to select all 'name' fields in an array response
Given a response body array of objects with a 'name' property, which JSON path expression correctly selects all 'name' values?
A$[*].name
B$.name
C$.name[*]
D$[*].['name']
Attempts:
2 left
💡 Hint
Use the wildcard to select all elements in the array, then access the 'name' property.
framework
expert
3:00remaining
Identify the correct Postman test script to assert sorted order of array by 'score'
You receive a response body array of objects with a numeric 'score' property. Which Postman test script correctly asserts that the array is sorted in ascending order by 'score'?
Postman
pm.test('Array is sorted by score ascending', () => {
    const jsonData = pm.response.json();
    // Write assertion here
});
Apm.expect(jsonData.map(v => v.score).sort()).to.eql(jsonData.map(v => v.score));
Bpm.expect(jsonData.reduce((acc,v) => acc && v.score >= acc.score, jsonData[0])).to.be.true;
Cpm.expect(jsonData.every((v,i,a) => i === 0 || a[i-1].score <= v.score)).to.be.true;
Dpm.expect(jsonData.sort((a,b) => a.score - b.score)).to.eql(jsonData);
Attempts:
2 left
💡 Hint
Check each element compared to the previous one to confirm ascending order.