Bird
0
0

You want to write a Cypress test that logs in using token-based authentication and then visits a protected page. Which approach correctly ensures the token is set before visiting the page?

hard📝 Application Q8 of 15
Cypress - Authentication and Sessions
You want to write a Cypress test that logs in using token-based authentication and then visits a protected page. Which approach correctly ensures the token is set before visiting the page?
AUse <code>cy.request</code> to get the token, then set it in a cookie inside <code>.then()</code>, and finally visit the page within the same <code>.then()</code> callback
BCall <code>cy.visit</code> immediately after <code>cy.request</code> without waiting for the token
CSet the token in a variable and visit the page outside any Cypress command callbacks
DUse <code>cy.request</code> and <code>cy.visit</code> in separate tests to avoid dependency
Step-by-Step Solution
Solution:
  1. Step 1: Understand Cypress command chaining

    Cypress commands are asynchronous and must be chained properly.
  2. Step 2: Ensure token is set before visiting

    Visiting the page must happen after the token is set, inside the .then() callback.
  3. Step 3: Evaluate options

    Use cy.request to get the token, then set it in a cookie inside .then(), and finally visit the page within the same .then() callback correctly sequences the commands; others ignore async nature or separate dependent steps.
  4. Final Answer:

    Use cy.request to get the token, then set it in a cookie inside .then(), and finally visit the page within the same .then() callback -> Option A
  5. Quick Check:

    Chain commands to ensure token set before visit [OK]
Quick Trick: Chain token set and visit inside .then() [OK]
Common Mistakes:
  • Visiting page before token is set
  • Using variables outside Cypress command chain
  • Splitting dependent steps into separate tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes