Bird
0
0

What is wrong with this programmatic login code?

medium📝 Debug Q7 of 15
Cypress - Authentication and Sessions
What is wrong with this programmatic login code?
cy.request({
  method: 'POST',
  url: '/login',
  body: { username: 'user', password: 'pass' }
}).then((response) => {
  cy.setCookie('authToken', response.body.token);
});
Aresponse.body.token might be undefined if login fails
BMissing await keyword before cy.request
Ccy.setCookie cannot be used inside then()
DCookies cannot be set programmatically in Cypress
Step-by-Step Solution
Solution:
  1. Step 1: Understand response handling

    If login fails, response.body.token may be undefined.

  2. Step 2: Identify potential issue

    Setting cookie with undefined token causes test errors.

  3. Final Answer:

    response.body.token might be undefined if login fails -> Option A
  4. Quick Check:

    Check token exists before setting cookie [OK]
Quick Trick: Verify token exists before setting cookie [OK]
Common Mistakes:
  • Assuming token always exists
  • Misusing cy.setCookie inside promise
  • Expecting await with cy.request (Cypress commands are chained)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes