Bird
0
0

What is wrong with this Cypress test setup?

medium📝 Debug Q7 of 15
Cypress - Writing Tests
What is wrong with this Cypress test setup?
describe('Tests', () => {
  beforeEach(() => {
    cy.get('#login').click()
  })

  afterEach(() => {
    cy.get('#logout').click()
  })

  it('Test 1', () => {
    cy.get('#dashboard').should('be.visible')
  })
})
AbeforeEach and afterEach should not contain UI actions
BMissing cy.visit() before UI actions
CafterEach should run before tests
DUsing cy.get in hooks without waiting can cause flaky tests
Step-by-Step Solution
Solution:
  1. Step 1: Check test setup flow

    Tests need to visit a page before interacting with UI elements.
  2. Step 2: Identify missing step

    No cy.visit() is called, so elements like '#login' may not exist, causing failures.
  3. Final Answer:

    Missing cy.visit() before UI actions -> Option B
  4. Quick Check:

    Always visit page before UI commands [OK]
Quick Trick: Always use cy.visit() before UI interactions [OK]
Common Mistakes:
  • Assuming elements exist without visiting page
  • Thinking hooks can't have UI actions
  • Confusing afterEach timing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Cypress Quizzes