fix(prompt): take snapshot for unclosed contexts as well (#35387)

Signed-off-by: Simon Knott <info@simonknott.de>
Co-authored-by: Dmitry Gozman <dgozman@gmail.com>
This commit is contained in:
Simon Knott 2025-03-27 16:25:46 +01:00 committed by GitHub
parent f5a7465a41
commit 8896454cdf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 9 deletions

View File

@ -668,14 +668,22 @@ class ArtifactsRecorder {
await this._stopTracing(context.tracing);
await this._screenshotRecorder.captureTemporary(context);
await this._takePageSnapshot(context);
}
if (!process.env.PLAYWRIGHT_NO_COPY_PROMPT && this._testInfo.errors.length > 0) {
try {
const page = context.pages()[0];
// TODO: maybe capture snapshot when the error is created, so it's from the right page and right time
this._pageSnapshot ??= await page?.locator('body').ariaSnapshot({ timeout: 5000 });
} catch {}
}
private async _takePageSnapshot(context: BrowserContext) {
if (process.env.PLAYWRIGHT_NO_COPY_PROMPT)
return;
if (this._testInfo.errors.length === 0)
return;
if (this._pageSnapshot)
return;
const page = context.pages()[0];
try {
// TODO: maybe capture snapshot when the error is created, so it's from the right page and right time
this._pageSnapshot = await page?.locator('body').ariaSnapshot({ timeout: 5000 });
} catch {}
}
async didCreateRequestContext(context: APIRequestContext) {
@ -707,6 +715,11 @@ class ArtifactsRecorder {
})));
await this._screenshotRecorder.persistTemporary();
const context = leftoverContexts[0];
if (context)
await this._takePageSnapshot(context);
if (this._attachErrorContext)
await attachErrorContext(this._testInfo, this._pageSnapshot);
else

View File

@ -2808,8 +2808,6 @@ for (const useIntermediateMergeReport of [true, false] as const) {
test('should not show prompt for empty timeout error', async ({ runInlineTest, showReport, page }) => {
const result = await runInlineTest({
'uncommitted.txt': `uncommitted file`,
'playwright.config.ts': `export default {}`,
'example.spec.ts': `
import { test, expect } from '@playwright/test';
test('sample', async ({ page }) => {
@ -2823,6 +2821,27 @@ for (const useIntermediateMergeReport of [true, false] as const) {
await page.getByRole('link', { name: 'sample' }).click();
await expect(page.getByRole('button', { name: 'Copy prompt' })).toHaveCount(1);
});
test('should include snapshot when page wasnt closed', async ({ runInlineTest, showReport, page }) => {
const result = await runInlineTest({
'example.spec.ts': `
import { test, expect } from '@playwright/test';
test('sample', async ({ browser }) => {
const page = await browser.newPage();
await page.setContent('<button>Click me</button>');
expect(2).toBe(3);
});
`,
}, { reporter: 'dot,html' }, { PLAYWRIGHT_HTML_OPEN: 'never' });
expect(result.exitCode).toBe(1);
await showReport();
await page.context().grantPermissions(['clipboard-read', 'clipboard-write']);
await page.getByRole('link', { name: 'sample' }).click();
await page.getByRole('button', { name: 'Copy prompt' }).click();
const prompt = await page.evaluate(() => navigator.clipboard.readText());
expect(prompt, 'contains snapshot').toContain('- button "Click me"');
});
});
}