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

[Feature] #28633

Closed JasKirk70 closed 10 months ago

JasKirk70 commented 10 months ago

Use case

I have to create a bug using some bug apis on test case failure which I am doing in test.afterAll method. For this I need the screenshots and video as well. Now this is easily available in reporters, but I want to access these screenshot and video to send in my bug api in test.afterAll method. is it possible?

I am doing below code in test.afterEach method but not sure if I can access these attachments through testinfo.attachments in test.afterAll method const screenshot = await page.screenshot(); await testInfo.attach('screenshot', { body: screenshot, contentType: 'image/png'});

mxschmitt commented 10 months ago

We recommend taking the resources/artifacts in the afterAll hooks and attaching them but doing the actual reporting as a custom reporter.

Have you tried that approach?

JasKirk70 commented 10 months ago

Is there any way to transfer data from the afterAll hooks to the custom reporter methods apart from writing content to a text file and attaching it. I need this as I get a unique id generated in afterAll hook through an api call, that I need to reuse in the onTestEnd method

mxschmitt commented 10 months ago

You can annotate tests and read the annotations on a test level.

JasKirk70 commented 10 months ago

in the test.afterEach method I added below code test.info().annotations.push({ type: 'bug', description: id, }); and trying to read in custom reporter class in onTestEnd method as test.annotations.forEach(annotation => { const { type, description } = annotation; console.log(Type: ${type}); if (description) { console.log(Description: ${description}); } else { console.log('No description available'); } }); but nothing is getting logged.

mxschmitt commented 10 months ago

I tried the following and it works for me:

// reporter.ts
import type { Reporter, TestCase, TestResult } from '@playwright/test/reporter';

class MyReporter implements Reporter {
  onTestEnd(test: TestCase, result: TestResult) {
    console.log(`Finished test ${test.title}: ${result.status}`);
    for (const annotation of test.annotations) {
      const { type, description } = annotation;
      console.log(`Type: ${type}`);
      if (description) {
        console.log(`Description: ${description}`);
      } else {
        console.log('No description available');
      }
    }
  }
}

export default MyReporter;
// mytest.spec.ts
import { test, expect } from '@playwright/test';

test.afterEach(async ({ }) => {
  test.info().annotations.push({
    description: "hi",
    type: 'comment',
  })
})

test('has title', async ({ }) => {
});

What am I doing different?

yury-s commented 10 months ago

Closing per the response above, feel free to open a new issue if it doesn't work.