From 0dea2f44b0f970b29096bd95d2cafb12cca0e1eb Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 4 Feb 2025 17:29:18 +0100 Subject: [PATCH] more --- .../playwright-core/src/client/browserType.ts | 11 ++++---- .../playwright-core/src/client/playwright.ts | 3 ++ packages/playwright/src/index.ts | 28 ++++++------------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/packages/playwright-core/src/client/browserType.ts b/packages/playwright-core/src/client/browserType.ts index 567fdc3c76..8776aeb1e7 100644 --- a/packages/playwright-core/src/client/browserType.ts +++ b/packages/playwright-core/src/client/browserType.ts @@ -49,7 +49,6 @@ export class BrowserType extends ChannelOwner imple _defaultContextOptions?: BrowserContextOptions; _defaultContextTimeout?: number; _defaultContextNavigationTimeout?: number; - _defaultLaunchOptions?: LaunchOptions; static from(browserType: channels.BrowserTypeChannel): BrowserType { return (browserType as any)._object; @@ -69,8 +68,8 @@ export class BrowserType extends ChannelOwner imple assert(!(options as any).userDataDir, 'userDataDir option is not supported in `browserType.launch`. Use `browserType.launchPersistentContext` instead'); assert(!(options as any).port, 'Cannot specify a port without launching as a server.'); - const logger = options.logger || this._defaultLaunchOptions?.logger; - options = { ...this._defaultLaunchOptions, ...options }; + const logger = options.logger || this._playwright._defaultLaunchOptions?.logger; + options = { ...this._playwright._defaultLaunchOptions, ...options }; const launchOptions: channels.BrowserTypeLaunchParams = { ...options, ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined, @@ -87,14 +86,14 @@ export class BrowserType extends ChannelOwner imple async launchServer(options: LaunchServerOptions = {}): Promise { if (!this._serverLauncher) throw new Error('Launching server is not supported'); - options = { ...this._defaultLaunchOptions, ...options }; + options = { ...this._playwright._defaultLaunchOptions, ...options }; return await this._serverLauncher.launchServer(options); } async launchPersistentContext(userDataDir: string, options: LaunchPersistentContextOptions = {}): Promise { - const logger = options.logger || this._defaultLaunchOptions?.logger; + const logger = options.logger || this._playwright._defaultLaunchOptions?.logger; assert(!(options as any).port, 'Cannot specify a port without launching as a server.'); - options = { ...this._defaultLaunchOptions, ...this._defaultContextOptions, ...options }; + options = { ...this._playwright._defaultLaunchOptions, ...this._defaultContextOptions, ...options }; const contextParams = await prepareBrowserContextParams(options); const persistentParams: channels.BrowserTypeLaunchPersistentContextParams = { ...contextParams, diff --git a/packages/playwright-core/src/client/playwright.ts b/packages/playwright-core/src/client/playwright.ts index eb476542fb..50ccb0c1d9 100644 --- a/packages/playwright-core/src/client/playwright.ts +++ b/packages/playwright-core/src/client/playwright.ts @@ -22,6 +22,7 @@ import { ChannelOwner } from './channelOwner'; import { Electron } from './electron'; import { APIRequest } from './fetch'; import { Selectors, SelectorsOwner } from './selectors'; +import type { LaunchOptions } from './types'; export class Playwright extends ChannelOwner { readonly _android: Android; @@ -36,6 +37,8 @@ export class Playwright extends ChannelOwner { readonly request: APIRequest; readonly errors: { TimeoutError: typeof TimeoutError }; + _defaultLaunchOptions?: LaunchOptions; + constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PlaywrightInitializer) { super(parent, type, guid, initializer); this.request = new APIRequest(this); diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts index 370a7040d9..89a63a6cba 100644 --- a/packages/playwright/src/index.ts +++ b/packages/playwright/src/index.ts @@ -85,11 +85,9 @@ const playwrightFixtures: Fixtures = ({ options.channel = channel; options.tracesDir = tracing().tracesDir(); - for (const browserType of playwright._browserTypes()) - browserType._defaultLaunchOptions = options; + playwright._defaultLaunchOptions = options; await use(options); - for (const browserType of playwright._browserTypes()) - browserType._defaultLaunchOptions = undefined; + playwright._defaultLaunchOptions = undefined; }, { scope: 'worker', auto: true, box: true }], browser: [async ({ playwright, browserName, _browserOptions, connectOptions, _reuseContext }, use, testInfo) => { @@ -562,17 +560,12 @@ class ArtifactsRecorder { this._screenshotOrdinal = testInfo.attachments.filter(a => a.name === 'screenshot').length; // Process existing contexts. - for (const browserType of [this._playwright.chromium, this._playwright.firefox, this._playwright.webkit]) { - const promises: (Promise | undefined)[] = []; - const existingContexts = Array.from((browserType as any)._contexts) as BrowserContext[]; - for (const context of existingContexts) { - if ((context as any)[kIsReusedContext]) - this._reusedContexts.add(context); - else - promises.push(this.didCreateBrowserContext(context)); - } - await Promise.all(promises); - } + await Promise.all(this._playwright._allContexts().map(async context => { + if ((context as any)[kIsReusedContext]) + this._reusedContexts.add(context); + else + await this.didCreateBrowserContext(context); + })); { const existingApiRequests: APIRequestContext[] = Array.from((this._playwright.request as any)._contexts as Set); await Promise.all(existingApiRequests.map(c => this.didCreateRequestContext(c))); @@ -622,10 +615,7 @@ class ArtifactsRecorder { if (captureScreenshots) await this._screenshotOnTestFailure(); - let leftoverContexts: BrowserContext[] = []; - for (const browserType of [this._playwright.chromium, this._playwright.firefox, this._playwright.webkit]) - leftoverContexts.push(...(browserType as any)._contexts); - leftoverContexts = leftoverContexts.filter(context => !this._reusedContexts.has(context)); + const leftoverContexts = this._playwright._allContexts().filter(context => !this._reusedContexts.has(context)); const leftoverApiRequests: APIRequestContext[] = Array.from((this._playwright.request as any)._contexts as Set); // Collect traces/screenshots for remaining contexts.