chore: add _browserTypes helpers to playwright

This commit is contained in:
Simon Knott 2025-02-04 16:10:08 +01:00
parent 5d82567346
commit 447cda49d6
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
3 changed files with 35 additions and 25 deletions

View file

@ -49,7 +49,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel> imple
_defaultContextOptions?: BrowserContextOptions; _defaultContextOptions?: BrowserContextOptions;
_defaultContextTimeout?: number; _defaultContextTimeout?: number;
_defaultContextNavigationTimeout?: number; _defaultContextNavigationTimeout?: number;
private _defaultLaunchOptions?: LaunchOptions; _defaultLaunchOptions?: LaunchOptions;
static from(browserType: channels.BrowserTypeChannel): BrowserType { static from(browserType: channels.BrowserTypeChannel): BrowserType {
return (browserType as any)._object; return (browserType as any)._object;

View file

@ -73,4 +73,16 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel> {
static from(channel: channels.PlaywrightChannel): Playwright { static from(channel: channels.PlaywrightChannel): Playwright {
return (channel as any)._object; return (channel as any)._object;
} }
_browserTypes(): BrowserType[] {
return [this.chromium, this.firefox, this.webkit, this._bidiChromium, this._bidiFirefox];
}
_allContexts() {
return this._browserTypes().flatMap(type => [...type._contexts]);
}
_allPages() {
return this._allContexts().flatMap(context => context.pages());
}
} }

View file

@ -24,6 +24,7 @@ import type { TestInfoImpl, TestStepInternal } from './worker/testInfo';
import { rootTestType } from './common/testType'; import { rootTestType } from './common/testType';
import type { ContextReuseMode } from './common/config'; import type { ContextReuseMode } from './common/config';
import type { ApiCallData, ClientInstrumentation, ClientInstrumentationListener } from '../../playwright-core/src/client/clientInstrumentation'; import type { ApiCallData, ClientInstrumentation, ClientInstrumentationListener } from '../../playwright-core/src/client/clientInstrumentation';
import type { Playwright as PlaywrightImpl } from '../../playwright-core/src/client/playwright';
import { currentTestInfo } from './common/globals'; import { currentTestInfo } from './common/globals';
export { expect } from './matchers/expect'; export { expect } from './matchers/expect';
export const _baseTest: TestType<{}, {}> = rootTestType.test; export const _baseTest: TestType<{}, {}> = rootTestType.test;
@ -50,6 +51,7 @@ type TestFixtures = PlaywrightTestArgs & PlaywrightTestOptions & {
}; };
type WorkerFixtures = PlaywrightWorkerArgs & PlaywrightWorkerOptions & { type WorkerFixtures = PlaywrightWorkerArgs & PlaywrightWorkerOptions & {
playwright: PlaywrightImpl;
_browserOptions: LaunchOptions; _browserOptions: LaunchOptions;
_optionContextReuseMode: ContextReuseMode, _optionContextReuseMode: ContextReuseMode,
_optionConnectOptions: PlaywrightWorkerOptions['connectOptions'], _optionConnectOptions: PlaywrightWorkerOptions['connectOptions'],
@ -83,11 +85,11 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
options.channel = channel; options.channel = channel;
options.tracesDir = tracing().tracesDir(); options.tracesDir = tracing().tracesDir();
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit, playwright._bidiChromium, playwright._bidiFirefox]) for (const browserType of playwright._browserTypes())
(browserType as any)._defaultLaunchOptions = options; browserType._defaultLaunchOptions = options;
await use(options); await use(options);
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit, playwright._bidiChromium, playwright._bidiFirefox]) for (const browserType of playwright._browserTypes())
(browserType as any)._defaultLaunchOptions = undefined; browserType._defaultLaunchOptions = undefined;
}, { scope: 'worker', auto: true, box: true }], }, { scope: 'worker', auto: true, box: true }],
browser: [async ({ playwright, browserName, _browserOptions, connectOptions, _reuseContext }, use, testInfo) => { browser: [async ({ playwright, browserName, _browserOptions, connectOptions, _reuseContext }, use, testInfo) => {
@ -230,20 +232,21 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
testInfo.snapshotSuffix = process.platform; testInfo.snapshotSuffix = process.platform;
if (debugMode()) if (debugMode())
(testInfo as TestInfoImpl)._setDebugMode(); (testInfo as TestInfoImpl)._setDebugMode();
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit]) {
(browserType as any)._defaultContextOptions = _combinedContextOptions; for (const browserType of playwright._browserTypes()) {
(browserType as any)._defaultContextTimeout = actionTimeout || 0; browserType._defaultContextOptions = _combinedContextOptions;
(browserType as any)._defaultContextNavigationTimeout = navigationTimeout || 0; browserType._defaultContextTimeout = actionTimeout || 0;
browserType._defaultContextNavigationTimeout = navigationTimeout || 0;
} }
(playwright.request as any)._defaultContextOptions = { ..._combinedContextOptions }; playwright.request._defaultContextOptions = { ..._combinedContextOptions };
(playwright.request as any)._defaultContextOptions.tracesDir = tracing().tracesDir(); playwright.request._defaultContextOptions.tracesDir = tracing().tracesDir();
(playwright.request as any)._defaultContextOptions.timeout = actionTimeout || 0; playwright.request._defaultContextOptions.timeout = actionTimeout || 0;
await use(); await use();
(playwright.request as any)._defaultContextOptions = undefined; playwright.request._defaultContextOptions = undefined;
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit]) { for (const browserType of playwright._browserTypes()) {
(browserType as any)._defaultContextOptions = undefined; browserType._defaultContextOptions = undefined;
(browserType as any)._defaultContextTimeout = undefined; browserType._defaultContextTimeout = undefined;
(browserType as any)._defaultContextNavigationTimeout = undefined; browserType._defaultContextNavigationTimeout = undefined;
} }
}, { auto: 'all-hooks-included', title: 'context configuration', box: true } as any], }, { auto: 'all-hooks-included', title: 'context configuration', box: true } as any],
@ -451,7 +454,6 @@ const playwrightFixtures: Fixtures<TestFixtures, WorkerFixtures> = ({
}); });
type ScreenshotOption = PlaywrightWorkerOptions['screenshot'] | undefined; type ScreenshotOption = PlaywrightWorkerOptions['screenshot'] | undefined;
type Playwright = PlaywrightWorkerArgs['playwright'];
function normalizeVideoMode(video: VideoMode | 'retry-with-video' | { mode: VideoMode } | undefined): VideoMode { function normalizeVideoMode(video: VideoMode | 'retry-with-video' | { mode: VideoMode } | undefined): VideoMode {
if (!video) if (!video)
@ -525,7 +527,7 @@ function connectOptionsFromEnv() {
class ArtifactsRecorder { class ArtifactsRecorder {
private _testInfo!: TestInfoImpl; private _testInfo!: TestInfoImpl;
private _playwright: Playwright; private _playwright: PlaywrightImpl;
private _artifactsDir: string; private _artifactsDir: string;
private _screenshotMode: ScreenshotMode; private _screenshotMode: ScreenshotMode;
private _screenshotOptions: { mode: ScreenshotMode } & Pick<playwrightLibrary.PageScreenshotOptions, 'fullPage' | 'omitBackground'> | undefined; private _screenshotOptions: { mode: ScreenshotMode } & Pick<playwrightLibrary.PageScreenshotOptions, 'fullPage' | 'omitBackground'> | undefined;
@ -536,7 +538,7 @@ class ArtifactsRecorder {
private _screenshottedSymbol: symbol; private _screenshottedSymbol: symbol;
private _startedCollectingArtifacts: symbol; private _startedCollectingArtifacts: symbol;
constructor(playwright: Playwright, artifactsDir: string, screenshot: ScreenshotOption) { constructor(playwright: PlaywrightImpl, artifactsDir: string, screenshot: ScreenshotOption) {
this._playwright = playwright; this._playwright = playwright;
this._artifactsDir = artifactsDir; this._artifactsDir = artifactsDir;
this._screenshotMode = normalizeScreenshotMode(screenshot); this._screenshotMode = normalizeScreenshotMode(screenshot);
@ -678,11 +680,7 @@ class ArtifactsRecorder {
} }
private async _screenshotOnTestFailure() { private async _screenshotOnTestFailure() {
const contexts: BrowserContext[] = []; await Promise.all(this._playwright._allPages().map(page => this._screenshotPage(page, false)));
for (const browserType of [this._playwright.chromium, this._playwright.firefox, this._playwright.webkit])
contexts.push(...(browserType as any)._contexts);
const pages = contexts.map(ctx => ctx.pages()).flat();
await Promise.all(pages.map(page => this._screenshotPage(page, false)));
} }
private async _startTraceChunkOnContextCreation(tracing: Tracing) { private async _startTraceChunkOnContextCreation(tracing: Tracing) {