Skip to content

Instantly share code, notes, and snippets.

@pmicko
Created February 15, 2024 19:27
Show Gist options
  • Save pmicko/159c7bad47e01241d15e85c3b1842624 to your computer and use it in GitHub Desktop.
Save pmicko/159c7bad47e01241d15e85c3b1842624 to your computer and use it in GitHub Desktop.
Playwright - Send metadata about test results from every test to ElasticSearch for further analysis (e.g. trends)
import type { Reporter, TestCase, TestResult } from '@playwright/test/reporter';
import axios, { AxiosResponse } from 'axios';
class ElasticReporter implements Reporter {
testResultsPromises: Array<Promise<AxiosResponse>> = [];
elasticUrl = process.env.ELASTIC_URL || undefined;
elasticDataBase = {
project: 'your-project',
team: 'your-team',
repo: 'repo',
type: 'e2e-api-etc',
framework: 'playwright',
environment: process.env.TEST_ENV,
debug: process.env.ELASTIC_DEBUG || true,
};
composeElasticData(test: TestCase, result: TestResult) {
const date = new Date();
const timestampElasticData = date.toISOString();
const testID =
test.annotations[0].type === 'testKey'
? test.annotations[0].description
: null;
const testResult = {
testTitle: `${test?.parent?.title} - ${test.title}`,
testResult: result.status,
testDuration: result.duration,
errorMessage: result.error?.message,
};
const elasticMessage = { message: `Test ${result.status} - ${test.title}` };
return {
'@timestamp': timestampElasticData,
...this.elasticDataBase,
...testResult,
...elasticMessage,
testID,
};
}
onTestEnd(test: TestCase, result: TestResult) {
const elasticData = this.composeElasticData(test, result);
const date = new Date();
const timestampIndex = date.toISOString().split('T')[0];
const index = `qa-test-${timestampIndex}`;
const myPromise = axios.post(
`${this.elasticUrl}${index}/_doc`,
elasticData,
);
this.testResultsPromises.push(myPromise);
}
async onEnd() {
await Promise.all(this.testResultsPromises);
}
}
export default ElasticReporter;
import { PlaywrightTestConfig } from '@playwright/test';
import dotenv from 'dotenv';
dotenv.config();
const config: PlaywrightTestConfig = {
// (...)
reporter: process.env.ELASTIC_URL
? [['list'], ['../src/elasticReporter.ts']]
: [['list']],
// (...)
}
export default config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment