Created
June 5, 2023 08:27
-
-
Save Xotabu4/2ebf5f610ac810e319dcfbb9b194d244 to your computer and use it in GitHub Desktop.
Playwright reporter for slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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