Coverage 类
¥Coverage class
Coverage 类提供了收集有关页面所使用的 JavaScript 和 CSS 部分的信息的方法。
¥The Coverage class provides methods to gather information about parts of JavaScript and CSS that were used by the page.
签名
¥Signature
export declare class Coverage
备注
¥Remarks
要以 伊斯坦布尔 可以使用的形式输出覆盖范围,请参阅 puppeteer-to-istanbul。
¥To output coverage in a form consumable by Istanbul, see puppeteer-to-istanbul.
此类的构造函数被标记为内部构造函数。第三方代码不应直接调用构造函数或创建扩展 Coverage
类的子类。
¥The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the Coverage
class.
示例
¥Example
使用 JavaScript 和 CSS 覆盖率来获取初始执行代码百分比的示例:
¥An example of using JavaScript and CSS coverage to get percentage of initially executed code:
// Enable both JavaScript and CSS coverage
await Promise.all([
page.coverage.startJSCoverage(),
page.coverage.startCSSCoverage(),
]);
// Navigate to page
await page.goto('https://example.com');
// Disable both JavaScript and CSS coverage
const [jsCoverage, cssCoverage] = await Promise.all([
page.coverage.stopJSCoverage(),
page.coverage.stopCSSCoverage(),
]);
let totalBytes = 0;
let usedBytes = 0;
const coverage = [...jsCoverage, ...cssCoverage];
for (const entry of coverage) {
totalBytes += entry.text.length;
for (const range of entry.ranges) usedBytes += range.end - range.start - 1;
}
console.log(`Bytes used: ${(usedBytes / totalBytes) * 100}%`);
方法
¥Methods
方法 | 修饰符 | 描述 |
---|---|---|
startCSSCoverage(options) | ||
startJSCoverage(options) | 评论: 匿名脚本是没有关联 URL 的脚本。这些是使用 | |
stopCSSCoverage() | Promise 解析所有样式表的覆盖率报告数组。 评论: CSS Coverage 不包括没有 sourceURL 的动态注入样式标签。 | |
stopJSCoverage() | Promise 解析所有脚本的覆盖率报告数组。 评论: 默认情况下,JavaScript 覆盖范围不包括匿名脚本。但是,会报告带有 sourceURL 的脚本。 |