feat: implement CSS Coverage (#1714)

This patch adds two new methods to the `page.coverage` namespace:
- `page.coverage.startCSSCoverage()` - to initiate css coverage
- `page.coverage.stopCSSCoverage()` - to stop css coverage

The coverage format is consistent with the JavaScript coverage.
This commit is contained in:
Andrey Lushnikov
2018-01-03 18:21:40 -08:00
committed by GitHub
parent f183664d0f
commit 24354a4879
15 changed files with 427 additions and 17 deletions

View File

@@ -199,7 +199,9 @@
* [target.type()](#targettype)
* [target.url()](#targeturl)
- [class: Coverage](#class-coverage)
* [coverage.startCSSCoverage(options)](#coveragestartcsscoverageoptions)
* [coverage.startJSCoverage(options)](#coveragestartjscoverageoptions)
* [coverage.stopCSSCoverage()](#coveragestopcsscoverage)
* [coverage.stopJSCoverage()](#coveragestopjscoverage)
<!-- tocstop -->
@@ -2155,11 +2157,56 @@ Identifies what kind of target this is. Can be `"page"`, `"service_worker"`, or
### class: Coverage
Coverage gathers information about parts of JavaScript and CSS that were used by the page.
An example of using JavaScript and CSS coverage to get percentage of initially
executed code:
```js
// Enable both JavaScript and CSS coverage
await Promise.all([
page.startJSCoverage(),
page.startCSSCoverage()
]);
// Navigate to page
await page.goto('https://example.com');
// Disable both JavaScript and CSS coverage
const [jsCoverage, cssCoverage] = await Promise.all([
page.stopJSCoverage(),
page.stopCSSCoverage(),
]);
let totalBytes = 0;
let usedBytes = 0;
const coverage = [].concat(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}%`);
```
#### coverage.startCSSCoverage(options)
- `options` <[Object]> Set of configurable options for coverage
- `resetOnNavigation` <[boolean]> Whether to reset coverage on every navigation. Defaults to `true`.
- returns: <[Promise]> Promise that resolves when coverage is started
#### coverage.startJSCoverage(options)
- `options` <[Object]> Set of configurable options for coverage
- `resetOnNavigation` <[boolean]> Whether to reset coverage on every navigation. Defaults to `true`.
- returns: <[Promise]> Promise that resolves when coverage is started
#### coverage.stopCSSCoverage()
- returns: <[Promise]<[Array]<[Object]>>> Promise that resolves to the array of coverage reports for all stylesheets
- `url` <[string]> StyleSheet URL
- `text` <[string]> StyleSheet content
- `ranges` <[Array]<[Object]>> StyleSheet ranges that were used. Ranges are sorted and non-overlapping.
- `start` <[number]> A start offset in text, inclusive
- `end` <[number]> An end offset in text, exclusive
> **NOTE** CSS Coverage doesn't include dynamically injected style tags without sourceURLs.
#### coverage.stopJSCoverage()
- returns: <[Promise]<[Array]<[Object]>>> Promise that resolves to the array of coverage reports for all non-anonymous scripts
- `url` <[string]> Script URL
@@ -2171,22 +2218,6 @@ Identifies what kind of target this is. Can be `"page"`, `"service_worker"`, or
> **NOTE** JavaScript Coverage doesn't include anonymous scripts; however, scripts with sourceURLs are
reported.
An example of using JavaScript coverage to get percentage of executed
code:
```js
await page.startJSCoverage();
await page.goto('https://example.com');
const coverage = await page.stopJSCoverage();
let totalBytes = 0;
let usedBytes = 0;
for (const entry of coverage) {
totalBytes += entry.text.length;
for (const range of entry.ranges)
usedBytes += range.end - range.start - 1;
}
console.log(`Coverage is ${usedBytes / totalBytes * 100}%`);
```
[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array"
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean"