Coverage
覆盖范围收集有关页面使用的 JavaScript 和 CSS 部分的信息。
¥Coverage gathers information about parts of JavaScript and CSS that were used by the page.
使用 JavaScript 覆盖率生成伊斯坦布尔页面加载报告的示例:
¥An example of using JavaScript coverage to produce Istanbul report for page load:
Coverage API 仅在基于 Chromium 的浏览器上受支持。
¥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();
})();
方法
¥Methods
startCSSCoverage
Added in: v1.11返回保障已开始
¥Returns coverage is started
用法
¥Usage
await coverage.startCSSCoverage();
await coverage.startCSSCoverage(options);
参数
¥Arguments
是否重置每次导航的覆盖范围。默认为 true。
¥Whether to reset coverage on every navigation. Defaults to true.
返回
¥Returns
startJSCoverage
Added in: v1.11返回保障已开始
¥Returns coverage is started
匿名脚本是没有关联 URL 的脚本。这些是使用 eval 或 new Function 在页面上动态创建的脚本。如果 reportAnonymousScripts 设置为 true,匿名脚本的 URL 将为 __playwright_evaluation_script__。
¥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
是否应报告页面生成的匿名脚本。默认为 false。
¥Whether anonymous scripts generated by the page should be reported. Defaults to false.
是否重置每次导航的覆盖范围。默认为 true。
¥Whether to reset coverage on every navigation. Defaults to true.
返回
¥Returns
stopCSSCoverage
Added in: v1.11返回所有样式表的覆盖率报告数组
¥Returns the array of coverage reports for all stylesheets
CSS Coverage 不包括没有 sourceURL 的动态注入样式标签。
¥CSS Coverage doesn't include dynamically injected style tags without sourceURLs.
用法
¥Usage
await coverage.stopCSSCoverage();
返回
¥Returns
样式表 URL
¥StyleSheet URL
textstring (optional)
样式表内容(如果有)。
¥StyleSheet content, if available.
文本中的起始偏移量,包括在内
¥A start offset in text, inclusive
endnumber
文本中的结束偏移量,独占
¥An end offset in text, exclusive
使用的样式表范围。范围已排序且不重叠。
¥StyleSheet ranges that were used. Ranges are sorted and non-overlapping.
stopJSCoverage
Added in: v1.11返回所有脚本的覆盖率报告数组
¥Returns the array of coverage reports for all scripts
默认情况下,JavaScript 覆盖范围不包括匿名脚本。但是,会报告带有 sourceURL 的脚本。
¥JavaScript Coverage doesn't include anonymous scripts by default. However, scripts with sourceURLs are reported.
用法
¥Usage
await coverage.stopJSCoverage();
返回
¥Returns
脚本网址
¥Script URL
scriptIdstring
脚本 ID
¥Script ID
sourcestring (optional)
脚本内容(如果适用)。
¥Script content, if applicable.
V8 特定的覆盖范围格式。
¥V8-specific coverage format.