From 192071d5bc2c494be9bb839ea26f06356568ad93 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Wed, 15 Dec 2021 16:06:10 -0800 Subject: [PATCH] feat(test runner): save traces for beforeAll/afterAll hooks (#10950) --- packages/playwright-test/src/index.ts | 5 ++--- tests/playwright-test/playwright.artifacts.spec.ts | 10 ++++++++++ tests/playwright-test/reporter-html.spec.ts | 6 +++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/playwright-test/src/index.ts b/packages/playwright-test/src/index.ts index e66c148090..a4c20b68a3 100644 --- a/packages/playwright-test/src/index.ts +++ b/packages/playwright-test/src/index.ts @@ -262,9 +262,8 @@ export const test = _baseTest.extend({ // 3. Determine whether we need the artifacts. const testFailed = testInfo.status !== testInfo.expectedStatus; - const isHook = !!hookType(testInfo); - const preserveTrace = captureTrace && !isHook && (traceMode === 'on' || (testFailed && traceMode === 'retain-on-failure') || (traceMode === 'on-first-retry' && testInfo.retry === 1)); - const captureScreenshots = !isHook && (screenshot === 'on' || (screenshot === 'only-on-failure' && testFailed)); + const preserveTrace = captureTrace && (traceMode === 'on' || (testFailed && traceMode === 'retain-on-failure') || (traceMode === 'on-first-retry' && testInfo.retry === 1)); + const captureScreenshots = (screenshot === 'on' || (screenshot === 'only-on-failure' && testFailed)); const traceAttachments: string[] = []; const addTraceAttachment = () => { diff --git a/tests/playwright-test/playwright.artifacts.spec.ts b/tests/playwright-test/playwright.artifacts.spec.ts index 572382a66c..a48788ed56 100644 --- a/tests/playwright-test/playwright.artifacts.spec.ts +++ b/tests/playwright-test/playwright.artifacts.spec.ts @@ -145,6 +145,10 @@ test('should work with screenshot: on', async ({ runInlineTest }, testInfo) => { ' test-failed-1.png', 'artifacts-persistent-passing', ' test-finished-1.png', + 'artifacts-shared-afterAll-worker0', + ' test-finished-1.png', + 'artifacts-shared-beforeAll-worker0', + ' test-finished-1.png', 'artifacts-shared-shared-failing', ' test-failed-1.png', 'artifacts-shared-shared-passing', @@ -210,6 +214,10 @@ test('should work with trace: on', async ({ runInlineTest }, testInfo) => { ' trace.zip', 'artifacts-persistent-passing', ' trace.zip', + 'artifacts-shared-afterAll-worker0', + ' trace.zip', + 'artifacts-shared-beforeAll-worker0', + ' trace.zip', 'artifacts-shared-shared-failing', ' trace.zip', 'artifacts-shared-shared-passing', @@ -269,6 +277,8 @@ test('should work with trace: on-first-retry', async ({ runInlineTest }, testInf ' trace.zip', 'artifacts-persistent-failing-retry1', ' trace.zip', + 'artifacts-shared-beforeAll-worker1-retry1', + ' trace.zip', 'artifacts-shared-shared-failing-retry1', ' trace.zip', 'artifacts-two-contexts-failing-retry1', diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts index 524b229150..ed9d677d5e 100644 --- a/tests/playwright-test/reporter-html.spec.ts +++ b/tests/playwright-test/reporter-html.spec.ts @@ -299,9 +299,12 @@ test('should render beforeAll/afterAll hooks', async ({ runInlineTest, page, sho const result = await runInlineTest({ 'a.test.js': ` const { test } = pwt; + test.use({ trace: 'on' }); test.beforeAll(async () => { }); - test.afterAll(async () => { + test.afterAll(async ({ browser }) => { + const page = await browser.newPage(); + await page.close(); await test.step('after step', () => { throw new Error('oh!'); }); @@ -328,6 +331,7 @@ test('should render beforeAll/afterAll hooks', async ({ runInlineTest, page, sho await page.click('text=afterAll'); await expect(page.locator('.tree-item:has-text("after step") svg.color-text-danger')).toHaveCount(1); + await expect(page.locator('img')).toBeVisible(); }); test('should render text attachments as text', async ({ runInlineTest, page, showReport }) => {