Playwright Performance Testing Plugin
playwright-performance is a plugin for Playwright that enables detailed performance analysis of end-to-end test flows, including UI, API, or hybrid scenarios. It measures the 'apparent response time' of key user procedures within your application, helping identify bottlenecks and areas for optimization. The current stable version is 2.0.6, with recent updates introducing HTML chart generation. The library maintains an active release cadence, with a major breaking change in version 2.x.x simplifying its integration model. Its key differentiator is the seamless integration into Playwright's `test.extend` fixture system, allowing developers to easily add performance sampling to existing tests without significant refactoring, and providing configurable output options for results.
Common errors
-
TypeError: playwright_1.test.extend is not a function
cause This typically occurs if Playwright's 'test' object is not imported correctly, or if an older version of Playwright is used that doesn't support the 'test.extend' pattern with the plugin's structure.fixEnsure you are using `import { test as base } from '@playwright/test';` and that your Playwright dependency is up-to-date and compatible with 'playwright-performance' v2.x.x. -
Error: Cannot find module 'playwright-performance' or its corresponding type declarations.
cause The package might not be installed, the import path is incorrect, or your project's TypeScript/module resolution configuration does not properly resolve the package or its types.fixRun `npm install playwright-performance --save-dev`. Verify your `tsconfig.json` includes `playwright-performance` in `types` or `typeRoots`, and ensure your environment supports ESM imports correctly, especially for Node.js projects. -
Property 'performance' does not exist on type 'TestFixture<{ page: Page; ... }>'cause The 'performance' fixture was not correctly added to your Playwright test context, meaning the 'test' object you are importing or using in your test file is not the extended one.fixEnsure your test file imports and uses the 'test' object that has been extended by `playwright-performance` (e.g., `import { test } from '../path/to/my-extended-test-base';`) and that 'performance' is destructured in your test function signature: `async ({ page, performance }) => { ... }`.
Warnings
- breaking Version 2.x.x introduced significant breaking changes, particularly in how the plugin is imported and used to extend the Playwright `test` object. Existing `require()` statements and older `test.extend` patterns will no longer work.
- breaking Starting with version 2.x.x, `playwright-performance` is primarily an ESM module. CommonJS `require()` syntax is no longer supported for importing the plugin.
- gotcha Defining the extended `test` object (`const test = base.extend<...>(...)`) directly within each test file can lead to unnecessary setup overhead, redundant code, and potential inconsistencies across tests.
- gotcha By default, performance results are saved to `performance-results/performance-results.json`. Running multiple test suites or different performance scenarios without configuring unique filenames or directories will overwrite previous results.
Install
-
npm install playwright-performance -
yarn add playwright-performance -
pnpm add playwright-performance
Imports
- extendPlaywrightPerformance
const extendPlaywrightPerformance = require('playwright-performance');import extendPlaywrightPerformance from 'playwright-performance';
- PlaywrightPerformance
const PlaywrightPerformance = require('playwright-performance').PlaywrightPerformance;import type { PlaywrightPerformance } from 'playwright-performance'; - PerformanceOptions
import type { PerformanceOptions } from 'playwright-performance';
Quickstart
import { test as base, expect } from '@playwright/test';
import extendPlaywrightPerformance, { PerformanceOptions, PlaywrightPerformance } from 'playwright-performance';
// Extend the Playwright test object with performance fixtures
const test = base.extend<PlaywrightPerformance, PerformanceOptions>(extendPlaywrightPerformance());
test.describe('Website Startup Performance', () => {
test('should load GitHub and SourceForge within acceptable limits', async ({ page, performance }) => {
// Sample startup time for GitHub
performance.sampleStart('GH-startup');
await page.goto('https://github.com/', { waitUntil: 'domcontentloaded' });
performance.sampleEnd('GH-startup');
// Sample startup time for SourceForge
performance.sampleStart('SF-startup');
await page.goto('https://sourceforge.net/', { waitUntil: 'domcontentloaded' });
performance.sampleEnd('SF-startup');
// You can also get individual sample times within the test
const githubStartupTime = performance.getSampleTime('GH-startup');
const sourceForgeStartupTime = performance.getSampleTime('SF-startup');
// Assertions for performance metrics
expect(githubStartupTime).toBeLessThanOrEqual(5000); // Expect GitHub to load in under 5 seconds
expect(sourceForgeStartupTime).toBeLessThanOrEqual(8000); // Expect SourceForge to load in under 8 seconds
console.log(`GitHub Startup Time: ${githubStartupTime}ms`);
console.log(`SourceForge Startup Time: ${sourceForgeStartupTime}ms`);
});
});