feat: integrate Lighthouse in MobilePerformanceMonitor
This commit is contained in:
Generated
+2516
File diff suppressed because it is too large
Load Diff
@@ -27,7 +27,9 @@
|
|||||||
"@types/node": "^20.11.0",
|
"@types/node": "^20.11.0",
|
||||||
"allure-commandline": "^2.37.0",
|
"allure-commandline": "^2.37.0",
|
||||||
"allure-playwright": "^3.5.0",
|
"allure-playwright": "^3.5.0",
|
||||||
|
"chrome-launcher": "^1.2.1",
|
||||||
"glob": "^13.0.6",
|
"glob": "^13.0.6",
|
||||||
|
"lighthouse": "^13.0.3",
|
||||||
"typescript": "^5.3.0"
|
"typescript": "^5.3.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,4 +12,14 @@ test.describe('MobilePerformanceMonitor', () => {
|
|||||||
expect(vitals.LCP).toBeGreaterThan(0);
|
expect(vitals.LCP).toBeGreaterThan(0);
|
||||||
expect(vitals.CLS).toBeGreaterThanOrEqual(0);
|
expect(vitals.CLS).toBeGreaterThanOrEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should run Lighthouse audit', async ({ page }) => {
|
||||||
|
await page.goto('/');
|
||||||
|
const monitor = new MobilePerformanceMonitor(page);
|
||||||
|
|
||||||
|
const result = await monitor.runLighthouseAudit(page.url());
|
||||||
|
|
||||||
|
expect(result.score).toBeGreaterThan(0);
|
||||||
|
expect(result.audits).toBeDefined();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
import { Page } from '@playwright/test';
|
import { Page } from '@playwright/test';
|
||||||
|
import lighthouse from 'lighthouse';
|
||||||
|
import * as chromeLauncher from 'chrome-launcher';
|
||||||
|
|
||||||
export interface CoreWebVitals {
|
export interface CoreWebVitals {
|
||||||
FCP: number;
|
FCP: number;
|
||||||
@@ -53,4 +55,26 @@ export class MobilePerformanceMonitor {
|
|||||||
TTI: 0,
|
TTI: 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async runLighthouseAudit(url: string): Promise<LighthouseResult> {
|
||||||
|
const chrome = await chromeLauncher.launch({ chromeFlags: ['--headless'] });
|
||||||
|
const options = {
|
||||||
|
logLevel: 'info' as const,
|
||||||
|
output: 'json' as const,
|
||||||
|
onlyCategories: ['performance'],
|
||||||
|
port: chrome.port,
|
||||||
|
};
|
||||||
|
|
||||||
|
const runnerResult = await lighthouse(url, options);
|
||||||
|
await chrome.kill();
|
||||||
|
|
||||||
|
if (!runnerResult) {
|
||||||
|
throw new Error('Lighthouse audit failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
score: runnerResult.lhr.categories.performance.score * 100,
|
||||||
|
audits: runnerResult.lhr.audits,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user