Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat: Include stderr/stdout in the HTML reporter 'Copy Prompt' context (#36836)
Copilot49cff6aaf775
diff --git a/packages/html-reporter/src/testResultView.tsx b/packages/html-reporter/src/testResultView.tsxindex c8f8f04029..8fa4ab017d 100644--- a/packages/html-reporter/src/testResultView.tsx+++ b/packages/html-reporter/src/testResultView.tsx@@ -91,6 +91,11 @@ export const TestResultView: React.FC<{}, [result]);const prompt = useAsyncMemo(async () => {+ const stdoutAttachment = result.attachments.find(a => a.name === 'stdout');+ const stderrAttachment = result.attachments.find(a => a.name === 'stderr');+ const stdout = stdoutAttachment?.body && stdoutAttachment.contentType === 'text/plain' ? stdoutAttachment.body : undefined;+ const stderr = stderrAttachment?.body && stderrAttachment.contentType === 'text/plain' ? stderrAttachment.body : undefined;+return await copyPrompt({testInfo: [`- Name: ${test.path.join(' >> ')} >> ${test.title}`,@@ -100,6 +105,8 @@ export const TestResultView: React.FC<{errorContext: errorContext?.path ? await fetch(errorContext.path!).then(r => r.text()) : errorContext?.body,errors: result.errors,buildCodeFrame: async error => error.codeframe,+ stdout,+ stderr,});}, [test, errorContext, testRunMetadata, result], undefined);diff --git a/packages/web/src/shared/prompts.ts b/packages/web/src/shared/prompts.tsindex a88d8bce74..1abe7cfcc0 100644--- a/packages/web/src/shared/prompts.ts+++ b/packages/web/src/shared/prompts.ts@@ -31,6 +31,8 @@ export async function copyPrompt<ErrorInfo extends { message: string }>({errors,buildCodeFrame,+ stdout,+ stderr,}: {testInfo: string;metadata: MetadataWithCommitInfo | undefined;@@ -38,6 +40,8 @@ export async function copyPrompt<ErrorInfo extends { message: string }>({errors: ErrorInfo[];buildCodeFrame(error: ErrorInfo): Promise<string | undefined>;+ stdout?: string;+ stderr?: string;}) {const meaningfulSingleLineErrors = new Set(errors.filter(e => e.message && !e.message.includes('\n')).map(e => e.message!));for (const error of errors) {@@ -66,10 +70,16 @@ export async function copyPrompt<ErrorInfo extends { message: string }>({`# Test info`,'',testInfo,- '',- '# Error details',];+ if (stdout)+ lines.push('', '# Stdout', '', '```', stripAnsiEscapes(stdout), '```');++ if (stderr)+ lines.push('', '# Stderr', '', '```', stripAnsiEscapes(stderr), '```');++ lines.push('', '# Error details');+for (const error of meaningfulErrors) {lines.push('',diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.tsindex 9156695723..d7e0f946f3 100644--- a/tests/playwright-test/reporter-html.spec.ts+++ b/tests/playwright-test/reporter-html.spec.ts@@ -519,6 +519,37 @@ for (const useIntermediateMergeReport of [true, false] as const) {await expect(page.locator('.attachment-body').nth(1)).toHaveText('Third line');});+ test('should include stdout/stderr in copy prompt', async ({ runInlineTest, page, showReport }) => {+ const result = await runInlineTest({+ 'a.test.js': `+ import { test, expect } from '@playwright/test';+ test('fails', async ({ page }) => {+ console.log('Output line 1');+ process.stdout.write('Output line 2\\n');+ console.error('Error line 1');+ process.stderr.write('Error line 2\\n');+ await expect(true).toBeFalsy();+ });+ `,+ }, { reporter: 'dot,html' }, { PLAYWRIGHT_HTML_OPEN: 'never' });+ expect(result.exitCode).toBe(1);+ expect(result.failed).toBe(1);++ await showReport();+ await page.context().grantPermissions(['clipboard-read', 'clipboard-write']);++ await page.getByRole('link', { name: 'fails' }).click();+ await page.getByRole('button', { name: 'Copy prompt' }).click();+ await page.waitForFunction(() => navigator.clipboard.readText());+ const prompt = await page.evaluate(() => navigator.clipboard.readText());+ expect(prompt, 'should contain stdout content').toContain('Output line 1');+ expect(prompt, 'should contain stdout content').toContain('Output line 2');+ expect(prompt, 'should contain stderr content').toContain('Error line 1');+ expect(prompt, 'should contain stderr content').toContain('Error line 2');+ expect(prompt, 'should contain stdout section').toContain('# Stdout');+ expect(prompt, 'should contain stderr section').toContain('# Stderr');+ });+test('should highlight error', async ({ runInlineTest, page, showReport }) => {const result = await runInlineTest({'a.test.js': `