Skip to main content
Dublin Library

The Publishing Project

Measuring Performance Tasks with Playwright

 

As I was writing my previous post about Playwright and writing tests I came across [an article](https://www.checklyhq.com/blog/how-playwright-can-monitor-third-party-resources/) that explained how to run performance measurements inside Playwright tests. ## Core Web Vitals The example runs against your app's local server and will generate the [Largest Contentful Paint](https://web.dev/articles/lcp) value using [performance observers](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceObserver) combined with Playwright commands. Because the local server will not act like your regular server (there are no network requests) the results will be significantly faster than they would be against your production site. You should take this into account when evaluating the tests and, if possible, should also test against your production site. The CWV tests are broken into separate tests for each of the measurements I want to take. ### before we start Before we write the test, we'll leverage the `beforeEach` Playwright hook to load the page we want to test before we run each test. This hook means that we only have to change the URL in one place and not have to change it in multiple locations. ```js const TEST_URL = "http://localhost:8080"; test.beforeEach(async ({ page }) => { await page.goto(TEST_URL); }); ``` ### Largest Contentful Paint (LCP) [Largest Contentful Paint](https://web.dev/articles/lcp) measures how long it takes for the largest image or text block visible within the viewport to render, relative to when the user first navigated to the page. As currently specified in the [Largest Contentful Paint API](https://wicg.github.io/largest-contentful-paint/), the types of elements considered for Largest Contentful Paint are: * `` elements * `` elements inside `` elements * Poster images inside `

Edit on Github