diff --git a/packages/playwright-core/src/client/browser.ts b/packages/playwright-core/src/client/browser.ts index be47ddeb51..10cac4d0f9 100644 --- a/packages/playwright-core/src/client/browser.ts +++ b/packages/playwright-core/src/client/browser.ts @@ -80,7 +80,7 @@ export class Browser extends ChannelOwner implements ap } async _innerNewContext(options: BrowserContextOptions = {}, forReuse: boolean): Promise { - options = { ...this._browserType._defaultContextOptions, ...options }; + options = { ...this._browserType._playwright._defaultContextOptions, ...options }; const contextOptions = await prepareBrowserContextParams(options); const response = forReuse ? await this._channel.newContextForReuse(contextOptions) : await this._channel.newContext(contextOptions); const context = BrowserContext.from(response.context); diff --git a/packages/playwright-core/src/client/browserType.ts b/packages/playwright-core/src/client/browserType.ts index 8776aeb1e7..1932d6e2d8 100644 --- a/packages/playwright-core/src/client/browserType.ts +++ b/packages/playwright-core/src/client/browserType.ts @@ -18,7 +18,7 @@ import type * as channels from '@protocol/channels'; import { Browser } from './browser'; import { BrowserContext, prepareBrowserContextParams } from './browserContext'; import { ChannelOwner } from './channelOwner'; -import type { LaunchOptions, LaunchServerOptions, ConnectOptions, LaunchPersistentContextOptions, BrowserContextOptions, Logger } from './types'; +import type { LaunchOptions, LaunchServerOptions, ConnectOptions, LaunchPersistentContextOptions, Logger } from './types'; import { Connection } from './connection'; import { Events } from './events'; import type { ChildProcess } from 'child_process'; @@ -45,11 +45,6 @@ export class BrowserType extends ChannelOwner imple _contexts = new Set(); _playwright!: Playwright; - // Instrumentation. - _defaultContextOptions?: BrowserContextOptions; - _defaultContextTimeout?: number; - _defaultContextNavigationTimeout?: number; - static from(browserType: channels.BrowserTypeChannel): BrowserType { return (browserType as any)._object; } @@ -93,7 +88,7 @@ export class BrowserType extends ChannelOwner imple async launchPersistentContext(userDataDir: string, options: LaunchPersistentContextOptions = {}): Promise { const logger = options.logger || this._playwright._defaultLaunchOptions?.logger; assert(!(options as any).port, 'Cannot specify a port without launching as a server.'); - options = { ...this._playwright._defaultLaunchOptions, ...this._defaultContextOptions, ...options }; + options = { ...this._playwright._defaultLaunchOptions, ...this._playwright._defaultContextOptions, ...options }; const contextParams = await prepareBrowserContextParams(options); const persistentParams: channels.BrowserTypeLaunchPersistentContextParams = { ...contextParams, @@ -236,10 +231,10 @@ export class BrowserType extends ChannelOwner imple context._browserType = this; this._contexts.add(context); context._setOptions(contextOptions, browserOptions); - if (this._defaultContextTimeout !== undefined) - context.setDefaultTimeout(this._defaultContextTimeout); - if (this._defaultContextNavigationTimeout !== undefined) - context.setDefaultNavigationTimeout(this._defaultContextNavigationTimeout); + if (this._playwright._defaultContextTimeout !== undefined) + context.setDefaultTimeout(this._playwright._defaultContextTimeout); + if (this._playwright._defaultContextNavigationTimeout !== undefined) + context.setDefaultNavigationTimeout(this._playwright._defaultContextNavigationTimeout); await this._instrumentation.runAfterCreateBrowserContext(context); } diff --git a/packages/playwright-core/src/client/fetch.ts b/packages/playwright-core/src/client/fetch.ts index 58928532ac..8bae02e21c 100644 --- a/packages/playwright-core/src/client/fetch.ts +++ b/packages/playwright-core/src/client/fetch.ts @@ -57,30 +57,29 @@ export class APIRequest implements api.APIRequest { private _playwright: Playwright; readonly _contexts = new Set(); - // Instrumentation. - _defaultContextOptions?: NewContextOptions & { tracesDir?: string }; - constructor(playwright: Playwright) { this._playwright = playwright; } async newContext(options: NewContextOptions = {}): Promise { - options = { ...this._defaultContextOptions, ...options }; + options = { + ...this._playwright._defaultContextOptions, + ...options, + timeout: this._playwright._defaultActionTimeout ?? options.timeout, + }; const storageState = typeof options.storageState === 'string' ? JSON.parse(await fs.promises.readFile(options.storageState, 'utf8')) : options.storageState; - // We do not expose tracesDir in the API, so do not allow options to accidentally override it. - const tracesDir = this._defaultContextOptions?.tracesDir; const context = APIRequestContext.from((await this._playwright._channel.newRequest({ ...options, extraHTTPHeaders: options.extraHTTPHeaders ? headersObjectToArray(options.extraHTTPHeaders) : undefined, storageState, - tracesDir, + tracesDir: this._playwright._tracesDir, clientCertificates: await toClientCertificatesProtocol(options.clientCertificates), })).request); this._contexts.add(context); context._request = this; - context._tracing._tracesDir = tracesDir; + context._tracing._tracesDir = this._playwright._tracesDir; await context._instrumentation.runAfterCreateRequestContext(context); return context; } diff --git a/packages/playwright-core/src/client/playwright.ts b/packages/playwright-core/src/client/playwright.ts index 50ccb0c1d9..f13af6b1f9 100644 --- a/packages/playwright-core/src/client/playwright.ts +++ b/packages/playwright-core/src/client/playwright.ts @@ -22,7 +22,7 @@ import { ChannelOwner } from './channelOwner'; import { Electron } from './electron'; import { APIRequest } from './fetch'; import { Selectors, SelectorsOwner } from './selectors'; -import type { LaunchOptions } from './types'; +import type { BrowserContextOptions, LaunchOptions } from 'playwright-core'; export class Playwright extends ChannelOwner { readonly _android: Android; @@ -37,7 +37,13 @@ export class Playwright extends ChannelOwner { readonly request: APIRequest; readonly errors: { TimeoutError: typeof TimeoutError }; + // Instrumentation. _defaultLaunchOptions?: LaunchOptions; + _defaultContextOptions?: BrowserContextOptions; + _defaultContextTimeout?: number; + _defaultContextNavigationTimeout?: number; + _defaultActionTimeout?: number; + _tracesDir?: string; constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PlaywrightInitializer) { super(parent, type, guid, initializer); diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts index 89a63a6cba..5966bd6c95 100644 --- a/packages/playwright/src/index.ts +++ b/packages/playwright/src/index.ts @@ -231,21 +231,15 @@ const playwrightFixtures: Fixtures = ({ if (debugMode()) (testInfo as TestInfoImpl)._setDebugMode(); - for (const browserType of playwright._browserTypes()) { - browserType._defaultContextOptions = _combinedContextOptions; - browserType._defaultContextTimeout = actionTimeout || 0; - browserType._defaultContextNavigationTimeout = navigationTimeout || 0; - } - playwright.request._defaultContextOptions = { ..._combinedContextOptions }; - playwright.request._defaultContextOptions.tracesDir = tracing().tracesDir(); - playwright.request._defaultContextOptions.timeout = actionTimeout || 0; + playwright._defaultContextOptions = _combinedContextOptions; + playwright._defaultContextTimeout = actionTimeout || 0; + playwright._defaultContextNavigationTimeout = navigationTimeout || 0; + playwright._tracesDir = tracing().tracesDir(); await use(); - playwright.request._defaultContextOptions = undefined; - for (const browserType of playwright._browserTypes()) { - browserType._defaultContextOptions = undefined; - browserType._defaultContextTimeout = undefined; - browserType._defaultContextNavigationTimeout = undefined; - } + playwright._defaultContextOptions = undefined; + playwright._defaultContextTimeout = undefined; + playwright._defaultContextNavigationTimeout = undefined; + playwright._tracesDir = undefined; }, { auto: 'all-hooks-included', title: 'context configuration', box: true } as any], _setupArtifacts: [async ({ playwright, screenshot }, use, testInfo) => {