playwright-test-workflow
Version:
Global test workflow package for Playwright with custom reporters
99 lines (79 loc) • 3.17 kB
JavaScript
const fs = require('fs');
const path = require('path');
class TextReporter {
constructor(options = {}) {
this.outputFile = options.outputFile || 'test-results/test-results.txt';
this.results = [];
this.testGroupPatterns = options.testGroupPatterns || this.getDefaultPatterns();
}
getDefaultPatterns() {
return {
'TEST#1': ['load', 'display', 'show', 'heading', 'button', 'input', 'result display'],
'TEST#2': ['calculate', 'sum', 'negative', 'zero', 'large', 'decimal'],
'TEST#3': ['empty', 'non-numeric', 'precision', 'error', 'validation'],
'TEST#4': ['responsive', 'clickable', 'accept', 'update', 'ui', 'ux']
};
}
determineTestGroup(testName) {
const lowerName = testName.toLowerCase();
for (const [group, patterns] of Object.entries(this.testGroupPatterns)) {
if (patterns.some(pattern => lowerName.includes(pattern))) {
return group;
}
}
return 'UNKNOWN';
}
onTestEnd(test, result) {
const status = result.status === 'passed' ? 'PASS' : 'FAIL';
const testName = test.title;
const error = result.error ? result.error.message : '';
const testGroup = this.determineTestGroup(testName);
this.results.push({
name: testName,
status: status,
error: error,
group: testGroup
});
}
onEnd() {
const dir = path.dirname(this.outputFile);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// Generate overall report
let report = 'Test Results\n============\n\n';
this.results.forEach(test => {
report += `${test.status}: ${test.name}\n`;
if (test.error && test.status === 'FAIL') {
report += ` Error: ${test.error}\n`;
}
report += '\n';
});
const passed = this.results.filter(t => t.status === 'PASS').length;
const failed = this.results.filter(t => t.status === 'FAIL').length;
const total = this.results.length;
report += `Summary: ${passed} passed, ${failed} failed, ${total} total\n`;
fs.writeFileSync(this.outputFile, report);
// Generate group-specific reports
const testGroups = [...new Set(this.results.map(r => r.group))];
testGroups.forEach(group => {
if (group === 'UNKNOWN') return;
const groupTests = this.results.filter(t => t.group === group);
let groupReport = `${group} Results\n${'='.repeat(group.length + 8)}\n\n`;
groupTests.forEach(test => {
groupReport += `${test.status}: ${test.name}\n`;
if (test.error && test.status === 'FAIL') {
groupReport += ` Error: ${test.error}\n`;
}
groupReport += '\n';
});
const groupPassed = groupTests.filter(t => t.status === 'PASS').length;
const groupFailed = groupTests.filter(t => t.status === 'FAIL').length;
const groupTotal = groupTests.length;
groupReport += `${group} Summary: ${groupPassed} passed, ${groupFailed} failed, ${groupTotal} total\n`;
const groupFile = path.join(dir, `${group}-results.txt`);
fs.writeFileSync(groupFile, groupReport);
});
}
}
module.exports = TextReporter;