1212 <a href =" https://github.com/Valiantsin2021/playwright-performance-metrics " ><img src =" https://img.shields.io/badge/Author-Valentin%20Lutchanka-blue " alt =" playwright-performance-metrics author " /></a >
1313 <a href =" https://github.com/Valiantsin2021/playwright-performance-metrics/actions/workflows/ci.yml " ><img src =" https://github.com/Valiantsin2021/playwright-performance-metrics/actions/workflows/ci.yml/badge.svg?branch=main " alt =" playwright-performance-metrics ci tests " /></a >
1414</p >
15+ <h3 align =" center " >A comprehensive performance metrics collector for Playwright tests. Collect and analyze web vital metrics, network timing, and resource usage in your Playwright tests.</h3 >
16+ <img src =" img\performance-UI.png " alt =" Performance-UI " />
1517
16- ---
17-
18- A comprehensive performance metrics collector for Playwright tests. Collect and analyze web vital metrics, network timing, and resource usage in your Playwright tests.
1918
2019## Features
2120
22- - Collect Web Vitals (LCP, FID, CLS)
21+ - Collect Web Vitals (LCP, CLS)
2322- Network timing metrics (TTFB, resource timing)
2423- Network condition emulation (3G, 4G, WiFi)
2524- Resource usage tracking
@@ -117,7 +116,9 @@ npm install -D playwright-performance-metrics
117116
118117## Usage
119118
120- Basic usage:
119+ ** Basic usage:**
120+
121+ * Note: it is important to wait the completion of the page load before collecting metrics.
121122
122123``` typescript
123124import { test } from ' @playwright/test' ;
@@ -126,8 +127,7 @@ import { PerformanceMetricsCollector } from 'playwright-performance-metrics';
126127test (' measure page performance' , async ({ page }) => {
127128 const collector = new PerformanceMetricsCollector (page );
128129
129- await page .goto (' https://example.com' );
130-
130+ await page .goto (' https://example.com' , { waitUntil: ' networkidle' })
131131 const metrics = await collector .collectMetrics ({
132132 timeout: 10000 ,
133133 initialDelay: 1000
@@ -139,17 +139,37 @@ test('measure page performance', async ({ page }) => {
139139 ' Largest Contentful Paint' : metrics .largestContentfulPaint ,
140140 ' Cumulative Layout Shift' : metrics .cumulativeLayoutShift
141141 });
142+ expect (metrics .pageloadTiming , ' Page load time is less than 2000ms' ).toBeLessThan (2000 )
143+ expect (metrics .domCompleteTiming , ' DOM Complete is less than 900ms' ).toBeLessThan (900 )
144+ expect (metrics .paint ?.firstContentfulPaint , ' First Contentful Paint is less than 2000ms' ).toBeLessThan (2000 )
145+ expect (metrics .paint ?.firstPaint , ' First Paint is less than 1000ms' ).toBeLessThan (1000 )
146+ expect (metrics .largestContentfulPaint , ' Largest Contentful Paint is less than 2500ms' ).toBeLessThan (2500 )
147+ expect (metrics .cumulativeLayoutShift , ' Cumulative Layout Shift is less than 0.1' ).toBeLessThan (0.1 )
148+ expect (metrics .totalBlockingTime , ' Total Blocking Time is less than 500ms' ).toBeLessThan (500 )
149+ expect (metrics .totalBytes , ' Total bytes is less than 1.5 MB' ).toBeLessThan (1024 * 1024 * 1.5 )
150+ expect (metrics .timeToFirstByte ?.total , ' Time to first byte is less than 900ms' ).toBeLessThan (900 )
151+ expect (metrics .timeToFirstByte .dns , ' DNS time is less than 100ms' ).toBeLessThan (100 )
152+ expect (metrics .timeToFirstByte .wait , ' Wait time is less than 100ms' ).toBeLessThan (100 )
153+ expect (metrics .timeToFirstByte .redirect , ' Redirect time is less than 100ms' ).toBeLessThan (100 )
154+ expect (metrics .timeToFirstByte .tls , ' TLS time is less than 100ms' ).toBeLessThan (100 )
155+ expect (metrics .timeToFirstByte .connection , ' Connection time is less than 100ms' ).toBeLessThan (100 )
142156});
143157```
144158
145- With network emulation:
159+ ** With network emulation:**
160+
161+ Library provides predefined presets for network conditions.
162+
163+ - REGULAR_4G
164+ - SLOW_3G
165+ - FAST_WIFI
146166
147167``` typescript
148168import { DefaultNetworkPresets } from ' playwright-performance-metrics' ;
149169
150170test (' measure performance with slow 3G' , async ({ page }) => {
151171 const collector = new PerformanceMetricsCollector (page );
152-
172+ await page . goto ( ' https://example.com ' , { waitUntil: ' networkidle ' })
153173 const metrics = await collector .collectMetrics ({
154174 networkConditions: DefaultNetworkPresets .SLOW_3G ,
155175 timeout: 30000
@@ -159,6 +179,34 @@ test('measure performance with slow 3G', async ({ page }) => {
159179});
160180```
161181
182+ ** With fixtures:**
183+
184+ ``` typescript
185+ // fixture.ts
186+
187+ import { test as base , expect } from ' @playwright/test'
188+ import { PerformanceMetricsCollector } from ' playwright-performance-metrics'
189+
190+ const test = base .extend ({
191+ collector : async ({ page }, use ) => {
192+ const collector = new PerformanceMetricsCollector (page )
193+ await use (collector )
194+ }
195+ })
196+
197+ export { test , expect }
198+
199+ // test.spec.ts
200+
201+ import { expect , test } from ' ./fixture.ts'
202+
203+ test (' measure page performance' , async ({ page , collector }) => {
204+ await page .goto (' https://example.com' , { waitUntil: ' networkidle' })
205+ const metrics = await collector .collectMetrics ()
206+ expect (metrics .domCompleteTiming , ' DOM Complete is less than 900ms' ).toBeLessThan (900 )
207+ expect (metrics .paint ?.firstContentfulPaint , ' First Contentful Paint is less than 2000ms' ).toBeLessThan (2000 )})
208+ })
209+ ```
162210## API Reference
163211
164212### PerformanceMetricsCollector
@@ -173,15 +221,25 @@ constructor(page: Page)
173221
174222#### Methods
175223
176- ##### collectMetrics(options?: PerformanceOptions): Promise<PerformanceMetrics >
224+ - ##### collectMetrics(options?: PerformanceOptions): Promise<PerformanceMetrics >
177225
178- Collects performance metrics from the page.
226+ ``` typescript
227+ /**
228+ * Collect performance metrics from the page.
229+ * @param options - Options for metric collection.
230+ * @returns Collected performance metrics.
231+ */
232+ ```
179233
180- Options:
181- - ` timeout ` : Collection timeout in milliseconds
182- - ` initialDelay ` : Initial delay before starting collection
183- - ` retryTimeout ` : Maximum time to retry collecting metrics
184- - ` networkConditions ` : Network conditions to emulate
234+ - ##### options:
235+ ``` typescript
236+ /** Maximum time to wait for metrics collection (ms) */
237+ timeout ?: number
238+ /** Maximum time to retry collecting metrics (ms) */
239+ retryTimeout ?: number
240+ /** Network emulation settings */
241+ networkConditions ?: NetworkConditions
242+ ```
185243
186244### Network Presets
187245
0 commit comments