0
0
Postmantesting~20 mins

Response body inspection in Postman - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Body Inspector
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Postman test script?

Given the following Postman test script that inspects the response body, what will be the test result?

Postman
pm.test("Check user name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.user.name).to.eql("Alice");
});
ATest fails if response JSON is not a JSON object
BTest fails if response JSON has user.name exactly 'Alice'
CTest passes if response JSON has user.name containing 'Alice' anywhere
DTest passes if response JSON has user.name exactly 'Alice'
Attempts:
2 left
💡 Hint

Look at the assertion method to.eql and what it checks.

assertion
intermediate
2:00remaining
Which assertion correctly checks if response body contains a key 'status' with value 'success'?

Choose the Postman test assertion that correctly verifies the response body JSON has a key status with value success.

Apm.expect(pm.response.json().status).to.equal('success');
Bpm.expect(pm.response.body.status).to.eql('success');
Cpm.expect(pm.response.json().status).to.contain('success');
Dpm.expect(pm.response.body).to.have.property('status', 'success');
Attempts:
2 left
💡 Hint

Remember that pm.response.json() parses the response body as JSON.

🔧 Debug
advanced
2:00remaining
Why does this Postman test fail with 'TypeError: Cannot read property "id" of undefined'?

Consider this test script:

pm.test("Check user id", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.user.id).to.be.a('number');
});

The test fails with the error above. What is the most likely cause?

AThe 'id' property exists but is not a number, causing the assertion to fail.
BThe response body is not valid JSON, so pm.response.json() throws an error.
CThe response JSON does not have a 'user' key, so jsonData.user is undefined.
DThe test script syntax is incorrect and causes a runtime error.
Attempts:
2 left
💡 Hint

Check what happens if you try to access a property of undefined.

🧠 Conceptual
advanced
2:00remaining
What is the best way to check if a response body contains an array with at least one item?

In Postman, you want to verify the response body JSON has an array called items and it contains at least one element. Which test assertion is correct?

Apm.expect(pm.response.json().items).to.have.lengthOf.above(0);
Bpm.expect(pm.response.json().items).to.be.an('array').and.to.have.length.above(0);
Cpm.expect(pm.response.json().items.length).to.be.above(0);
Dpm.expect(pm.response.json().items).to.be.an('array').with.length.above(0);
Attempts:
2 left
💡 Hint

Check how to assert type and length in Postman using Chai assertions.

framework
expert
3:00remaining
How to write a Postman test to verify nested JSON keys safely?

You want to write a Postman test that checks if response.data.user.profile.email exists and is a non-empty string, but the response may not always have all nested keys. Which approach is safest to avoid runtime errors?

Avar jsonData = pm.response.json(); pm.expect(jsonData.data && jsonData.data.user && jsonData.data.user.profile && jsonData.data.user.profile.email).to.be.a('string').and.not.empty;
Bpm.expect(pm.response.json()?.data?.user?.profile?.email).to.be.a('string').and.not.empty;
Cpm.expect(pm.response.json().data.user.profile.email).to.be.a('string').and.not.empty;
Dvar email = pm.response.json().data.user.profile.email || ''; pm.expect(email).to.be.a('string').and.not.empty;
Attempts:
2 left
💡 Hint

Think about how to safely access nested properties without causing errors.