Skip to main content

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();
})();

方法

startCSSCoverage

Added in: v1.11 coverage.startCSSCoverage

Returns coverage is started

使用方式

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

參數

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

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

傳回值


startJSCoverage

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.

使用方式

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

參數

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

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

    • resetOnNavigation boolean (optional)#

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

傳回值


stopCSSCoverage

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.

使用方式

await coverage.stopCSSCoverage();

傳回值

  • Promise<Array<Object>>#
    • url string

      StyleSheet URL

    • text string (optional)

      StyleSheet content, if available.

    • ranges Array<Object>

      • start number

        A start offset in text, inclusive

      • end number

        An end offset in text, exclusive

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


stopJSCoverage

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.

使用方式

await coverage.stopJSCoverage();

傳回值