|
| 1 | +import { existsSync, mkdirSync, writeFileSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { Page, chromium } from '@playwright/test'; |
| 4 | +import { playAudit, playwrightLighthouseResult } from 'playwright-lighthouse'; |
| 5 | +import { LIGHTHOUSE_CONFIG } from './lighthouse.config'; |
| 6 | +import { Categories, LighthouseResult, MetricName, Metrics, TestCase } from './lighthouse.type'; |
| 7 | + |
| 8 | +const RESULTS_DIR = './.lighthouse'; |
| 9 | +const results: LighthouseResult[] = []; |
| 10 | + |
| 11 | +export const runAudit = async (page: Page, pageName: string): Promise<playwrightLighthouseResult> => { |
| 12 | + return playAudit({ |
| 13 | + page, |
| 14 | + ...LIGHTHOUSE_CONFIG, |
| 15 | + reports: { |
| 16 | + ...LIGHTHOUSE_CONFIG.reports, |
| 17 | + name: pageName, |
| 18 | + }, |
| 19 | + }); |
| 20 | +}; |
| 21 | + |
| 22 | +export const getMetricScore = (result: playwrightLighthouseResult, metricName: MetricName) => { |
| 23 | + const audit = result.lhr.audits[metricName]; |
| 24 | + return { |
| 25 | + displayValue: audit.displayValue || '', |
| 26 | + score: (audit.score || 0) * 100, |
| 27 | + }; |
| 28 | +}; |
| 29 | + |
| 30 | +export const extractResults = (result: playwrightLighthouseResult) => { |
| 31 | + const categories: Categories = { |
| 32 | + performance: { score: (result.lhr.categories.performance?.score || 0) * 100 }, |
| 33 | + accessibility: { score: (result.lhr.categories.accessibility?.score || 0) * 100 }, |
| 34 | + 'best-practices': { score: (result.lhr.categories['best-practices']?.score || 0) * 100 }, |
| 35 | + seo: { score: (result.lhr.categories.seo?.score || 0) * 100 }, |
| 36 | + }; |
| 37 | + |
| 38 | + const metrics: Metrics = { |
| 39 | + FCP: getMetricScore(result, 'first-contentful-paint'), |
| 40 | + LCP: getMetricScore(result, 'largest-contentful-paint'), |
| 41 | + TBT: getMetricScore(result, 'total-blocking-time'), |
| 42 | + CLS: getMetricScore(result, 'cumulative-layout-shift'), |
| 43 | + SI: getMetricScore(result, 'speed-index'), |
| 44 | + }; |
| 45 | + |
| 46 | + return { categories, metrics }; |
| 47 | +}; |
| 48 | + |
| 49 | +export const saveResult = (result: LighthouseResult) => { |
| 50 | + results.push(result); |
| 51 | + persistResults(); |
| 52 | +}; |
| 53 | + |
| 54 | +export const persistResults = () => { |
| 55 | + if (!existsSync(RESULTS_DIR)) { |
| 56 | + mkdirSync(RESULTS_DIR, { recursive: true }); |
| 57 | + } |
| 58 | + writeFileSync(join(RESULTS_DIR, 'results.json'), JSON.stringify(results, null, 2)); |
| 59 | +}; |
| 60 | + |
| 61 | +export const printScores = (result: LighthouseResult) => { |
| 62 | + console.log(`\n-----${result.pageName}-----`); |
| 63 | + console.table(result.categories); |
| 64 | + console.table(result.metrics); |
| 65 | +}; |
| 66 | + |
| 67 | +export const initBrowser = async () => { |
| 68 | + const browser = await chromium.launch(); |
| 69 | + const context = await browser.newContext(); |
| 70 | + const page = await context.newPage(); |
| 71 | + return { browser, context, page }; |
| 72 | +}; |
| 73 | + |
| 74 | +export const navigateToPage = async (page: Page, { url, setup }: TestCase) => { |
| 75 | + await page.goto(url); |
| 76 | + if (setup) { |
| 77 | + await setup(page); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +export const collectMetrics = async (page: Page, pageName: string) => { |
| 82 | + const rawResults = await runAudit(page, pageName); |
| 83 | + return { |
| 84 | + pageName, |
| 85 | + ...extractResults(rawResults), |
| 86 | + }; |
| 87 | +}; |
| 88 | + |
| 89 | +export const runPerformanceTest = async (config: TestCase) => { |
| 90 | + const { browser, page } = await initBrowser(); |
| 91 | + |
| 92 | + try { |
| 93 | + await navigateToPage(page, config); |
| 94 | + const testResults = await collectMetrics(page, config.pageName); |
| 95 | + printScores(testResults); |
| 96 | + saveResult(testResults); |
| 97 | + } finally { |
| 98 | + await browser.close(); |
| 99 | + } |
| 100 | +}; |
0 commit comments