some more refactoring

This commit is contained in:
Simon Knott 2025-01-31 10:47:43 +01:00
parent 3bafe1784e
commit a9f93120c9
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -527,27 +527,17 @@ function connectOptionsFromEnv() {
} }
class SnapshotRecorder { class SnapshotRecorder {
private _artifactsRecorder: ArtifactsRecorder;
private _ordinal = 0; private _ordinal = 0;
private _temporary: string[] = []; private _temporary: string[] = [];
private _snapshottedSymbol = Symbol('snapshotted'); private _snapshottedSymbol = Symbol('snapshotted');
private _mode: ScreenshotMode;
private _name: string;
private _contentType: string;
private _extension: string;
private _doSnapshot: (page: Page, path: string) => Promise<void>;
constructor(artifactsRecorder: ArtifactsRecorder, mode: ScreenshotMode, name: string, contentType: string, extension: string, doSnapshot: (page: Page, path: string) => Promise<void>) { constructor(
this._artifactsRecorder = artifactsRecorder; private _artifactsRecorder: ArtifactsRecorder,
this._mode = mode; private _mode: ScreenshotMode,
this._name = name; private _name: string,
this._contentType = contentType; private _contentType: string,
this._extension = extension; private _extension: string,
this._doSnapshot = doSnapshot; private _doSnapshot: (page: Page, path: string) => Promise<void>) {
}
private get testInfo(): TestInfoImpl {
return this._artifactsRecorder._testInfo;
} }
fixOrdinal() { fixOrdinal() {
@ -556,28 +546,22 @@ class SnapshotRecorder {
this._ordinal = this.testInfo.attachments.filter(a => a.name === this._name).length; this._ordinal = this.testInfo.attachments.filter(a => a.name === this._name).length;
} }
async captureTemporary(context: BrowserContext) {
if (this._mode === 'on' || this._mode === 'only-on-failure' || (this._mode === 'on-first-failure' && this.testInfo.retry === 0))
await Promise.all(context.pages().map(page => this._snapshotPage(page, true)));
}
private shouldCaptureUponFinish() { private shouldCaptureUponFinish() {
return this._mode === 'on' || return this._mode === 'on' ||
(this._mode === 'only-on-failure' && this.testInfo._isFailure()) || (this._mode === 'only-on-failure' && this.testInfo._isFailure()) ||
(this._mode === 'on-first-failure' && this.testInfo._isFailure() && this.testInfo.retry === 0); (this._mode === 'on-first-failure' && this.testInfo._isFailure() && this.testInfo.retry === 0);
} }
private _allContexts() { async maybeCapture() {
if (!this.shouldCaptureUponFinish())
return;
const contexts: BrowserContext[] = []; const contexts: BrowserContext[] = [];
const playwright = this._artifactsRecorder._playwright; const playwright = this._artifactsRecorder._playwright;
for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit]) for (const browserType of [playwright.chromium, playwright.firefox, playwright.webkit])
contexts.push(...(browserType as any)._contexts); contexts.push(...(browserType as any)._contexts);
return contexts;
}
async maybeCapture() { await Promise.all(contexts.flatMap(context => context.pages().map(page => this._snapshotPage(page, false))));
if (this.shouldCaptureUponFinish())
await Promise.all(this._allContexts().flatMap(context => context.pages().map(page => this._snapshotPage(page, false))));
} }
async persistTemporary() { async persistTemporary() {
@ -593,6 +577,11 @@ class SnapshotRecorder {
} }
} }
async captureTemporary(context: BrowserContext) {
if (this._mode === 'on' || this._mode === 'only-on-failure' || (this._mode === 'on-first-failure' && this.testInfo.retry === 0))
await Promise.all(context.pages().map(page => this._snapshotPage(page, true)));
}
private _attach(screenshotPath: string) { private _attach(screenshotPath: string) {
this.testInfo.attachments.push({ name: this._name, path: screenshotPath, contentType: this._contentType }); this.testInfo.attachments.push({ name: this._name, path: screenshotPath, contentType: this._contentType });
} }
@ -625,6 +614,10 @@ class SnapshotRecorder {
// snapshot may fail, just ignore. // snapshot may fail, just ignore.
} }
} }
private get testInfo(): TestInfoImpl {
return this._artifactsRecorder._testInfo;
}
} }
class ArtifactsRecorder { class ArtifactsRecorder {