microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.39k stars 3.63k forks source link

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

Open s-sivasubramaniam opened 1 year ago

s-sivasubramaniam commented 1 year ago

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?

aslushnikov commented 1 year ago

@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!

moshikd commented 1 year ago

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

aslushnikov commented 1 year ago

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

cheshi-mantu commented 1 year ago

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

vigneshwarselvaraj92 commented 1 year ago

this will be great feature

Ash2803 commented 1 year ago

gents

++++ waiting for this feature

soofianazneen commented 1 year ago

++

aidenstern commented 1 year ago

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

etwood24 commented 1 year ago

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

jonasclaes commented 1 year ago

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.

ramsar7002 commented 1 year ago

This will be a great feature

nizzel55 commented 10 months ago

Any news about this feature?

vitalets commented 8 months ago

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](https://github.com/microsoft/playwright/assets/1473072/5a9decd6-8fb6-4f68-a9f3-21a684664d66)
Screenshots in HTML report: ![image](https://github.com/microsoft/playwright/assets/1473072/e04fdfe7-fd7e-45ed-bc78-f7eac7799f60)
rakhmatullov commented 6 months ago
ardikun1412 commented 4 months ago

+++

abautu commented 1 month ago

+1