Files
novalon-website/test-framework/shared/utils/performance/LighthouseRunner.ts
T
张翔 6d92024b63 feat: 修复测试套件问题并添加Woodpecker CI配置
- 修复API测试认证问题:创建全局认证设置,更新Playwright配置
- 优化回归测试稳定性:增加超时时间到15秒,修复定位器
- 创建Woodpecker CI工作流:CI、部署和质量门禁配置
- 添加Jest配置和测试脚本
- 移除登录页面的默认账号密码显示(安全问题修复)
2026-03-09 10:26:02 +08:00

93 lines
2.9 KiB
TypeScript

import { Page } from '@playwright/test';
import { LighthouseResult } from '../../types';
export class LighthouseRunner {
constructor(private page: Page) {}
async runLighthouse(url: string): Promise<LighthouseResult> {
const results = await this.page.evaluate(async () => {
return new Promise<LighthouseResult>((resolve) => {
if (!(window as any).lighthouse) {
resolve({
performance: 0,
accessibility: 0,
bestPractices: 0,
seo: 0,
pwa: 0
});
return;
}
(window as any).lighthouse(url, {
onlyCategories: ['performance', 'accessibility', 'best-practices', 'seo', 'pwa']
}).then((result: any) => {
resolve({
performance: Math.round(result.categories.performance.score * 100),
accessibility: Math.round(result.categories.accessibility.score * 100),
bestPractices: Math.round(result.categories['best-practices'].score * 100),
seo: Math.round(result.categories.seo.score * 100),
pwa: Math.round(result.categories.pwa.score * 100)
});
});
});
});
return results;
}
async runPerformanceAudit(): Promise<{
score: number;
metrics: {
firstContentfulPaint: number;
largestContentfulPaint: number;
cumulativeLayoutShift: number;
firstInputDelay: number;
speedIndex: number;
};
}> {
const results = await this.page.evaluate(async () => {
return new Promise<{
score: number;
metrics: {
firstContentfulPaint: number;
largestContentfulPaint: number;
cumulativeLayoutShift: number;
firstInputDelay: number;
speedIndex: number;
};
}>((resolve) => {
if (!(window as any).lighthouse) {
resolve({
score: 0,
metrics: {
firstContentfulPaint: 0,
largestContentfulPaint: 0,
cumulativeLayoutShift: 0,
firstInputDelay: 0,
speedIndex: 0
}
});
return;
}
(window as any).lighthouse(window.location.href, {
onlyCategories: ['performance']
}).then((result: any) => {
resolve({
score: Math.round(result.categories.performance.score * 100),
metrics: {
firstContentfulPaint: result.audits['first-contentful-paint'].numericValue,
largestContentfulPaint: result.audits['largest-contentful-paint'].numericValue,
cumulativeLayoutShift: result.audits['cumulative-layout-shift'].numericValue,
firstInputDelay: result.audits['max-potential-fid'].numericValue,
speedIndex: result.audits['speed-index'].numericValue
}
});
});
});
});
return results;
}
}