playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question] Is there a way to capture screenshot at the end of each test.step?

Open s-sivasubramaniam opened this issue 2 years ago • 13 comments

I understand playwright captures screenshot at the end of each test if we keep "screenshot: on" in playwright.config.js. But is there a way to do it at the end of each test.step as well and include it in html reporter? as my project requires to capture screenshot after each step. I know it can be achieved with testInfo.attach which means i have to include the piece of code "await testInfo.attach('screenshot', { body: await page.screenshot(), contentType: 'image/png' });" in each test.step and but is there a simpler way to do it?

s-sivasubramaniam avatar Jan 12 '23 10:01 s-sivasubramaniam

@s-sivasubramaniam no, there's no way for now. Let me marked this as a feature request. If it becomes popular, we'll prioritize it accordingly!

aslushnikov avatar Jan 13 '23 00:01 aslushnikov

this will be a great feature. can it be implemented soon?

moshikd avatar Jan 13 '23 22:01 moshikd

@moshikd please upvote the issue to bump it's priority. The more upvotes something has, the more we look onto it!

aslushnikov avatar Jan 13 '23 22:01 aslushnikov

many PW / allure report users are asking for this feature gents

cheshi-mantu avatar Jan 14 '23 18:01 cheshi-mantu

this will be great feature

vigneshwarselvaraj92 avatar Jan 15 '23 13:01 vigneshwarselvaraj92

gents

++++ waiting for this feature

Ash2803 avatar Feb 12 '23 19:02 Ash2803

++

soofianazneen avatar Mar 10 '23 11:03 soofianazneen

Would really love this feature, have a ton of "testInfo.attach"on each test.step

aidenstern avatar May 15 '23 19:05 aidenstern

Yes please - would be great to have this as a feature without needing testInfo.attach scatterd throughout the code.

etwood24 avatar Jul 06 '23 04:07 etwood24

If this existed in the code, I would use it immediately!

I think something like a test.beforeEachStep and a test.afterEachStep function would be really useful. This would allow for other things to be called as well.

jonasclaes avatar Jul 21 '23 17:07 jonasclaes

This will be a great feature

ramsar7002 avatar Jul 30 '23 09:07 ramsar7002

Any news about this feature?

nizzel55 avatar Dec 07 '23 14:12 nizzel55

Until this is implemented I use the following custom fixture step:

import { test as base, expect } from '@playwright/test';

type StepFn = (title: string, fn: () => unknown) => unknown;

const test = base.extend<{step: StepFn}>({
  step: async ({ page }, use, testInfo) => {
    await use(async (title, fn) => {
      await test.step(title, async () => {
        try {
          await fn();
        } finally {
          await testInfo.attach(`screenshot after step "${title}"`, { 
            body: await page.screenshot(), 
            contentType: 'image/png' 
          }); 
        }
      });
    });
  }
});

Usage in test:

  test('Check title', async ({ page, step }) => {
    await step('I open url', async () => {
       await page.goto('https://playwright.dev');
    });
    await step('I click link', async () => {
      await page.getByRole('link', { name: 'Get started' }).click();
    });
    await step('I check title', async () => {
      await expect(page).toHaveTitle('Installation | Playwright');
    })
  });
Steps in HTML report:

image

Screenshots in HTML report:

image

vitalets avatar Feb 02 '24 17:02 vitalets

  • for the test step screenshotting feature to be added so that screenshots can be available in the report after each test step.

rakhmatullov avatar Apr 12 '24 12:04 rakhmatullov

+++

ardikun1412 avatar May 29 '24 07:05 ardikun1412