0
0
Cypresstesting~20 mins

Asserting response bodies in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Body Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2: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
})
Aexpect(response.body.name).to.equal('Alice')
Bexpect(response.body.name).to.be.undefined
Cexpect(response.body).to.have.property('name', 'Bob')
Dexpect(response.status).to.equal(404)
Attempts:
2 left
💡 Hint
Check the property name and expected value carefully.
Predict Output
intermediate
2: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;
})
ATest throws a TypeError if product property is missing
BTest fails if product id is 123 but available is false
CTest passes if product id is 123 and available is true
DTest passes regardless of response body content
Attempts:
2 left
💡 Hint
Consider what happens if the assertions match the response body.
🔧 Debug
advanced
2: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')
})
Aresponse.body.data is not an array, so .length causes error
BMissing semicolon after first expect causes syntax error
CUsing .equal instead of .deep.equal causes failure
DThe URL '/api/data' is incorrect causing 404 error
Attempts:
2 left
💡 Hint
Check the type of response.body.data before using .length
🧠 Conceptual
advanced
2: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?
Aexpect(response.body).to.deep.equal({status: 'success'})
Bexpect(response.body).to.include({status: 'success'})
Cexpect(response.body).to.have.length(1)
Dexpect(response.body.status).to.equal('success')
Attempts:
2 left
💡 Hint
Think about checking only one property without affecting others.
framework
expert
3: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?
A
cy.request('POST', '/api/login', {user: 'test'}).wait(1000).then((response) => {
  expect(response.body.token).to.exist
})
B
cy.request('POST', '/api/login', {user: 'test'}).then((response) => {
  expect(response.body.token).to.exist
})
C
cy.request('POST', '/api/login', {user: 'test'}).should((response) => {
  expect(response.body.token).to.exist
})
D
cy.request('POST', '/api/login', {user: 'test'}).then(() => {
  expect(response.body.token).to.exist
})
Attempts:
2 left
💡 Hint
Remember how Cypress handles promises and response objects.