Implementation:Microsoft Playwright Client Coverage
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, Code Coverage |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for collecting JavaScript and CSS code coverage data from Chromium-based browsers provided by the Playwright library.
Description
The Coverage class implements the api.Coverage interface and wraps the page channel's coverage-related methods. It provides paired start/stop methods for both JavaScript and CSS coverage collection. startJSCoverage() and startCSSCoverage() accept options to configure coverage collection (such as resetting counters or reporting anonymous scripts). The corresponding stopJSCoverage() and stopCSSCoverage() methods return arrays of coverage entries containing URL, source text, and range information.
Usage
Use the Coverage class when you need to measure which JavaScript or CSS code was executed during page interactions. This is available only in Chromium-based browsers. Access it via page.coverage.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File:
packages/playwright-core/src/client/coverage.ts
Signature
export class Coverage implements api.Coverage {
private _channel: channels.PageChannel;
constructor(channel: channels.PageChannel);
async startJSCoverage(options?: channels.PageStartJSCoverageOptions): Promise<void>;
async stopJSCoverage(): Promise<channels.PageStopJSCoverageResult['entries']>;
async startCSSCoverage(options?: channels.PageStartCSSCoverageOptions): Promise<void>;
async stopCSSCoverage(): Promise<channels.PageStopCSSCoverageResult['entries']>;
}
Import
import { Coverage } from 'playwright-core/src/client/coverage';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| channel | channels.PageChannel |
Yes | The page channel to send coverage commands through (constructor parameter) |
| options (startJSCoverage) | PageStartJSCoverageOptions |
No | Options such as resetOnNavigation and reportAnonymousScripts |
| options (startCSSCoverage) | PageStartCSSCoverageOptions |
No | Options such as resetOnNavigation |
Outputs
| Name | Type | Description |
|---|---|---|
| stopJSCoverage() | Promise<CoverageEntry[]> |
Array of JS coverage entries with URL, source text, and function ranges |
| stopCSSCoverage() | Promise<CoverageEntry[]> |
Array of CSS coverage entries with URL, source text, and ranges |
Usage Examples
// Start JS coverage collection
await page.coverage.startJSCoverage();
// Navigate and interact with the page
await page.goto('https://example.com');
await page.click('button');
// Stop and retrieve coverage data
const jsCoverage = await page.coverage.stopJSCoverage();
for (const entry of jsCoverage) {
console.log(`${entry.url}: ${entry.functions.length} functions`);
}
// CSS coverage
await page.coverage.startCSSCoverage();
await page.goto('https://example.com');
const cssCoverage = await page.coverage.stopCSSCoverage();