Skip to content

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

chore: fix stress heap tests and chromium profile-in-use detection (#40222)

Dmitry Gozmanb87cc9462ab6
diff --git a/packages/playwright-core/src/client/index.ts b/packages/playwright-core/src/client/index.ts
deleted file mode 100644
index d0c347137f..0000000000
--- a/packages/playwright-core/src/client/index.ts
+++ /dev/null
@@ -1,17 +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.
- */
-
-export * from './eventEmitter';
diff --git a/packages/playwright-core/src/coreBundle.ts b/packages/playwright-core/src/coreBundle.ts
index 6aa199f14d..c99ff3a6e1 100644
--- a/packages/playwright-core/src/coreBundle.ts
+++ b/packages/playwright-core/src/coreBundle.ts
@@ -16,7 +16,7 @@
export * as iso from '@isomorphic/index';
export * as utils from '@utils/index';
-export * as client from './client';
+export { EventEmitter as clientEventEmitter } from './client/eventEmitter';
export * as libCli from './cli/program';
export * as libCliTestStub from './cli/programWithTestStub';
export * as inprocess from './inprocess';
diff --git a/packages/playwright-core/src/server/bidi/bidiChromium.ts b/packages/playwright-core/src/server/bidi/bidiChromium.ts
index 2e02b92958..9af2f80d28 100644
--- a/packages/playwright-core/src/server/bidi/bidiChromium.ts
+++ b/packages/playwright-core/src/server/bidi/bidiChromium.ts
@@ -22,7 +22,7 @@ import { BrowserType, kNoXServerRunningError } from '../browserType';
import { BidiBrowser } from './bidiBrowser';
import { kBrowserCloseMessageId } from './bidiConnection';
import { chromiumSwitches } from '../chromium/chromiumSwitches';
-import { waitForReadyState } from '../chromium/chromium';
+import { profileInUseError, waitForReadyState } from '../chromium/chromium';
import { shouldProxyLoopback } from '../chromium/crBrowser';
import type { BrowserOptions } from '../browser';
@@ -46,12 +46,9 @@ export class BidiChromium extends BrowserType {
try {
return BidiBrowser.connect(this.attribution.playwright, bidiTransport, options);
} catch (e) {
- if (browserLogsCollector.recentLogs().some(log => log.includes('Failed to create a ProcessSingleton for your profile directory.'))) {
- throw new Error(
- 'Failed to create a ProcessSingleton for your profile directory. ' +
- 'This usually means that the profile is already in use by another instance of Chromium.'
- );
- }
+ const error = profileInUseError(browserLogsCollector.recentLogs());
+ if (error)
+ throw error;
throw e;
}
}
diff --git a/packages/playwright-core/src/server/chromium/chromium.ts b/packages/playwright-core/src/server/chromium/chromium.ts
index 15aa8f02bf..4dfc587641 100644
--- a/packages/playwright-core/src/server/chromium/chromium.ts
+++ b/packages/playwright-core/src/server/chromium/chromium.ts
@@ -193,12 +193,9 @@ export class Chromium extends BrowserType {
try {
return await CRBrowser.connect(this.attribution.playwright, transport, options, this._devtools);
} catch (e) {
- if (browserLogsCollector.recentLogs().some(log => log.includes('Failed to create a ProcessSingleton for your profile directory.'))) {
- throw new Error(
- 'Failed to create a ProcessSingleton for your profile directory. ' +
- 'This usually means that the profile is already in use by another instance of Chromium.'
- );
- }
+ const error = profileInUseError(browserLogsCollector.recentLogs());
+ if (error)
+ throw error;
throw e;
}
}
@@ -409,16 +406,31 @@ export class Chromium extends BrowserType {
}
}
+export function profileInUseError(logs: string[]): Error | undefined {
+ const markers = [
+ 'Failed to create a ProcessSingleton for your profile directory.',
+ 'Opening in existing browser session.',
+ ];
+ for (const log of logs) {
+ const marker = markers.find(m => log.includes(m));
+ if (marker) {
+ return new Error(
+ `${marker} ` +
+ 'This usually means that the profile is already in use by another instance of Chromium.'
+ );
+ }
+ }
+}
+
export async function waitForReadyState(options: types.LaunchOptions, browserLogsCollector: RecentLogsCollector): Promise<{ wsEndpoint?: string }> {
if (!options.args?.some(a => a.startsWith('--remote-debugging-port')))
return {};
const result = new ManualPromise<{ wsEndpoint?: string }>();
browserLogsCollector.onMessage(message => {
- if (message.includes('Failed to create a ProcessSingleton for your profile directory.')) {
- result.reject(new Error('Failed to create a ProcessSingleton for your profile directory. ' +
- 'This usually means that the profile is already in use by another instance of Chromium.'));
- }
+ const error = profileInUseError([message]);
+ if (error)
+ result.reject(error);
const match = message.match(/DevTools listening on (.*)/);
if (match)
result.resolve({ wsEndpoint: match[1] });
diff --git a/tests/library/events/utils.ts b/tests/library/events/utils.ts
index 6e21379240..c4f2d581c5 100644
--- a/tests/library/events/utils.ts
+++ b/tests/library/events/utils.ts
@@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.
import { expect } from '@playwright/test';
-import { client, utils } from '../../../packages/playwright-core/lib/coreBundle';
+import { clientEventEmitter, utils } from '../../../packages/playwright-core/lib/coreBundle';
const { nodePlatform } = utils;
@@ -49,7 +49,7 @@ export const mustCall = (fn?: Function, exact?: number) => {
};
/** as any breaks long TS resolution chain that makes tests unhappy */
-export class EventEmitter extends (client as any).EventEmitter {
+export class EventEmitter extends (clientEventEmitter as any) {
constructor() {
super(nodePlatform(process.cwd()));
}
diff --git a/tests/stress/heap.spec.ts b/tests/stress/heap.spec.ts
index 938dd46a0b..f04e2202e8 100644
--- a/tests/stress/heap.spec.ts
+++ b/tests/stress/heap.spec.ts
@@ -21,6 +21,30 @@ import { queryObjectCount } from '../config/queryObjects';
test.describe.configure({ mode: 'serial' });
test.skip(({ browserName }) => browserName !== 'chromium');
+const clientClass = {
+ Page: null as Function,
+ BrowserContext: null as Function,
+ Browser: null as Function,
+ Request: null as Function,
+ Response: null as Function,
+};
+
+test.beforeAll(async ({ browser, server }) => {
+ const context = await browser.newContext();
+ const page = await context.newPage();
+ clientClass.Page = page.constructor;
+ clientClass.BrowserContext = context.constructor;
+ clientClass.Browser = browser.constructor;
+ const [request, response] = await Promise.all([
+ page.waitForRequest(() => true),
+ page.waitForResponse(() => true),
+ page.goto(server.EMPTY_PAGE),
+ ]);
+ clientClass.Request = request.constructor;
+ clientClass.Response = response.constructor;
+ await context.close();
+});
+
for (let i = 0; i < 3; ++i) {
test(`test #${i} to request page and context`, async ({ page, context }) => {
// This test is here to create page instance
@@ -32,15 +56,15 @@ test('test to request page and context', async ({ page, context }) => {
});
test('should not leak fixtures w/ page', async ({ page }) => {
- expect(await queryObjectCount(require('../../packages/playwright-core/lib/client/page').Page)).toBe(1);
- expect(await queryObjectCount(require('../../packages/playwright-core/lib/client/browserContext').BrowserContext)).toBe(1);
- expect(await queryObjectCount(require('../../packages/playwright-core/lib/client/browser').Browser)).toBe(1);
+ expect(await queryObjectCount(clientClass.Page)).toBe(1);
+ expect(await queryObjectCount(clientClass.BrowserContext)).toBe(1);
+ expect(await queryObjectCount(clientClass.Browser)).toBe(1);
});
test('should not leak fixtures w/o page', async ({}) => {
- expect(await queryObjectCount(require('../../packages/playwright-core/lib/client/page').Page)).toBe(0);
- expect(await queryObjectCount(require('../../packages/playwright-core/lib/client/browserContext').BrowserContext)).toBe(0);
- expect(await queryObjectCount(require('../../packages/playwright-core/lib/client/browser').Browser)).toBe(1);
+ expect(await queryObjectCount(clientClass.Page)).toBe(0);
+ expect(await queryObjectCount(clientClass.BrowserContext)).toBe(0);
+ expect(await queryObjectCount(clientClass.Browser)).toBe(1);
});
test('should not leak server-side objects', async ({ page }) => {
@@ -76,10 +100,10 @@ test('should not leak dispatchers after closing page', async ({ context, server
expect(await queryObjectCount(coreServer.RequestDispatcher)).toBe(0);
expect(await queryObjectCount(coreServer.ResponseDispatcher)).toBe(0);
- expect(await queryObjectCount(require('../../packages/playwright-core/lib/client/page').Page)).toBeLessThan(COUNT);
+ expect(await queryObjectCount(clientClass.Page)).toBeLessThan(COUNT);
expect(await queryObjectCount(coreServer.Page)).toBe(0);
- expect(await queryObjectCount(require('../../packages/playwright-core/lib/client/network').Request)).toBe(0);
- expect(await queryObjectCount(require('../../packages/playwright-core/lib/client/network').Response)).toBe(0);
+ expect(await queryObjectCount(clientClass.Request)).toBe(0);
+ expect(await queryObjectCount(clientClass.Response)).toBe(0);
});
test.describe(() => {
@@ -100,8 +124,8 @@ test.describe(() => {
expect(e.message).toContain('The object has been collected to prevent unbounded heap growth.');
const counts = [
- { count: await queryObjectCount(require('../../packages/playwright-core/lib/client/network').Request), message: 'client.Request' },
- { count: await queryObjectCount(require('../../packages/playwright-core/lib/client/network').Response), message: 'client.Response' },
+ { count: await queryObjectCount(clientClass.Request), message: 'client.Request' },
+ { count: await queryObjectCount(clientClass.Response), message: 'client.Response' },
{ count: await queryObjectCount(coreServer.Request), message: 'server.Request' },
{ count: await queryObjectCount(coreServer.Response), message: 'server.Response' },
{ count: await queryObjectCount(coreServer.RequestDispatcher), message: 'dispatchers.RequestDispatcher' },