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] Call a function after test report xml file is generated #28852

Closed python012 closed 9 months ago

python012 commented 9 months ago

Hi, is it possible to call a function within a Playwright(TypeScript) test project after tests are finished and test report xml file is generated?

In my project, I'd like to import test result file to Jira, which need send a POST REQUEST to specific Jira endpoint, but looks like generating test result xml file is almost the very last step in the task of npx playwright test.

mxschmitt commented 9 months ago

If I understand you correctly, this is about the junit reporter. What you can do there is either create a custom reporter, which you put after the junit reporter. It looks like this:

// config
  reporter: [['html'], ['junit', { outputFile: './foo.xml'}], ['./custom']],
// reporter.ts
import type { FullConfig, FullResult, Reporter, Suite } from '@playwright/test/reporter';
import { existsSync } from 'fs';

class MyReporter implements Reporter {
  private _config: FullConfig;
  onBegin(config: FullConfig, suite: Suite) {
    this._config = config;
  }

  async onEnd(result: FullResult) {
    const xmlFile = this._config.reporter[1][1].outputFile
    // read and upload it somewhere.
    console.log(`Finished the run: ${result.status}`, existsSync(this._config.reporter[1][1].outputFile));
  }
}

export default MyReporter;

or you just execute a custom Node.js script after your test execution has finished.

Would that work for you?

python012 commented 9 months ago

thanks so much! I'll take a try