179 lines
5.6 KiB
JavaScript
179 lines
5.6 KiB
JavaScript
|
/**
|
||
|
* Copyright 2017 Google Inc. All rights reserved.
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
|
const {helper, debugError} = require('./helper');
|
||
|
|
||
|
class Coverage {
|
||
|
/**
|
||
|
* @param {!Puppeteer.Session} client
|
||
|
*/
|
||
|
constructor(client) {
|
||
|
this._jsCoverage = new JSCoverage(client);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param {!Object} options
|
||
|
*/
|
||
|
async startJSCoverage(options) {
|
||
|
return await this._jsCoverage.start(options);
|
||
|
}
|
||
|
|
||
|
async stopJSCoverage() {
|
||
|
return await this._jsCoverage.stop();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = {Coverage};
|
||
|
helper.tracePublicAPI(Coverage);
|
||
|
|
||
|
class JSCoverage {
|
||
|
/**
|
||
|
* @param {!Puppeteer.Session} client
|
||
|
*/
|
||
|
constructor(client) {
|
||
|
this._client = client;
|
||
|
this._enabled = false;
|
||
|
this._scriptURLs = new Map();
|
||
|
this._scriptSources = new Map();
|
||
|
this._eventListeners = [];
|
||
|
this._resetOnNavigation = false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param {!Object} options
|
||
|
*/
|
||
|
async start(options = {}) {
|
||
|
console.assert(!this._enabled, 'JSCoverage is already enabled');
|
||
|
this._resetOnNavigation = options.resetOnNavigation === undefined ? true : !!options.resetOnNavigation;
|
||
|
this._enabled = true;
|
||
|
this._scriptURLs.clear();
|
||
|
this._scriptSources.clear();
|
||
|
this._eventListeners = [
|
||
|
helper.addEventListener(this._client, 'Debugger.scriptParsed', this._onScriptParsed.bind(this)),
|
||
|
helper.addEventListener(this._client, 'Runtime.executionContextsCleared', this._onExecutionContextsCleared.bind(this)),
|
||
|
];
|
||
|
await Promise.all([
|
||
|
this._client.send('Profiler.enable'),
|
||
|
this._client.send('Profiler.startPreciseCoverage', {callCount: false, detailed: true}),
|
||
|
this._client.send('Debugger.enable'),
|
||
|
this._client.send('Debugger.setSkipAllPauses', {skip: true})
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
_onExecutionContextsCleared() {
|
||
|
if (!this._resetOnNavigation)
|
||
|
return;
|
||
|
this._scriptURLs.clear();
|
||
|
this._scriptSources.clear();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param {!Object} event
|
||
|
*/
|
||
|
async _onScriptParsed(event) {
|
||
|
// Ignore anonymous scripts
|
||
|
if (!event.url)
|
||
|
return;
|
||
|
try {
|
||
|
const response = await this._client.send('Debugger.getScriptSource', {scriptId: event.scriptId});
|
||
|
this._scriptURLs.set(event.scriptId, event.url);
|
||
|
this._scriptSources.set(event.scriptId, response.scriptSource);
|
||
|
} catch (e) {
|
||
|
// This might happen if the page has already navigated away.
|
||
|
debugError(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return {!Promise<!Array<!{url:string, text:string, ranges:!Array<!{start:number, end:number}>}>>}
|
||
|
*/
|
||
|
async stop() {
|
||
|
console.assert(this._enabled, 'JSCoverage is not enabled');
|
||
|
this._enabled = false;
|
||
|
const [profileResponse] = await Promise.all([
|
||
|
this._client.send('Profiler.takePreciseCoverage'),
|
||
|
this._client.send('Profiler.stopPreciseCoverage'),
|
||
|
this._client.send('Profiler.disable'),
|
||
|
this._client.send('Debugger.disable'),
|
||
|
]);
|
||
|
helper.removeEventListeners(this._eventListeners);
|
||
|
|
||
|
const coverage = [];
|
||
|
for (const entry of profileResponse.result) {
|
||
|
const url = this._scriptURLs.get(entry.scriptId);
|
||
|
const text = this._scriptSources.get(entry.scriptId);
|
||
|
if (text === undefined || url === undefined)
|
||
|
continue;
|
||
|
const flattenRanges = [];
|
||
|
for (const func of entry.functions)
|
||
|
flattenRanges.push(...func.ranges);
|
||
|
const ranges = convertToDisjointRanges(flattenRanges);
|
||
|
coverage.push({url, ranges, text});
|
||
|
}
|
||
|
return coverage;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param {!Array<!{startOffset:number, endOffset:number, count:number}>} nestedRanges
|
||
|
* @return {!Array<!{start:number, end:number}>}
|
||
|
*/
|
||
|
function convertToDisjointRanges(nestedRanges) {
|
||
|
const points = [];
|
||
|
for (const range of nestedRanges) {
|
||
|
points.push({ offset: range.startOffset, type: 0, range });
|
||
|
points.push({ offset: range.endOffset, type: 1, range });
|
||
|
}
|
||
|
// Sort points to form a valid parenthesis sequence.
|
||
|
points.sort((a, b) => {
|
||
|
// Sort with increasing offsets.
|
||
|
if (a.offset !== b.offset)
|
||
|
return a.offset - b.offset;
|
||
|
// All "end" points should go before "start" points.
|
||
|
if (a.type !== b.type)
|
||
|
return b.type - a.type;
|
||
|
const aLength = a.range.endOffset - a.range.startOffset;
|
||
|
const bLength = b.range.endOffset - b.range.startOffset;
|
||
|
// For two "start" points, the one with longer range goes first.
|
||
|
if (a.type === 0)
|
||
|
return bLength - aLength;
|
||
|
// For two "end" points, the one with shorter range goes first.
|
||
|
return aLength - bLength;
|
||
|
});
|
||
|
|
||
|
const hitCountStack = [];
|
||
|
const results = [];
|
||
|
let lastOffset = 0;
|
||
|
// Run scanning line to intersect all ranges.
|
||
|
for (const point of points) {
|
||
|
if (hitCountStack.length && lastOffset < point.offset && hitCountStack[hitCountStack.length - 1] > 0) {
|
||
|
const lastResult = results.length ? results[results.length - 1] : null;
|
||
|
if (lastResult && lastResult.end === lastOffset)
|
||
|
lastResult.end = point.offset;
|
||
|
else
|
||
|
results.push({start: lastOffset, end: point.offset});
|
||
|
}
|
||
|
lastOffset = point.offset;
|
||
|
if (point.type === 0)
|
||
|
hitCountStack.push(point.range.count);
|
||
|
else
|
||
|
hitCountStack.pop();
|
||
|
}
|
||
|
// Filter out empty ranges.
|
||
|
return results.filter(range => range.end - range.start > 1);
|
||
|
}
|
||
|
|