playwright: [Question] is there a way to take screenshots at the exact moment of the failure?

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?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 30
  • Comments: 15 (3 by maintainers)

Most upvoted comments

For anyone looking for a screenshot solution (might not be exact moment of failure), Playwright offers a screenshot option that can be set under the use: { ... } section in the config.ts

use: {
    /* Screenshot on failure. */
    screenshot: "only-on-failure",
  }

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);

There is nothing built-in for this. However, it is easy to write an afterEach hook that does this. Just make sure to put this hook before all other hooks.

/ 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 }) => {
  // ...
});

is there a way to take a fullPage screenshot while using

{
  use: {
    screenshot: 'only-on-failure'
  }
}

?

Hello, I apologize in advance if my question is not related to this topic. What if all my test assertions are expect.soft, and when one of them fails the execution continues and the screenshot is taken at the end, thus that is not a fail but just a screenshot of the last action. How can I get exact screenshot on failure in this case?

@cj-larsen this is what the author doesn’t want - it makes the screen at the very end, not the exact fail moment

@watson28 It makes sense, but is a bit more nuanced. For example, if failure happens during the afterEach hook, then we should probably screenshot at that moment. I’ll see what we can do.

Hi! ✌️ @dgozman

Sorry, but I don’t understand why you can not do a screenshot on the expect.soft as like on the expect failure. I think it is the main feature for the fast and fulfilment testing Maybe you can advise another realization for configure expect.soft (maybe create a custom assertion) to solve this significant omission?

hi , i also have same issue as arsenpapoyan described above. We cannot leverage .soft in our tests coz of screenshots not being able to be taken at that exact failing moment. Any solutions will be appreciated

thanks for the workaround!

but is there a particular reason for the current behavior? for me, the screenshot option only-on-failure will make more sense to work at the exact moment of the failure and not afterward. I feel that should be the default behavior or at least supported as a new option. That will avoid those conflicts of not being able to take the screenshot of the failure if you have defined after* hooks.