Isolation
Every test in Playwright starts with a clean slate: its own cookies, localStorage and session — completely separate from every other test. That's browser contexts at work. The practical consequence: I never need to clean up state between tests, and I can run any test in any order without worrying about leftover state from another test.
What is a browser context
A browser context is like a fresh incognito window — isolated from everything else. It has its own cookies, localStorage, sessionStorage and cache. Playwright creates one per test automatically. When the test finishes, the context is thrown away.
The key thing about isolation: tests can't affect each other. If test A logs in and stores a session cookie, test B starts from zero — no cookie, no session, no state carryover.
Why isolation matters
Without isolation, tests share state. One failed test can corrupt state for ten others. That's the worst kind of bug — a test fails, but the problem isn't in that test. Isolation solves this:
- A failing test only affects itself — no cascade
- You can run any single test in any order without setup
- Parallel execution just works — no race conditions on shared state
The alternative — cleaning up state between tests — sounds fine in theory but breaks in practice. You forget to clean something, or some things are impossible to clean (like visited link styles). Start fresh every time instead.
Context in Playwright Test
When you use @playwright/test, you get page and context as fixtures — both already set up and isolated for your test. You don't create them manually. Two tests running at the same time each get their own completely separate context.
Multiple contexts in one test
Sometimes you need two users at once — for example, testing a chat or checking that admin actions affect regular users. You can create multiple contexts manually within a single test, each acting as a different user.