Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

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

Simon Knott8896454cdf32
diff --git a/packages/playwright/src/index.ts b/packages/playwright/src/index.ts
index 4017a7b00b..597be6a8ba 100644
--- a/packages/playwright/src/index.ts
+++ b/packages/playwright/src/index.ts
@@ -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
diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts
index a6e10b53a4..a65b013914 100644
--- a/tests/playwright-test/reporter-html.spec.ts
+++ b/tests/playwright-test/reporter-html.spec.ts
@@ -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"');
+ });
});
}