Playwright "Target page, context or browser has been closed"
A Playwright action ran against a page/context/browser that was already closed. Either teardown raced the action, a navigation tore down the context, or the browser process crashed mid-test.
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.
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
An async action (a pending click, a dangling listener) runs after the test’s 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 you are still using.
How to fix it
Await all actions before teardown
Make sure every action and assertion is awaited so nothing runs after the test ends.
test('checkout', async ({ page }) => {
await page.goto('/cart');
await page.getByRole('button', { name: 'Checkout' }).click();
await expect(page).toHaveURL(/\/success/);
}); // fixture closes the page only after the body fully resolvesInvestigate crashes with a trace
- Run with
--trace onand inspect whether the browser crashed (renderer OOM) before the action. - If it crashed for memory, lower parallel
workersor raise container memory/--shm-size. - Avoid sharing one page across tests that close it; use per-test fixtures.
How to prevent it
- Await every Playwright action and assertion.
- Use per-test page/context fixtures, not shared globals.
- Size workers and container memory so the browser does not crash.