Playwright
It's easy to report results from Playwright using a custom reporter. Create a testify-reporter.ts
file in your repository and copy the contents of the file below into it.
import type {
FullResult,
Reporter,
TestCase,
TestResult
} from '@playwright/test/reporter';
/**
* For example test title "[PROJ-C123] Test case title"
* Available capture groups:
* - project_identifier: "PROJ"
* - case_display_id: "123"
* - case_identifier: "PROJ-C123"
*/
const CASE_ID_REGEX =
/.*\[(?<case_identifier>(?<project_identifier>[a-zA-Z]*)-C(?<case_display_id>\d*))\].*/;
type TestifyResult = {
caseId: string;
result: 'passed' | 'failed' | 'inProgress' | 'untested';
comment?: string;
};
interface TestifyReporterOptions {
apiKey?: string;
runId?: string;
}
const log = (message: string, ...data: any) => {
console.log(`[TestifyReporter] ${message}`, ...data);
};
/**
* Reports playwright test results to Testify. Test names are expected to contain
* a case identifier in the format [PROJ-C123] somewhere in the title. To customize
* the regex used to extract the case identifier, modify the CASE_ID_REGEX constant.
* @see https://docs.testify.so/reporting/playwright
*/
class TestifyReporter implements Reporter {
results = new Map<string, TestResult>();
apiKey?: string;
runId?: string;
constructor(options: TestifyReporterOptions) {
if (!options.apiKey) {
log('No API key provided. Test results will not be reported.');
}
if (!options.runId) {
log('No test run ID provided. Test results will not be reported.');
}
this.apiKey = options.apiKey;
this.runId = options.runId;
if (this.apiKey && this.runId) {
log(
'Testify reporter configured with API key and run ID. Test results will be reported.'
);
}
}
async postRequest(url: string, body: object) {
if (!this.apiKey) {
log('No API key provided. Test results will not be reported.');
return;
}
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': this.apiKey
},
body: JSON.stringify(body)
};
try {
const response = await fetch(url, options);
return await response.json();
} catch (error) {
console.log(error);
throw error;
}
}
async reportResults(results: TestifyResult[]) {
if (!this.runId) {
log('No test run ID provided. Test results will not be reported.');
return;
}
const url = `https://api.testify.so/v1/test-runs/${this.runId}/report`;
try {
const response = await this.postRequest(url, {
results
});
return response;
} catch (error) {
console.log(error);
process.exit(1);
}
}
onTestEnd(test: TestCase, result: TestResult) {
this.results.set(test.title, result);
}
async onEnd(result: FullResult) {
const resultsToReport: TestifyResult[] = [];
for (const [key, value] of this.results.entries()) {
const caseIdentifier = key.match(CASE_ID_REGEX)?.groups?.case_identifier;
if (!caseIdentifier) {
log('Could not find case ID for test', {
key,
value
});
continue;
}
const testResult: TestifyResult = {
caseId: caseIdentifier,
result: value.status === 'passed' ? 'passed' : 'failed'
};
if (value.status === 'failed') {
testResult.comment = value.error?.message;
}
resultsToReport.push(testResult);
}
log(
`Reporting results for ${resultsToReport.length} tests...`,
resultsToReport
);
await this.reportResults(resultsToReport);
log('Results reported!');
}
}
export default TestifyReporter;
In your playwright.config.ts
file, import the reporter and add it to the reporter
array. Reports will be sent to Testify using the provided API key and run ID.
Generally it is expected that a test run is created as part of the CI/CD pipeline and the run ID is passed as an environment variable. If a test run ID or an API key is not provided, the reporter will log a message and not report any results.
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
...
reporter: [
['list'],
['html', { open: 'never' }],
[
'./testify-reporter.ts',
{
apiKey: process.env.TESTIFY_API_KEY,
runId: process.env.TESTIFY_RUN_ID
}
]
]
...
]
});
Once this configuration is complete, simply add the appropriate test case identifier to the title of each test case. For example, if you have a test case with the title [PROJ-C123] Test case title
, the results of that test case will be reported to the Testify test case with the identifier PROJ-C123
.
As written, a single Playwright test case can only be associated with a single Testify test case. Feel free to modify this reporter to suit your specific use case!