Playwright "Target page, context or browser has been closed"
By Daniel Zoghalchali·Latchkey
A Playwright action ran against a page/context/browser that was already closed. Teardown raced the action, a navigation tore down the context, or the browser process crashed mid-test (often a renderer OOM).
What this error means
An action fails with "Target page, context or browser has been closed." It can be intermittent - a fixture closed the page slightly before an in-flight action, or a renderer crash took the page down.
playwright
Error: page.click: Target page, context or browser has been closed
at CartPage.checkout (tests/cart.spec.ts:42:16)
Common causes
Using the page after teardown
A pending action or dangling listener runs after the fixture already closed the page or context, so the target is gone.
Browser crash or forced navigation
A renderer OOM/crash, or a full-page navigation that replaced the context, invalidates the handle still in use.
How to fix it
Await all actions before teardown
Ensure every action and assertion is awaited so nothing runs after the test body resolves.
Run with --trace on and check whether the browser crashed (renderer OOM) before the action.
If it crashed for memory, lower parallel workers or raise container memory/--shm-size.
Use per-test page/context fixtures, never a shared page that gets closed.
How to prevent it
Await every Playwright action and assertion.
Use per-test fixtures, not shared globals.
Size workers and container memory so the browser does not crash.
Frequently asked questions
What causes ""Target ... has been closed""?
A pending action or dangling listener runs after the fixture already closed the page or context, so the target is gone.
How do I fix "Target ... has been closed"?
Ensure every action and assertion is awaited so nothing runs after the test body resolves.
Can Latchkey fix this automatically?
Yes. Latchkey runs your GitHub Actions on managed runners that detect this failure, apply the fix, and retry the job automatically - self-healing is on by default.