Challenge - 5 Problems
Response Body Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2:00remaining
Correct assertion for JSON response property
You have a Cypress test that sends a GET request to '/api/user'. The response body is expected to have a property 'name' with value 'Alice'. Which assertion correctly verifies this?
Cypress
cy.request('/api/user').then((response) => { // Insert assertion here })
Attempts:
2 left
💡 Hint
Check the property name and expected value carefully.
✗ Incorrect
Option A correctly asserts that the 'name' property in the response body equals 'Alice'. Option A checks for 'Bob', which is incorrect. Option A expects the property to be undefined, which is wrong. Option A checks the status code, not the body property.
❓ Predict Output
intermediate2:00remaining
Output of response body assertion with nested object
What will be the result of this Cypress test snippet?
Cypress
cy.request('/api/product').then((response) => { expect(response.body.product.id).to.equal(123); expect(response.body.product.available).to.be.true; })
Attempts:
2 left
💡 Hint
Consider what happens if the assertions match the response body.
✗ Incorrect
The test passes only if both assertions are true: product id equals 123 and available is true. If either fails, the test fails. If product is missing, a TypeError would occur but that is not the expected normal case.
🔧 Debug
advanced2:00remaining
Identify the error in response body assertion
This Cypress test fails with an error. What is the cause?
Cypress
cy.request('/api/data').then((response) => { expect(response.body.data).to.have.length(5) expect(response.body.data[0]).to.equal('item1') })
Attempts:
2 left
💡 Hint
Check the type of response.body.data before using .length
✗ Incorrect
If response.body.data is not an array, accessing .length or indexing with [0] will cause an error. The semicolon is optional in JavaScript. .equal works for string comparison. The URL correctness is not indicated by the error.
🧠 Conceptual
advanced2:00remaining
Best practice for asserting partial response body
You want to check that the response body contains a property 'status' with value 'success', but the body has many other properties you don't want to assert. Which is the best Cypress assertion?
Attempts:
2 left
💡 Hint
Think about checking only one property without affecting others.
✗ Incorrect
Option D directly asserts the 'status' property value without requiring the whole object to match. Option D requires exact match of the entire body, which is too strict. Option D is invalid because .include is not a valid Chai assertion for objects in this way. Option D checks length which is irrelevant.
❓ framework
expert3:00remaining
Handling asynchronous response body assertions in Cypress
You want to assert that a POST request to '/api/login' returns a JSON body with a token property. Which Cypress code snippet correctly waits and asserts the token exists?
Attempts:
2 left
💡 Hint
Remember how Cypress handles promises and response objects.
✗ Incorrect
Option B correctly uses .then() to access the response and assert the token property. Option B uses .should() which is not designed for cy.request responses. Option B adds unnecessary wait which is not needed. Option B tries to access response.body without receiving the response parameter, causing an error.