Bird
Raised Fist0
Cypresstesting~20 mins

Asserting response bodies in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of asserting response bodies in Cypress tests?
easy
A. To check if the UI elements are visible on the page
B. To verify the server sends the correct data in the response body
C. To measure the page load time
D. To test the browser compatibility

Solution

  1. Step 1: Understand response body assertion

    Asserting response bodies means checking the data returned by the server is correct and as expected.
  2. Step 2: Identify the purpose in Cypress tests

    Cypress uses assertions on response bodies to confirm the server sends the right data, not UI or performance checks.
  3. Final Answer:

    To verify the server sends the correct data in the response body -> Option B
  4. Quick Check:

    Response body assertion = verify server data [OK]
Hint: Response body assertions check server data, not UI or speed [OK]
Common Mistakes:
  • Confusing response body with UI element checks
  • Thinking it measures performance
  • Assuming it tests browser features
2. Which of the following is the correct syntax to assert that the response body has a property name equal to "John" in Cypress?
easy
A. cy.request('/api/user').expect('body.name').to.equal('John')
B. cy.request('/api/user').body.should('contain', 'name', 'John')
C. cy.request('/api/user').its('body').should('have.property', 'name', 'John')
D. cy.request('/api/user').should('body.name', 'John')

Solution

  1. Step 1: Review Cypress chaining syntax

    In Cypress, cy.request() returns a response object. To assert a property in the body, use its('body') to access it.
  2. Step 2: Use should('have.property', key, value) for assertion

    The correct way to check a property and its value is should('have.property', 'name', 'John').
  3. Final Answer:

    cy.request('/api/user').its('body').should('have.property', 'name', 'John') -> Option C
  4. Quick Check:

    Use its('body').should('have.property', key, value) [OK]
Hint: Use its('body').should('have.property', key, value) for body checks [OK]
Common Mistakes:
  • Trying to access body directly without its()
  • Using expect() incorrectly inside cy.request chain
  • Wrong method names like expect() or body.should()
3. Given the following Cypress code, what will be the result of the assertion?
cy.request('/api/data').then((response) => {
  expect(response.status).to.eq(200)
  expect(response.body.items).to.have.length(3)
})
medium
A. The test passes if the response status is 200 and items array has exactly 3 elements
B. The test fails if the response status is 200 but items array has more than 3 elements
C. The test passes regardless of the items array length
D. The test fails if the response status is not 404

Solution

  1. Step 1: Analyze the status code assertion

    The code expects response.status to equal 200, so the server must respond with HTTP 200.
  2. Step 2: Analyze the items array length assertion

    The code expects response.body.items to have exactly 3 elements. If it has more or less, the test fails.
  3. Final Answer:

    The test passes if the response status is 200 and items array has exactly 3 elements -> Option A
  4. Quick Check:

    Status 200 and items length 3 = pass [OK]
Hint: Both status and body length must match exactly for pass [OK]
Common Mistakes:
  • Ignoring the length check on items array
  • Confusing status 200 with 404
  • Assuming partial matches pass
4. Identify the error in this Cypress test code that asserts the response body property success is true:
cy.request('/api/submit').then((response) => {
  expect(response.body.success).to.be.true()
})
medium
A. The cy.request method is missing a URL
B. The property success does not exist in response.body
C. The then callback should use response.body.success()
D. The assertion should be to.be.true without parentheses

Solution

  1. Step 1: Check the assertion syntax

    In Chai assertions used by Cypress, to.be.true is a property, not a function, so it should not have parentheses.
  2. Step 2: Confirm other parts are correct

    The URL is provided, and accessing response.body.success is valid if the property exists.
  3. Final Answer:

    The assertion should be to.be.true without parentheses -> Option D
  4. Quick Check:

    Use to.be.true without () for boolean assertions [OK]
Hint: Boolean assertions use to.be.true without parentheses [OK]
Common Mistakes:
  • Adding parentheses to boolean assertions
  • Assuming success is a function
  • Missing URL in cy.request
5. You want to assert that the response body contains a nested object with a user's id equal to 5 and a roles array containing "admin". Which Cypress assertion correctly checks both conditions?
hard
A. cy.request('/api/user/5').its('body').should((body) => { expect(body.id).to.eq(5) expect(body.roles).to.include('admin') })
B. cy.request('/api/user/5').should('body.id', 5).and('body.roles', 'admin')
C. cy.request('/api/user/5').then((response) => { expect(response.body.id).to.equal(5) expect(response.body.roles).to.have.length(1) })
D. cy.request('/api/user/5').its('body').should('have.property', 'id', 5).and('have.property', 'roles', 'admin')

Solution

  1. Step 1: Understand nested assertions with callback

    Using should with a callback allows multiple assertions on the body object.
  2. Step 2: Check each assertion correctness

    expect(body.id).to.eq(5) checks the id correctly. expect(body.roles).to.include('admin') checks the roles array contains 'admin'.
  3. Step 3: Identify incorrect options

    cy.request('/api/user/5').should('body.id', 5).and('body.roles', 'admin') uses invalid chaining syntax. cy.request('/api/user/5').then((response) => { expect(response.body.id).to.equal(5) expect(response.body.roles).to.have.length(1) }) misses checking 'admin' in roles. cy.request('/api/user/5').its('body').should('have.property', 'id', 5).and('have.property', 'roles', 'admin') wrongly checks roles as a property equal to string 'admin'.
  4. Final Answer:

    cy.request('/api/user/5').its('body').should((body) => { expect(body.id).to.eq(5) expect(body.roles).to.include('admin') }) -> Option A
  5. Quick Check:

    Use callback in should for multiple nested assertions [OK]
Hint: Use should callback for multiple nested property checks [OK]
Common Mistakes:
  • Trying to chain multiple properties incorrectly
  • Checking array length instead of content
  • Using have.property with array content as string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard