Bird
0
0

What is wrong with this Cypress API-first setup code?

medium📝 Debug Q7 of 15
Cypress - Test Organization and Patterns
What is wrong with this Cypress API-first setup code?
cy.request('POST', '/api/login', {username: 'user', password: 'pass'})
  .then((response) => {
    cy.setCookie('auth', response.token)
  })
Aresponse.token is undefined; should use response.body.token
Bcy.setCookie() cannot be used inside .then()
CPOST method cannot be used with cy.request()
DMissing cy.visit() after login
Step-by-Step Solution
Solution:
  1. Step 1: Check how to access response data

    Cypress response object stores API response data inside response.body, so token is at response.body.token.
  2. Step 2: Identify incorrect property usage

    Using response.token is undefined, causing cookie to be set with undefined value.
  3. Final Answer:

    response.token is undefined; should use response.body.token -> Option A
  4. Quick Check:

    Access API data via response.body [OK]
Quick Trick: API response data is inside response.body, not response directly [OK]
Common Mistakes:
  • Accessing response properties directly
  • Believing cy.setCookie() can't be in .then()
  • Thinking POST is invalid for cy.request()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes