# class:coverage
### class: Coverage v0.9.0
Coverage 收集相关页面使用的 JavaScript 和 CSS 部分的信息。
使用 JavaScript 和 CSS 覆盖率来获取初始百分比的例子 执行代码:
```
// 启用 JavaScript 和 CSS 覆盖await Promise.all([ page.coverage.startJSCoverage(), page.coverage.startCSSCoverage()]);// 导航至页面await page.goto('https://example.com');// 禁用 JavaScript 和 CSS 覆盖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}%`);
```
*使用 [Istanbul](https://github.com/istanbuljs) 输出一个覆盖率表格,见 [puppeteer-to-istanbul](https://github.com/istanbuljs/puppeteer-to-istanbul).*
#### Methods
- [coverage.startCSSCoverage(options)](#?product=Puppeteer&version=v1.11.0&show=api-coveragestartcsscoverageoptions)v0.9.0
- [coverage.startJSCoverage(options)](#?product=Puppeteer&version=v1.11.0&show=api-coveragestartjscoverageoptions)v0.9.0
- [coverage.stopCSSCoverage()](#?product=Puppeteer&version=v1.11.0&show=api-coveragestopcsscoverage)v0.9.0
- [coverage.stopJSCoverage()](#?product=Puppeteer&version=v1.11.0&show=api-coveragestopjscoverage)v0.9.0
### Methods
#### coverage.startCSSCoverage(options)v0.9.0
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")> 覆盖范围的配置项
- `resetOnNavigation` <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> 是否重置每个导航的覆盖范围。默认是 `true`。
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")> Promise that resolves when coverage is started
#### coverage.startJSCoverage(options)v0.9.0
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")> 覆盖范围的配置项
- `resetOnNavigation` <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> 是否重置每个导航的覆盖范围。默认是 `true`。
- `reportAnonymousScripts` <[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean")> 是否应报告页面生成的匿名脚本。 默认为 `false`。
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")> Promise that resolves when coverage is started
> **注意** 匿名脚本指的是没有关联 URL 的脚本。 它们是使用 `eval` 或 `new Function` 在页面上动态创建的脚本。如果`reportAnonymousScripts` 设置为 `true`,匿名脚本将使用 `__puppeteer_evaluation_script__` 作为其URL。
#### coverage.stopCSSCoverage()v0.9.0
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")>>> Promise that resolves to the array of coverage reports for all stylesheets
- `url` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> 样式表 URL
- `text` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> 样式表内容
- `ranges` <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")>> 所使用的StyleSheet范围。 范围已排序且不重叠。
- `start` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> 包含文字的起始偏移量
- `end` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> 文本中的结尾偏移,独占
> **注意** CSS Coverage 不包含没有 sourceURL 的动态注入式样式标签。
#### coverage.stopJSCoverage()v0.9.0
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")>>> Promise that resolves to the array of coverage reports for all non-anonymous scripts
- `url` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> 脚本 URL
- `text` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> 脚本内容
- `ranges` <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array")<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object")>> 已执行的脚本范围。 范围已排序且不重叠。
- `start` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> 包含文字的起始偏移量
- `end` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number")> 文本中的结尾偏移,独占
> **注意** JavaScript Coverage 默认情况下不包含匿名脚本。 但是,具有 sourceURL 的脚本将被上报。
![](images/pptr.png)
puppeteer.js中文网|class:coverage
puppeteer.js中文文档, puppeteer chrome, puppeteer firefox, puppeteer api 中文文档
puppeteer.js中文网包含了Puppeteer中文文档,最新资讯,应用案例等。Puppeteer 是一个 Node 库,它提供了一个高级 API 来通过 DevTools 协议控制 Chromium 或 Chrome。
- Introduction
- 版本记录
- 概要
- puppeteer-vs-puppeteer-core
- 环境变量
- error-handling
- working-with-chrome-extensions
- class:puppeteer
- class:browserfetcher
- class:browser
- class:page
- class:worker
- class:accessibility
- class:keyboard
- class:mouse
- class:touchscreen
- class:tracing
- class:dialog
- class:consolemessage
- class:frame
- class:executioncontext
- class:jshandle
- class:elementhandle
- class:request
- class:response
- class:securitydetails
- class:target
- class:cdpsession
- class:coverage
- class:timeouterror