From 0861d296303aca76f8aba1e9072e55d3e3aa9847 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Tue, 17 Dec 2024 09:58:34 +0100 Subject: [PATCH] fix playwright.trace tests --- packages/playwright/src/index.ts | 8 ++++---- tests/playwright-test/playwright.trace.spec.ts | 6 ++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts index 27f1e9320b..1e70bfc151 100644 --- a/packages/playwright/src/index.ts +++ b/packages/playwright/src/index.ts @@ -638,7 +638,7 @@ class ArtifactsRecorder { try { const screenshotPath = this._createScreenshotAttachmentPath(); await fs.promises.rename(file, screenshotPath); - this._attachScreenshot(screenshotPath); + await this._attachScreenshot(screenshotPath); } catch { } } @@ -665,14 +665,14 @@ class ArtifactsRecorder { if (temporary) this._temporaryScreenshots.push(screenshotPath); else - this._attachScreenshot(screenshotPath); + await this._attachScreenshot(screenshotPath); } catch { // Screenshot may fail, just ignore. } } - private _attachScreenshot(screenshotPath: string) { - this._testInfo.attachments.push({ name: 'screenshot', path: screenshotPath, contentType: 'image/png' }); + private async _attachScreenshot(screenshotPath: string) { + await this._testInfo.attach('screenshot', { path: screenshotPath, contentType: 'image/png' }); } private async _screenshotOnTestFailure() { diff --git a/tests/playwright-test/playwright.trace.spec.ts b/tests/playwright-test/playwright.trace.spec.ts index 5c5d6c304a..b4ee572d71 100644 --- a/tests/playwright-test/playwright.trace.spec.ts +++ b/tests/playwright-test/playwright.trace.spec.ts @@ -540,7 +540,7 @@ test('should include attachments by default', async ({ runInlineTest, server }, contentType: 'text/plain', sha1: expect.any(String), }]); - expect([...trace.resources.keys()].filter(f => f.startsWith('resources/'))).toHaveLength(1); + expect([...trace.resources.keys()]).toContain(`resources/${trace.actions[1].attachments[0].sha1}`); }); test('should opt out of attachments', async ({ runInlineTest, server }, testInfo) => { @@ -566,7 +566,7 @@ test('should opt out of attachments', async ({ runInlineTest, server }, testInfo 'After Hooks', ]); expect(trace.actions[1].attachments).toEqual(undefined); - expect([...trace.resources.keys()].filter(f => f.startsWith('resources/'))).toHaveLength(0); + expect([...trace.resources.keys()].filter(f => f.startsWith('resources/'))).toEqual([expect.stringMatching(/^resources\/src@.*$/)]); }); test('should record with custom page fixture', async ({ runInlineTest }, testInfo) => { @@ -730,8 +730,6 @@ test('should not throw when attachment is missing', async ({ runInlineTest }, te expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); - const trace = await parseTrace(testInfo.outputPath('test-results', 'a-passes', 'trace.zip')); - expect(trace.actionTree).toContain('attach "screenshot"'); }); test('should not throw when screenshot on failure fails', async ({ runInlineTest, server }, testInfo) => {