This thread is trying to answer question "How can the user run a basic LCP test in a TypeScript file without encountering an import error?"
import { chromium } from '@playwright/test'
;(async () => {
try {
const browser = await chromium.launch()
const page = await browser.newPage()
await page.goto('http://localhost:5173')
// ---------------------
// Complete
const LCP: number | undefined = await page.evaluate(() => {
return new Promise((resolve) => {
new PerformanceObserver((list) => {
const entries = list.getEntries()
const latestEntry: PerformanceEntry | undefined = entries.at(-1)
resolve(latestEntry ? latestEntry.startTime : undefined)
}).observe({ type: 'largest-contentful-paint', buffered: true })
})
})
if (LCP !== undefined) {
console.log(parseInt(String(LCP), 10))
} else {
console.log('LCP is undefined')
}
await browser.close()
} catch (error) {
console.error(error)
process.exit(1)
}
})()
this will give you LCP and other WebVitals automatically: https://github.com/scalewright/scalewright#description
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].