-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] is there a way to take screenshots at the exact moment of the failure? #14854
Comments
+1 for this feature ( yes, I know, 👍 is enough, but :) ) |
There is nothing built-in for this. However, it is easy to write an / example.spec.ts
import { test, expect } from '@playwright/test';
test.afterEach(async ({ page }, 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 });
}
});
test('my test', async ({ page }) => {
// ...
}); |
if one has many specs - this will be a sad copy-paste :( |
You can make it two lines per test file :) // helper.ts
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 });
}
}
// example.spec.ts
import { screenshotOnFailure } from './helper';
test.afterEach(screenshotOnFailure); |
thanks for the workaround! but is there a particular reason for the current behavior? for me, the screenshot option |
@watson28 It makes sense, but is a bit more nuanced. For example, if failure happens during the |
Hello, I apologize in advance if my question is not related to this topic. |
For anyone looking for an alternative solution, Playwright offers a
|
@cj-larsen this is what the author doesn't want - it makes the screen at the very end, not the exact fail moment |
Of all the possible solutions this was the only one able to solve my problem exactly the way I needed it. |
Let me explain in detail. Test:
The first two fail, but the test is not stopped because all expects are soft. |
is there a way to take a fullPage screenshot while using {
use: {
screenshot: 'only-on-failure'
}
} ? |
hi , i also have same issue as arsenpapoyan described above. |
Hi! ✌️ @dgozman Sorry, but I don't understand why you can not do a screenshot on the |
You use I'm not saying having this a default behaviour would not be nice, but it can be put in place. Am I missing something here? |
Playwright supports the ability to take screenshots on failed tests: https://playwright.dev/docs/test-configuration#automatic-screenshots.
However, we need to take screenshots at the exact moment of the failure and not after the test cycle. The reason is that we have
afterEach
/afterAll
hooks that change the states of the page, and after their execution, the issue is no longer visible.Is there a way to achieve this, or can it be added as a new feature?
The text was updated successfully, but these errors were encountered: