Rayrun
← Back to Discord Forum

basic lcp test

insidiousssssposted in #help-playwright
Open in Discord

hi , I'm trying to test basic lcp test, but I get import error, is there a fix for that?

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?"

6 replies

import { chromium } from '@playwright/test' does not work, but const { chromium } = require('playwright') works

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)
    }
})()
insidiousssss
insidiousssss

is there a way I can run this in ts file?

insidiousssss
insidiousssss
image.png
insidiousssss
insidiousssss
image.png

this will give you LCP and other WebVitals automatically: https://github.com/scalewright/scalewright#description

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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].