A Playwright Target closed error usually means the browser, page, or browser context that the test was using is no longer available. The failure can appear when execution is interrupted, the browser disconnects, or the test tries to use a page after it has already been closed.
The important thing is to find what caused the target to close, rather than treating Target closed itself as the root cause.
Leapwork Play gives you a managed execution workflow around your Playwright tests, making the failed test and the point where execution stopped part of the Play run rather than leaving you with only a cryptic Playwright exception.
What does Target closed mean in Playwright?
A simplified failure might look like:
page.click: Target page, context or browser has been closed
In simple terms, the test was still trying to continue after the browser or page was no longer available.
-
The test starts running normally.
-
The browser, page, or context becomes unavailable.
-
The test tries to run the next action.
-
Playwright reports
Target closed.
This means the step shown in the error is not always the step that caused the problem. It may simply be the first step that noticed the browser or page had already closed.
For example, if the browser disconnects before this step:
await page
.getByRole('button', { name: 'Submit' })
.click();
the click can report Target closed, even though the button itself has nothing to do with the failure.
How does Play help with this kind of failure?
Play gives you a clearer way to investigate this error by showing the failed run as part of the full test execution. When Target closed appears, open the failed Play run and review the steps leading up to the failure. Look for the last step that completed successfully, then check what happened immediately before the browser, page, or context became unavailable.
This helps you focus on the real cause of the failure instead of only the Playwright error message. In many cases, the reported step is just the first action that tried to use a page or browser that had already closed.
What usually causes Target closed?
Start by checking whether something explicitly or unexpectedly ended the browser state the next action depends on.
Common areas to investigate include:
|
Check |
What to look for |
|---|---|
|
Page lifecycle |
Was the page closed before the next action? |
|
Browser context |
Was the context closed while another step still needed it? |
|
Browser process |
Did the browser terminate or disconnect? |
|
Test lifecycle |
Did cleanup run before asynchronous work completed? |
|
Navigation/workflow |
Did the application close or replace the page the test expected to use? |
The key is to investigate what happened before the error, not only the line where Playwright finally reported it.
Check for asynchronous work after the page closes
A common pattern to investigate is work that continues after the page or context has already been closed.
For example:
await page.close();
await page
.getByRole('button', { name: 'Submit' })
.click();
The second action cannot succeed because its target page no longer exists.
Less obvious cases can occur when asynchronous operations are not awaited correctly.
Make sure an operation that depends on the page finishes before teardown or another part of the test closes it.
Check where the browser or page is closed
Look for lifecycle operations such as:
await page.close();
or:
await context.close();
and confirm they occur only after all actions that depend on that page or context have completed.
Also check shared fixtures, hooks, and cleanup logic if the close is not visible in the failing test itself.
Don't fix Target closed with a longer timeout
A timeout does not restore a browser or page that has already closed.
Adding:
await page.waitForTimeout(5000);
before the failing action can make the failure slower without addressing the cause.
Instead, determine why the expected target was no longer available when Playwright attempted the action.
How Play extends the Playwright debugging workflow
|
With Playwright |
With Leapwork Play |
|---|---|
|
|
Play runs the test as part of a managed execution |
|
Investigate what happened before the failing Playwright command |
Start from the failed Play execution and identify where execution stopped |
|
Debug the lifecycle of the page/context/browser |
Work with the failure in the context of the Play test steps |
|
Fix the underlying Playwright/test/application problem |
Keep Playwright underneath while using the Play execution workflow |
Play does not change what Target closed means. It provides the execution workflow around the Playwright test; the underlying cause still needs to be identified and fixed.
Best practices
-
Use the Play execution flow to identify where the test stopped before investigating the final Playwright exception.
-
Treat
Target closedas a symptom until you identify what closed or disconnected. -
Make sure asynchronous Playwright operations are awaited.
-
Do not close a page or context while another operation still depends on it.
-
Check teardown and fixture cleanup when the target appears to close unexpectedly.
-
Do not add arbitrary waits to a closed-target failure.
Troubleshooting
|
Problem |
What to check |
|---|---|
|
|
Check what happens to the page/browser immediately before that step. |
|
The test closes a page explicitly |
Make sure no later operation still uses that page. |
|
Failure appears during cleanup |
Check whether asynchronous test work is still running when teardown begins. |
|
Browser disconnects during execution |
Investigate why the browser execution ended unexpectedly. |
|
Increasing the timeout does not help |
Stop treating the failure as a timing problem and inspect the target lifecycle instead. |
Frequently asked questions
What does Target closed mean in Playwright?
It means the page, browser context, or browser required by the Playwright operation is no longer available.
Why does the error point to a normal action like click()?
The action may simply be the first operation that tries to use the target after it became unavailable. Check what happened before the reported failure.
Will increasing the Playwright timeout fix Target closed?
Usually not. A longer timeout does not restore a page or browser that has already closed or disconnected.
How should I debug Target closed?
Find the last successful operation, then determine what happened to the page, browser context, or browser before the failing action executed.
How does Leapwork Play help with Target closed?
Play provides the managed execution workflow around the Playwright test, so you can investigate where the execution stopped. The underlying reason the browser or page became unavailable still needs to be identified.