Skip to content
Latchkey

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.

Playwright output
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.

cart.spec.ts
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 resolves

Investigate crashes with a trace

  1. Run with --trace on and inspect whether the browser crashed (renderer OOM) before the action.
  2. If it crashed for memory, lower parallel workers or raise container memory/--shm-size.
  3. 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.

Frequently asked questions

What causes ""Target ... has been closed""?
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.
How do I fix "Target ... has been closed"?
Make sure every action and assertion is awaited so nothing runs after the test ends.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card