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:
覆盖率 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
用法
await coverage.startCSSCoverage();
await coverage.startCSSCoverage(options);
参数
返回
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.
用法
await coverage.startJSCoverage();
await coverage.startJSCoverage(options);
参数
optionsObject (optional)
返回
stopCSSCoverage
Added in: v1.11返回所有样式表的覆盖率报告数组
🌐 Returns the array of coverage reports for all stylesheets
CSS 覆盖率不包括没有 sourceURL 的动态注入样式标签。
🌐 CSS Coverage doesn't include dynamically injected style tags without sourceURLs.
用法
await coverage.stopCSSCoverage();
返回
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.
用法
await coverage.stopJSCoverage();
返回