Files
novalon-website/test-framework/shared/utils/reporting/TestReporter.ts
T
2026-03-06 12:56:35 +08:00

216 lines
5.5 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
export class TestReporter {
private results: Map<string, any> = new Map();
addResult(type: string, result: any): void {
this.results.set(type, result);
}
generateHTMLReport(): string {
const timestamp = new Date().toLocaleString('zh-CN');
let html = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>综合测试报告 - ${timestamp}</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
border-radius: 10px;
margin-bottom: 30px;
}
.section {
background: white;
padding: 25px;
border-radius: 10px;
margin-bottom: 20px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.metric {
display: inline-block;
padding: 10px 20px;
margin: 5px;
border-radius: 5px;
font-weight: bold;
}
.metric.success { background: #10b981; color: white; }
.metric.warning { background: #f59e0b; color: white; }
.metric.danger { background: #ef4444; color: white; }
table {
width: 100%;
border-collapse: collapse;
margin-top: 15px;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #e5e7eb;
}
th { background: #f9fafb; font-weight: bold; }
</style>
</head>
<body>
<div class="header">
<h1>综合测试报告</h1>
<p>生成时间: ${timestamp}</p>
</div>
`;
for (const [type, result] of this.results.entries()) {
html += this.generateSection(type, result);
}
html += `
</body>
</html>`;
return html;
}
private generateSection(type: string, result: any): string {
switch (type) {
case 'accessibility':
return this.generateAccessibilitySection(result);
case 'seo':
return this.generateSEOSection(result);
case 'performance':
return this.generatePerformanceSection(result);
default:
return `<div class="section"><h2>${type}</h2><pre>${JSON.stringify(result, null, 2)}</pre></div>`;
}
}
private generateAccessibilitySection(results: any[]): string {
const totalViolations = results.reduce((sum: number, r: any) => sum + r.violations.length, 0);
const avgScore = results.reduce((sum: number, r: any) => sum + r.score, 0) / results.length;
return `
<div class="section">
<h2>可访问性测试</h2>
<div>
<span class="metric ${avgScore >= 80 ? 'success' : 'warning'}">平均分数: ${avgScore.toFixed(1)}</span>
<span class="metric ${totalViolations <= 5 ? 'success' : 'danger'}">总违规数: ${totalViolations}</span>
</div>
<table>
<thead>
<tr>
<th>页面</th>
<th>分数</th>
<th>违规数</th>
</tr>
</thead>
<tbody>
${results.map((r: any) => `
<tr>
<td>${r.page}</td>
<td>${r.score}</td>
<td>${r.violations.length}</td>
</tr>
`).join('')}
</tbody>
</table>
</div>`;
}
private generateSEOSection(results: any[]): string {
const avgScore = results.reduce((sum: number, r: any) => sum + r.score, 0) / results.length;
return `
<div class="section">
<h2>SEO检查</h2>
<div>
<span class="metric ${avgScore >= 80 ? 'success' : 'warning'}">平均分数: ${avgScore.toFixed(1)}</span>
</div>
<table>
<thead>
<tr>
<th>页面</th>
<th>分数</th>
<th>Meta标签</th>
<th>标题</th>
</tr>
</thead>
<tbody>
${results.map((r: any) => `
<tr>
<td>${r.page}</td>
<td>${r.score}</td>
<td>${r.metaTags.title && r.metaTags.description ? '✅' : '❌'}</td>
<td>${r.headings.hasH1 ? '✅' : '❌'}</td>
</tr>
`).join('')}
</tbody>
</table>
</div>`;
}
private generatePerformanceSection(results: any[]): string {
const avgLoadTime = results.reduce((sum: number, r: any) => sum + r.loadTime, 0) / results.length;
return `
<div class="section">
<h2>性能测试</h2>
<div>
<span class="metric ${avgLoadTime <= 3000 ? 'success' : 'warning'}">平均加载时间: ${avgLoadTime.toFixed(0)}ms</span>
</div>
<table>
<thead>
<tr>
<th>页面</th>
<th>加载时间</th>
<th>DOM加载</th>
</tr>
</thead>
<tbody>
${results.map((r: any) => `
<tr>
<td>${r.page}</td>
<td>${r.loadTime}ms</td>
<td>${r.domContentLoaded}ms</td>
</tr>
`).join('')}
</tbody>
</table>
</div>`;
}
saveHTMLReport(outputPath: string): void {
const html = this.generateHTMLReport();
const dir = path.dirname(outputPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(outputPath, html, 'utf-8');
}
generateJSONReport(): any {
return {
timestamp: new Date().toISOString(),
results: Object.fromEntries(this.results)
};
}
saveJSONReport(outputPath: string): void {
const json = this.generateJSONReport();
const dir = path.dirname(outputPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(outputPath, JSON.stringify(json, null, 2), 'utf-8');
}
}