move some more

This commit is contained in:
Simon Knott 2025-02-06 10:46:14 +01:00
parent 0dea2f44b0
commit 8a22302e43
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
5 changed files with 29 additions and 35 deletions

View file

@ -80,7 +80,7 @@ export class Browser extends ChannelOwner<channels.BrowserChannel> implements ap
}
async _innerNewContext(options: BrowserContextOptions = {}, forReuse: boolean): Promise<BrowserContext> {
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);

View file

@ -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<channels.BrowserTypeChannel> imple
_contexts = new Set<BrowserContext>();
_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<channels.BrowserTypeChannel> imple
async launchPersistentContext(userDataDir: string, options: LaunchPersistentContextOptions = {}): Promise<BrowserContext> {
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<channels.BrowserTypeChannel> 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);
}

View file

@ -57,30 +57,29 @@ export class APIRequest implements api.APIRequest {
private _playwright: Playwright;
readonly _contexts = new Set<APIRequestContext>();
// Instrumentation.
_defaultContextOptions?: NewContextOptions & { tracesDir?: string };
constructor(playwright: Playwright) {
this._playwright = playwright;
}
async newContext(options: NewContextOptions = {}): Promise<APIRequestContext> {
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;
}

View file

@ -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<channels.PlaywrightChannel> {
readonly _android: Android;
@ -37,7 +37,13 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel> {
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);

View file

@ -231,21 +231,15 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
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) => {