Bird
0
0

Given the following code snippet, what will be the value of token after the test runs?

medium📝 Predict Output Q13 of 15
Cypress - Authentication and Sessions
Given the following code snippet, what will be the value of token after the test runs?
cy.request('POST', '/api/login', { username: 'test', password: '1234' })
  .then((response) => {
    const token = response.body.token
    cy.wrap(token).as('authToken')
  })
AUndefined, because <code>token</code> is declared inside the callback only.
BThe token string returned from the login API response body.
CAn error because <code>cy.wrap</code> cannot be used inside <code>then</code>.
DThe entire response object instead of just the token.
Step-by-Step Solution
Solution:
  1. Step 1: Understand cy.request and then callback

    The then receives the response object from the POST request.
  2. Step 2: Extract token from response.body.token

    The code assigns token to the token string from the response body and saves it as an alias.
  3. Final Answer:

    The token string returned from the login API response body. -> Option B
  4. Quick Check:

    Response body token is saved as alias [OK]
Quick Trick: Response body holds token; alias saves it for later [OK]
Common Mistakes:
  • Thinking token is undefined outside callback
  • Believing cy.wrap cannot be used inside then
  • Confusing response object with token string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes