From 569cb7d21cec480e3b2bb7780465238d858b0bf7 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Fri, 23 Feb 2024 16:40:45 -0600 Subject: [PATCH] test(playwright-test): add initial tests for trace on-first-failure --- .../playwright-test/playwright.trace.spec.ts | 68 ++++++++++++++++++- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/tests/playwright-test/playwright.trace.spec.ts b/tests/playwright-test/playwright.trace.spec.ts index 69f1912752..7aa0af0db8 100644 --- a/tests/playwright-test/playwright.trace.spec.ts +++ b/tests/playwright-test/playwright.trace.spec.ts @@ -14,9 +14,9 @@ * limitations under the License. */ -import { test, expect } from './playwright-test-fixtures'; -import { parseTrace, parseTraceRaw } from '../config/utils'; import fs from 'fs'; +import { parseTrace, parseTraceRaw } from '../config/utils'; +import { expect, test } from './playwright-test-fixtures'; test.describe.configure({ mode: 'parallel' }); @@ -402,7 +402,7 @@ test('should respect PW_TEST_DISABLE_TRACING', async ({ runInlineTest }, testInf expect(fs.existsSync(testInfo.outputPath('test-results', 'a-test-1', 'trace.zip'))).toBe(false); }); -for (const mode of ['off', 'retain-on-failure', 'on-first-retry', 'on-all-retries']) { +for (const mode of ['off', 'retain-on-failure', 'on-first-retry', 'on-all-retries', 'on-first-failure']) { test(`trace:${mode} should not create trace zip artifact if page test passed`, async ({ runInlineTest }) => { const result = await runInlineTest({ 'a.spec.ts': ` @@ -1034,3 +1034,65 @@ test('should attribute worker fixture teardown to the right test', async ({ runI ' step in foo teardown', ]); }); + +test('trace:on-first-failure should create trace if context is closed before failure in the test', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { use: { trace: 'on-first-failure' } }; + `, + 'a.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('passing test', async ({ page, context }) => { + await page.goto('about:blank'); + await context.close(); + expect(1).toBe(2); + }); + `, + }, { trace: 'on-first-failure' }); + const tracePath = test.info().outputPath('test-results', 'a-passing-test', 'trace.zip'); + const trace = await parseTrace(tracePath); + expect(trace.apiNames).toContain('page.goto'); + expect(result.failed).toBe(1); +}); + +test('trace:on-first-failure should create trace if context is closed before failure in afterEach', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { use: { trace: 'on-first-failure' } }; + `, + 'a.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('passing test', async ({ page, context }) => { + }); + test.afterEach(async ({ page, context }) => { + await page.goto('about:blank'); + await context.close(); + expect(1).toBe(2); + }); + `, + }, { trace: 'on-first-failure' }); + const tracePath = test.info().outputPath('test-results', 'a-passing-test', 'trace.zip'); + const trace = await parseTrace(tracePath); + expect(trace.apiNames).toContain('page.goto'); + expect(result.failed).toBe(1); +}); + +test('trace:on-first-failure should create trace if request context is disposed before failure', async ({ runInlineTest, server }) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { use: { trace: 'on-first-failure' } }; + `, + 'a.spec.ts': ` + import { test, expect } from '@playwright/test'; + test('passing test', async ({ request }) => { + expect(await request.get('${server.EMPTY_PAGE}')).toBeOK(); + await request.dispose(); + expect(1).toBe(2); + }); + `, + }, { trace: 'on-first-failure' }); + const tracePath = test.info().outputPath('test-results', 'a-passing-test', 'trace.zip'); + const trace = await parseTrace(tracePath); + expect(trace.apiNames).toContain('apiRequestContext.get'); + expect(result.failed).toBe(1); +});