lecture 15 of 190 completed

Hooks: setup that runs for you

before, beforeEach, after and afterEach. Which one to use is a decision about test independence, not style.

What you'll learn

  • What before, beforeEach, after and afterEach do
  • Why beforeEach protects test independence
  • Why assertions do not belong inside hooks

You write five tests for the contacts page. Every one starts with the same two lines: log in, open the page. Copying them is tedious, and when the login flow changes you edit five places.

Hooks solve this. A hook is code that runs around your tests automatically.

full course

Keep reading with the full course

The Foundations track and the first lessons in each framework are free. This lesson needs full course access. A course plan includes all lessons for Playwright, Cypress and Selenium.

See course plans →