Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: fix cross browser leak tests (#32843)
Pavel Feldmanbcb6860ef59a
diff --git a/packages/playwright-core/src/client/jsHandle.ts b/packages/playwright-core/src/client/jsHandle.tsindex bb85894870..8577400f3d 100644--- a/packages/playwright-core/src/client/jsHandle.ts+++ b/packages/playwright-core/src/client/jsHandle.ts@@ -78,13 +78,6 @@ export class JSHandle<T = any> extends ChannelOwner<channels.JSHandleChannel> im}}- async _objectCount() {- return await this._wrapApiCall(async () => {- const { count } = await this._channel.objectCount();- return count;- });- }-override toString(): string {return this._preview;}diff --git a/packages/playwright-core/src/protocol/validator.ts b/packages/playwright-core/src/protocol/validator.tsindex 4069b906c7..c8cd65e47e 100644--- a/packages/playwright-core/src/protocol/validator.ts+++ b/packages/playwright-core/src/protocol/validator.ts@@ -1843,12 +1843,6 @@ scheme.JSHandleJsonValueResult = tObject({value: tType('SerializedValue'),});scheme.ElementHandleJsonValueResult = tType('JSHandleJsonValueResult');-scheme.JSHandleObjectCountParams = tOptional(tObject({}));-scheme.ElementHandleObjectCountParams = tType('JSHandleObjectCountParams');-scheme.JSHandleObjectCountResult = tObject({- count: tNumber,-});-scheme.ElementHandleObjectCountResult = tType('JSHandleObjectCountResult');scheme.ElementHandleInitializer = tObject({preview: tString,});diff --git a/packages/playwright-core/src/server/bidi/bidiExecutionContext.ts b/packages/playwright-core/src/server/bidi/bidiExecutionContext.tsindex c037ba44b4..201b3f999d 100644--- a/packages/playwright-core/src/server/bidi/bidiExecutionContext.ts+++ b/packages/playwright-core/src/server/bidi/bidiExecutionContext.ts@@ -122,10 +122,6 @@ export class BidiExecutionContext implements js.ExecutionContextDelegate {});}- objectCount(objectId: js.ObjectId): Promise<number> {- throw new Error('Method not implemented.');- }-async rawCallFunction(functionDeclaration: string, arg: bidi.Script.LocalValue): Promise<bidi.Script.RemoteValue> {const response = await this._session.send('script.callFunction', {functionDeclaration,diff --git a/packages/playwright-core/src/server/chromium/crExecutionContext.ts b/packages/playwright-core/src/server/chromium/crExecutionContext.tsindex c6952f5c82..d54505d283 100644--- a/packages/playwright-core/src/server/chromium/crExecutionContext.ts+++ b/packages/playwright-core/src/server/chromium/crExecutionContext.ts@@ -102,14 +102,6 @@ export class CRExecutionContext implements js.ExecutionContextDelegate {async releaseHandle(objectId: js.ObjectId): Promise<void> {await releaseObject(this._client, objectId);}-- async objectCount(objectId: js.ObjectId): Promise<number> {- const result = await this._client.send('Runtime.queryObjects', {- prototypeObjectId: objectId- });- const match = result.objects.description!.match(/Array\((\d+)\)/)!;- return +match[1];- }}function rewriteError(error: Error): Protocol.Runtime.evaluateReturnValue {diff --git a/packages/playwright-core/src/server/dispatchers/jsHandleDispatcher.ts b/packages/playwright-core/src/server/dispatchers/jsHandleDispatcher.tsindex 07e9d38ee5..33960e72d5 100644--- a/packages/playwright-core/src/server/dispatchers/jsHandleDispatcher.ts+++ b/packages/playwright-core/src/server/dispatchers/jsHandleDispatcher.ts@@ -63,10 +63,6 @@ export class JSHandleDispatcher extends Dispatcher<js.JSHandle, channels.JSHandlreturn { value: serializeResult(await this._object.jsonValue()) };}- async objectCount(params?: channels.JSHandleObjectCountParams | undefined): Promise<channels.JSHandleObjectCountResult> {- return { count: await this._object.objectCount() };- }-async dispose(_: any, metadata: CallMetadata) {metadata.potentiallyClosesScope = true;this._object.dispose();diff --git a/packages/playwright-core/src/server/firefox/ffExecutionContext.ts b/packages/playwright-core/src/server/firefox/ffExecutionContext.tsindex 3a6b931a47..ba981da942 100644--- a/packages/playwright-core/src/server/firefox/ffExecutionContext.ts+++ b/packages/playwright-core/src/server/firefox/ffExecutionContext.ts@@ -98,10 +98,6 @@ export class FFExecutionContext implements js.ExecutionContextDelegate {objectId});}-- objectCount(objectId: js.ObjectId): Promise<number> {- throw new Error('Method not implemented in Firefox.');- }}function checkException(exceptionDetails?: Protocol.Runtime.ExceptionDetails) {diff --git a/packages/playwright-core/src/server/javascript.ts b/packages/playwright-core/src/server/javascript.tsindex 663df78b5b..40dee4888a 100644--- a/packages/playwright-core/src/server/javascript.ts+++ b/packages/playwright-core/src/server/javascript.ts@@ -58,7 +58,6 @@ export interface ExecutionContextDelegate {getProperties(context: ExecutionContext, objectId: ObjectId): Promise<Map<string, JSHandle>>;createHandle(context: ExecutionContext, remoteObject: RemoteObject): JSHandle;releaseHandle(objectId: ObjectId): Promise<void>;- objectCount(objectId: ObjectId): Promise<number>;}export class ExecutionContext extends SdkObject {@@ -126,10 +125,6 @@ export class ExecutionContext extends SdkObject {return this._utilityScriptPromise;}- async objectCount(objectId: ObjectId): Promise<number> {- return this._delegate.objectCount(objectId);- }-async doSlowMo() {// overridden in FrameExecutionContext}@@ -246,12 +241,6 @@ export class JSHandle<T = any> extends SdkObject {if (this._previewCallback)this._previewCallback(preview);}-- async objectCount(): Promise<number> {- if (!this._objectId)- throw new Error('Can only count objects for a handle that points to the constructor prototype');- return this._context.objectCount(this._objectId);- }}export async function evaluate(context: ExecutionContext, returnByValue: boolean, pageFunction: Function | string, ...args: any[]): Promise<any> {diff --git a/packages/playwright-core/src/server/webkit/wkExecutionContext.ts b/packages/playwright-core/src/server/webkit/wkExecutionContext.tsindex 8d6bbff084..4416df7a9e 100644--- a/packages/playwright-core/src/server/webkit/wkExecutionContext.ts+++ b/packages/playwright-core/src/server/webkit/wkExecutionContext.ts@@ -116,10 +116,6 @@ export class WKExecutionContext implements js.ExecutionContextDelegate {async releaseHandle(objectId: js.ObjectId): Promise<void> {await this._session.send('Runtime.releaseObject', { objectId });}-- objectCount(objectId: js.ObjectId): Promise<number> {- throw new Error('Method not implemented in WebKit.');- }}function potentiallyUnserializableValue(remoteObject: Protocol.Runtime.RemoteObject): any {diff --git a/packages/protocol/src/channels.ts b/packages/protocol/src/channels.tsindex b99490a955..5a4985c253 100644--- a/packages/protocol/src/channels.ts+++ b/packages/protocol/src/channels.ts@@ -3226,7 +3226,6 @@ export interface JSHandleChannel extends JSHandleEventTarget, Channel {getPropertyList(params?: JSHandleGetPropertyListParams, metadata?: CallMetadata): Promise<JSHandleGetPropertyListResult>;getProperty(params: JSHandleGetPropertyParams, metadata?: CallMetadata): Promise<JSHandleGetPropertyResult>;jsonValue(params?: JSHandleJsonValueParams, metadata?: CallMetadata): Promise<JSHandleJsonValueResult>;- objectCount(params?: JSHandleObjectCountParams, metadata?: CallMetadata): Promise<JSHandleObjectCountResult>;}export type JSHandlePreviewUpdatedEvent = {preview: string,@@ -3278,11 +3277,6 @@ export type JSHandleJsonValueOptions = {};export type JSHandleJsonValueResult = {value: SerializedValue,};-export type JSHandleObjectCountParams = {};-export type JSHandleObjectCountOptions = {};-export type JSHandleObjectCountResult = {- count: number,-};export interface JSHandleEvents {'previewUpdated': JSHandlePreviewUpdatedEvent;diff --git a/packages/protocol/src/protocol.yml b/packages/protocol/src/protocol.ymlindex e9891adf6b..c89c2b96f2 100644--- a/packages/protocol/src/protocol.yml+++ b/packages/protocol/src/protocol.yml@@ -2488,10 +2488,6 @@ JSHandle:returns:value: SerializedValue- objectCount:- returns:- count: number-events:previewUpdated:diff --git a/tests/page/page-leaks.spec.ts b/tests/page/page-leaks.spec.tsindex ce356f1e12..abd33e651e 100644--- a/tests/page/page-leaks.spec.ts+++ b/tests/page/page-leaks.spec.ts@@ -41,16 +41,36 @@ function leakedJSHandles(): string {return lines.join('\n');}-async function objectCounts(pageImpl, constructorName: string): Promise<{ main: number, utility: number }> {+async function weakRefObjects(pageImpl: any, selector: string) {+ for (const world of ['main', 'utility']) {+ const context = await pageImpl.mainFrame()._context(world);+ await context.evaluate(selector => {+ const elements = document.querySelectorAll(selector);+ globalThis.weakRefs = globalThis.weakRefs || [];+ for (const element of elements)+ globalThis.weakRefs.push(new WeakRef(element));+ }, selector);+ }+}++async function weakRefCount(pageImpl): Promise<{ main: number, utility: number }> {const result = { main: 0, utility: 0 };for (const world of ['main', 'utility']) {+ await pageImpl.requestGC();const context = await pageImpl.mainFrame()._context(world);- const prototype = await context.evaluateHandle(name => (window as any)[name].prototype, constructorName);- result[world] = await prototype.objectCount();+ result[world] = await context.evaluate(() => globalThis.weakRefs.filter(r => !!r.deref()).length);}return result;}+async function checkWeakRefs(pageImpl, from: number, to: number) {+ await expect(async () => {+ const counts = await weakRefCount(pageImpl);+ expect(counts.main + counts.utility).toBeGreaterThanOrEqual(from);+ expect(counts.main + counts.utility).toBeLessThan(to);+ }).toPass();+}+test.beforeEach(() => {(globalThis as any).leakedJSHandles = new Map();});@@ -59,14 +79,12 @@ test.afterEach(() => {(globalThis as any).leakedJSHandles = null;});-test('click should not leak', async ({ page, browserName, toImpl }) => {+test('click should not leak', async ({ page, toImpl }) => {await page.setContent(`<button>static button 1</button><button>static button 2</button><div id="buttons"></div>`);- // Create JS wrappers for static elements.- await page.evaluate(() => document.querySelectorAll('button'));for (let i = 0; i < 25; ++i) {await page.evaluate(i => {@@ -74,24 +92,19 @@ test('click should not leak', async ({ page, browserName, toImpl }) => {element.textContent = 'dynamic ' + i;document.getElementById('buttons').appendChild(element);}, i);- await page.locator('#buttons > button').click();- await page.evaluate(() => {- document.getElementById('buttons').textContent = '';- });+ await page.locator('#buttons > button').last().click();}- expect(leakedJSHandles()).toBeFalsy();+ await weakRefObjects(toImpl(page), 'button');+ await page.evaluate(() => {+ document.getElementById('buttons').textContent = '';+ });- if (browserName === 'chromium') {- await expect(async () => {- const counts = await objectCounts(toImpl(page), 'HTMLButtonElement');- expect(counts.main + counts.utility).toBeGreaterThanOrEqual(2);- expect(counts.main + counts.utility).toBeLessThan(25);- }).toPass();- }+ expect(leakedJSHandles()).toBeFalsy();+ await checkWeakRefs(toImpl(page), 2, 25);});-test('fill should not leak', async ({ page, mode, browserName, toImpl }) => {+test('fill should not leak', async ({ page, mode, toImpl }) => {test.skip(mode !== 'default');await page.setContent(`@@ -99,32 +112,25 @@ test('fill should not leak', async ({ page, mode, browserName, toImpl }) => {<input value="static input 2"</input><div id="inputs"></div>`);- // Create JS wrappers for static elements.- await page.evaluate(() => document.querySelectorAll('input'));for (let i = 0; i < 25; ++i) {- await page.evaluate(i => {+ await page.evaluate(() => {const element = document.createElement('input');document.getElementById('inputs').appendChild(element);- }, i);- await page.locator('#inputs > input').fill('input ' + i);- await page.evaluate(() => {- document.getElementById('inputs').textContent = '';});+ await page.locator('#inputs > input').last().fill('input ' + i);}- expect(leakedJSHandles()).toBeFalsy();+ await weakRefObjects(toImpl(page), 'input');+ await page.evaluate(() => {+ document.getElementById('inputs').textContent = '';+ });- if (browserName === 'chromium') {- await expect(async () => {- const counts = await objectCounts(toImpl(page), 'HTMLInputElement');- expect(counts.main + counts.utility).toBeGreaterThanOrEqual(2);- expect(counts.main + counts.utility).toBeLessThan(25);- }).toPass();- }+ expect(leakedJSHandles()).toBeFalsy();+ await checkWeakRefs(toImpl(page), 2, 25);});-test('expect should not leak', async ({ page, mode, browserName, toImpl }) => {+test('expect should not leak', async ({ page, mode, toImpl }) => {test.skip(mode !== 'default');await page.setContent(`@@ -132,6 +138,7 @@ test('expect should not leak', async ({ page, mode, browserName, toImpl }) => {<button>static button 2</button><div id="buttons"></div>`);+ await weakRefObjects(toImpl(page), 'button');for (let i = 0; i < 25; ++i) {await page.evaluate(i => {@@ -139,24 +146,19 @@ test('expect should not leak', async ({ page, mode, browserName, toImpl }) => {element.textContent = 'dynamic ' + i;document.getElementById('buttons').appendChild(element);}, i);- await expect(page.locator('#buttons > button')).toBeVisible();- await page.evaluate(() => {- document.getElementById('buttons').textContent = '';- });+ await expect(page.locator('#buttons > button').last()).toBeVisible();}- expect(leakedJSHandles()).toBeFalsy();+ await weakRefObjects(toImpl(page), 'button');+ await page.evaluate(() => {+ document.getElementById('buttons').textContent = '';+ });- if (browserName === 'chromium') {- await expect(async () => {- const counts = await objectCounts(toImpl(page), 'HTMLButtonElement');- expect(counts.main + counts.utility).toBeGreaterThanOrEqual(2);- expect(counts.main + counts.utility).toBeLessThan(25);- }).toPass();- }+ expect(leakedJSHandles()).toBeFalsy();+ await checkWeakRefs(toImpl(page), 2, 25);});-test('waitFor should not leak', async ({ page, mode, browserName, toImpl }) => {+test('waitFor should not leak', async ({ page, mode, toImpl }) => {test.skip(mode !== 'default');await page.setContent(`@@ -171,19 +173,14 @@ test('waitFor should not leak', async ({ page, mode, browserName, toImpl }) => {element.textContent = 'dynamic ' + i;document.getElementById('buttons').appendChild(element);}, i);- await page.locator('#buttons > button').waitFor();- await page.evaluate(() => {- document.getElementById('buttons').textContent = '';- });+ await page.locator('#buttons > button').last().waitFor();}- expect(leakedJSHandles()).toBeFalsy();+ await weakRefObjects(toImpl(page), 'button');+ await page.evaluate(() => {+ document.getElementById('buttons').textContent = '';+ });- if (browserName === 'chromium') {- await expect(async () => {- const counts = await objectCounts(toImpl(page), 'HTMLButtonElement');- expect(counts.main + counts.utility).toBeGreaterThanOrEqual(2);- expect(counts.main + counts.utility).toBeLessThan(25);- }).toPass();- }+ expect(leakedJSHandles()).toBeFalsy();+ await checkWeakRefs(toImpl(page), 2, 25);});diff --git a/tests/page/page-object-count.spec.ts b/tests/page/page-object-count.spec.tsdeleted file mode 100644index 57377b76e7..0000000000--- a/tests/page/page-object-count.spec.ts+++ /dev/null@@ -1,25 +0,0 @@-/**- * Copyright (c) Microsoft Corporation.- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */--import { test, expect } from './pageTest';--test('should count objects', async ({ page, browserName }) => {- test.skip(browserName !== 'chromium');- await page.setContent('<button>Submit</button>');- await page.evaluate(() => document.querySelectorAll('button'));- const proto = await page.evaluateHandle(() => HTMLButtonElement.prototype);- expect(await (proto as any)._objectCount()).toBe(1);-});