Skip to main content
Version: 24.38.0

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

要以 Istanbul 可使用的格式输出覆盖率,请参阅 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

方法

🌐 Method

修饰符

🌐 Modifiers

描述

🌐 Description

startCSSCoverage(options)
startJSCoverage(options)

附注

匿名脚本是没有关联 URL 的脚本。这些脚本是在页面上使用 evalnew Function 动态创建的。如果 reportAnonymousScripts 被设置为 true,匿名脚本的 URL 将以 debugger://VM 开头(除非存在一个魔法 //# sourceURL 注释,在这种情况下,该注释将作为 URL)。

🌐 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 URL will start with debugger://VM (unless a magic //# sourceURL comment is present, in which case that will the be URL).

stopCSSCoverage()

Promise 解析所有样式表的覆盖率报告数组。

🌐 Promise that resolves to the array of coverage reports for all stylesheets.

附注

CSS Coverage 不包括没有 sourceURL 的动态注入样式标签。

🌐 CSS Coverage doesn't include dynamically injected style tags without sourceURLs.

stopJSCoverage()

Promise 解析所有脚本的覆盖率报告数组。

🌐 Promise that resolves to 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.