class: ChromiumCoverage

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

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

  1. const { chromium } = require('.');
  2. const v8toIstanbul = require('v8-to-istanbul');
  3. (async() => {
  4. const browser = await chromium.launch();
  5. const page = await browser.newPage();
  6. await page.coverage.startJSCoverage();
  7. await page.goto('https://chromium.org');
  8. const coverage = await page.coverage.stopJSCoverage();
  9. for (const entry of coverage) {
  10. const converter = new v8toIstanbul('', 0, { source: entry.source });
  11. await converter.load();
  12. converter.applyCoverage(entry.functions);
  13. console.log(JSON.stringify(converter.toIstanbul()));
  14. }
  15. await browser.close();
  16. })();

chromiumCoverage.startCSSCoverage([options])

  • options <Object> Set of configurable options for coverage
    • resetOnNavigation <boolean> Whether to reset coverage on every navigation. Defaults to true.
  • returns: <Promise> Promise that resolves when coverage is started

chromiumCoverage.startJSCoverage([options])

  • options <Object> Set of configurable options for coverage
    • resetOnNavigation <boolean> Whether to reset coverage on every navigation. Defaults to true.
    • reportAnonymousScripts <boolean> Whether anonymous scripts generated by the page should be reported. Defaults to false.
  • returns: <Promise> Promise that resolves when 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.

chromiumCoverage.stopCSSCoverage()

  • returns: <Promise<Array<Object>>> Promise that resolves to the array of coverage reports for all stylesheets
    • url <string> StyleSheet URL
    • text <string> StyleSheet content, if available.
    • ranges <Array<Object>> StyleSheet ranges that were used. Ranges are sorted and non-overlapping.
      • start <number> A start offset in text, inclusive
      • end <number> An end offset in text, exclusive

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

chromiumCoverage.stopJSCoverage()

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