Skip to content

Instantly share code, notes, and snippets.

@stevenroh
Created June 21, 2023 08:15
Show Gist options
  • Save stevenroh/b9c40a78c209aa690d18df5c741838a1 to your computer and use it in GitHub Desktop.
Save stevenroh/b9c40a78c209aa690d18df5c741838a1 to your computer and use it in GitHub Desktop.
Playwright screenshot on failure
import { screenshotOnFailure } from './helper';
test.afterEach(screenshotOnFailure);
import type { TestInfo } from '@playwright/test';
export async function screenshotOnFailure({ page }: { page: Page }, testInfo: TestInfo) {
if (testInfo.status !== testInfo.expectedStatus) {
// Get a unique place for the screenshot.
const screenshotPath = testInfo.outputPath(`failure.png`);
// Add it to the report.
testInfo.attachments.push({ name: 'screenshot', path: screenshotPath, contentType: 'image/png' });
// Take the screenshot itself.
await page.screenshot({ path: screenshotPath, timeout: 5000 });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment