Bird
0
0

Identify the error in this Cypress test snippet for token-based login:

medium📝 Debug Q14 of 15
Cypress - Authentication and Sessions
Identify the error in this Cypress test snippet for token-based login:
cy.request('POST', '/api/login', { username: 'user', password: 'pass' })
  .then((response) => {
    cy.setCookie('authToken', response.token);
  });
Aresponse.token should be response.body.token to access the token
Bcy.setCookie is not a valid Cypress command
CThe request method should be GET, not POST
DCookies cannot store authentication tokens
Step-by-Step Solution
Solution:
  1. Step 1: Check response object structure

    The token is usually inside response.body.token, not directly response.token.
  2. Step 2: Validate cy.setCookie usage

    cy.setCookie is a valid command to set cookies in Cypress.
  3. Final Answer:

    response.token should be response.body.token to access the token -> Option A
  4. Quick Check:

    Access token via response.body.token [OK]
Quick Trick: Use response.body.token, not response.token [OK]
Common Mistakes:
  • Using response.token instead of response.body.token
  • Thinking cy.setCookie is invalid
  • Changing POST to GET incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes