Bird
0
0

You want to test a form that requires logging in before each test and clearing local storage after each test. Which is the best way to organize your Cypress hooks?

hard📝 framework Q15 of 15
Cypress - Writing Tests
You want to test a form that requires logging in before each test and clearing local storage after each test. Which is the best way to organize your Cypress hooks?
AUse only <code>beforeEach</code> for both login and clearing local storage
BUse <code>afterEach</code> to log in and <code>beforeEach</code> to clear local storage
CUse only <code>afterEach</code> for both login and clearing local storage
DUse <code>beforeEach</code> to log in and <code>afterEach</code> to clear local storage
Step-by-Step Solution
Solution:
  1. Step 1: Identify setup and cleanup tasks

    Logging in is setup needed before each test; clearing local storage is cleanup after each test.
  2. Step 2: Assign tasks to correct hooks

    Use beforeEach for setup (login) and afterEach for cleanup (clear local storage).
  3. Final Answer:

    Use beforeEach to log in and afterEach to clear local storage -> Option D
  4. Quick Check:

    Setup beforeEach, cleanup afterEach [OK]
Quick Trick: Setup in beforeEach, cleanup in afterEach [OK]
Common Mistakes:
  • Mixing setup and cleanup in wrong hooks
  • Using only one hook for both tasks
  • Not cleaning up after tests

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes