Skip to content

Instantly share code, notes, and snippets.

@Xotabu4
Created June 5, 2023 08:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xotabu4/2ebf5f610ac810e319dcfbb9b194d244 to your computer and use it in GitHub Desktop.
Save Xotabu4/2ebf5f610ac810e319dcfbb9b194d244 to your computer and use it in GitHub Desktop.
Playwright reporter for slack
import type {
FullResult, Reporter, TestCase, TestResult,
} from '@playwright/test/types/testReporter';
class PlaywrightSlackReporter implements Reporter {
allResults: Array<{ test: TestCase, result: TestResult }>;
constructor(private conf: { enabled: boolean, webhookUrl: string }) {
this.conf = conf;
this.allResults = [];
if (!this.conf.enabled || !this.conf.webhookUrl) {
console.info('[SLACK REPORTER] is disabled, this.conf.enabled or this.conf.webhookUrl is not set');
}
}
onTestEnd(test: TestCase, result: TestResult) {
if (!this.conf.enabled || !this.conf.webhookUrl) return;
this.allResults.push({ test, result });
}
async onEnd(result: FullResult) {
if (!this.conf.enabled || !this.conf.webhookUrl) return;
const { IncomingWebhook } = await import('@slack/webhook');
const webhook = new IncomingWebhook(this.conf.webhookUrl);
const allPassed = this.allResults.filter((res) => res.result.status === 'passed');
const allFailed = this.allResults.filter((res) => res.result.status === 'failed');
const allTimedOut = this.allResults.filter((res) => res.result.status === 'timedOut');
const allSkipped = this.allResults.filter((res) => res.result.status === 'skipped');
await webhook.send(`
=========== E2E tests results ===========
Total executed tests: ${this.allResults.length}
Passed: ${allPassed.length}
Failed: ${Math.floor(allFailed.length / 3)}
Timeout: ${allTimedOut.length}
Skipped: ${allSkipped.length}
Job name: ${process.env.CI_JOB_NAME ?? 'local name'}
Job URL: ${process.env.CI_JOB_URL ?? 'local run'}
Report URL: https://PROJECT_NAME.gitlab.io/-/app/PROJECT_NAME/-/jobs/${process.env.CI_JOB_ID as string}/artifacts/test/e2e/test-report/index.html
Commit Author: ${process.env.CI_COMMIT_AUTHOR ?? 'local runer'}
==========================================
`);
}
}
export default PlaywrightSlackReporter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment