Skip to content

Coverage

Source URL: https://playwright.dev/docs/api/class-coverage

Coverage gathers information about parts of JavaScript and CSS that were used by the page.

An example of using JavaScript coverage to produce Istanbul report for page load:

note

Coverage APIs are only supported on Chromium-based browsers.

const { chromium } = require('playwright');
const v8toIstanbul = require('v8-to-istanbul');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.coverage.startJSCoverage();
await page.goto('https://chromium.org');
const coverage = await page.coverage.stopJSCoverage();
for (const entry of coverage) {
const converter = v8toIstanbul('', 0, { source: entry.source });
await converter.load();
converter.applyCoverage(entry.functions);
console.log(JSON.stringify(converter.toIstanbul()));
}
await browser.close();
})();

Added in: v1.11 coverage.startCSSCoverage

Returns coverage is started

Usage

await coverage.startCSSCoverage();
await coverage.startCSSCoverage(options);

Arguments

Whether to reset coverage on every navigation. Defaults to true.

Returns


Added in: v1.11 coverage.startJSCoverage

Returns coverage is started

note

Anonymous scripts are ones that don’t have an associated url. These are scripts that are dynamically created on the page using eval or new Function. If reportAnonymousScripts is set to true, anonymous scripts will have __playwright_evaluation_script__ as their URL.

Usage

await coverage.startJSCoverage();
await coverage.startJSCoverage(options);

Arguments

  • options Object (optional)
    • reportAnonymousScripts boolean (optional)#

Whether anonymous scripts generated by the page should be reported. Defaults to false.

* `resetOnNavigation` [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean") _(optional)_[#](https://playwright.dev/docs/api/class-coverage#coverage-start-js-coverage-option-reset-on-navigation)

Whether to reset coverage on every navigation. Defaults to true.

Returns


Added in: v1.11 coverage.stopCSSCoverage

Returns the array of coverage reports for all stylesheets

note

CSS Coverage doesn’t include dynamically injected style tags without sourceURLs.

Usage

await coverage.stopCSSCoverage();

Returns

StyleSheet URL

* `text` [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "string") _(optional)_

StyleSheet content, if available.

* `ranges` [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")>
* `start` [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")

A start offset in text, inclusive

* `end` [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")

An end offset in text, exclusive

StyleSheet ranges that were used. Ranges are sorted and non-overlapping.


Added in: v1.11 coverage.stopJSCoverage

Returns the array of coverage reports for all scripts

note

JavaScript Coverage doesn’t include anonymous scripts by default. However, scripts with sourceURLs are reported.

Usage

await coverage.stopJSCoverage();

Returns

Script URL

* `scriptId` [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "string")

Script ID

* `source` [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "string") _(optional)_

Script content, if applicable.

* `functions` [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")>
* `functionName` [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "string")
* `isBlockCoverage` [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")
* `ranges` [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")>
* `count` [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")
* `startOffset` [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")
* `endOffset` [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")

V8-specific coverage format.