Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix: allow disposing ElementHandles multiple times (#29953)

Max Schmitt5bc6ca634566
diff --git a/packages/playwright-core/src/client/jsHandle.ts b/packages/playwright-core/src/client/jsHandle.ts
index ad03609e40..bb85894870 100644
--- a/packages/playwright-core/src/client/jsHandle.ts
+++ b/packages/playwright-core/src/client/jsHandle.ts
@@ -19,6 +19,7 @@ import { ChannelOwner } from './channelOwner';
import { parseSerializedValue, serializeValue } from '../protocol/serializers';
import type * as api from '../../types/types';
import type * as structs from '../../types/structs';
+import { isTargetClosedError } from './errors';
export class JSHandle<T = any> extends ChannelOwner<channels.JSHandleChannel> implements api.JSHandle {
private _preview: string;
@@ -68,7 +69,13 @@ export class JSHandle<T = any> extends ChannelOwner<channels.JSHandleChannel> im
}
async dispose() {
- return await this._channel.dispose();
+ try {
+ await this._channel.dispose();
+ } catch (e) {
+ if (isTargetClosedError(e))
+ return;
+ throw e;
+ }
}
async _objectCount() {
diff --git a/tests/page/elementhandle-misc.spec.ts b/tests/page/elementhandle-misc.spec.ts
index 2be0d10c8a..7224e87779 100644
--- a/tests/page/elementhandle-misc.spec.ts
+++ b/tests/page/elementhandle-misc.spec.ts
@@ -85,3 +85,12 @@ it('should focus a button', async ({ page, server }) => {
await button.focus();
expect(await button.evaluate(button => document.activeElement === button)).toBe(true);
});
+
+it('should allow disposing twice', async ({ page }) => {
+ it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/29945' });
+ await page.setContent('<section>39</section>');
+ const element = await page.$('section');
+ expect(element).toBeTruthy();
+ await element.dispose();
+ await element.dispose();
+});