puppeteer
Advanced tools
Comparing version 1.0.0-next.1515022965203 to 1.0.0-next.1515032607003
@@ -19,2 +19,9 @@ /** | ||
/** | ||
* @typedef {Object} CoverageEntry | ||
* @property {string} url | ||
* @property {string} text | ||
* @property {!Array<!{start: number, end: number}>} ranges | ||
*/ | ||
class Coverage { | ||
@@ -26,2 +33,3 @@ /** | ||
this._jsCoverage = new JSCoverage(client); | ||
this._cssCoverage = new CSSCoverage(client); | ||
} | ||
@@ -36,5 +44,22 @@ | ||
/** | ||
* @return {!Promise<!Array<!CoverageEntry>>} | ||
*/ | ||
async stopJSCoverage() { | ||
return await this._jsCoverage.stop(); | ||
} | ||
/** | ||
* @param {!Object} options | ||
*/ | ||
async startCSSCoverage(options) { | ||
return await this._cssCoverage.start(options); | ||
} | ||
/** | ||
* @return {!Promise<!Array<!CoverageEntry>>} | ||
*/ | ||
async stopCSSCoverage() { | ||
return await this._cssCoverage.stop(); | ||
} | ||
} | ||
@@ -104,3 +129,3 @@ | ||
/** | ||
* @return {!Promise<!Array<!{url:string, text:string, ranges:!Array<!{start:number, end:number}>}>>} | ||
* @return {!Promise<!Array<!CoverageEntry>>} | ||
*/ | ||
@@ -134,2 +159,100 @@ async stop() { | ||
class CSSCoverage { | ||
/** | ||
* @param {!Puppeteer.Session} client | ||
*/ | ||
constructor(client) { | ||
this._client = client; | ||
this._enabled = false; | ||
this._stylesheetURLs = new Map(); | ||
this._stylesheetSources = new Map(); | ||
this._eventListeners = []; | ||
this._resetOnNavigation = false; | ||
} | ||
/** | ||
* @param {!Object} options | ||
*/ | ||
async start(options = {}) { | ||
console.assert(!this._enabled, 'CSSCoverage is already enabled'); | ||
this._resetOnNavigation = options.resetOnNavigation === undefined ? true : !!options.resetOnNavigation; | ||
this._enabled = true; | ||
this._stylesheetURLs.clear(); | ||
this._stylesheetSources.clear(); | ||
this._eventListeners = [ | ||
helper.addEventListener(this._client, 'CSS.styleSheetAdded', this._onStyleSheet.bind(this)), | ||
helper.addEventListener(this._client, 'Runtime.executionContextsCleared', this._onExecutionContextsCleared.bind(this)), | ||
]; | ||
await Promise.all([ | ||
this._client.send('DOM.enable'), | ||
this._client.send('CSS.enable'), | ||
this._client.send('CSS.startRuleUsageTracking'), | ||
]); | ||
} | ||
_onExecutionContextsCleared() { | ||
if (!this._resetOnNavigation) | ||
return; | ||
this._stylesheetURLs.clear(); | ||
this._stylesheetSources.clear(); | ||
} | ||
/** | ||
* @param {!Object} event | ||
*/ | ||
async _onStyleSheet(event) { | ||
const header = event.header; | ||
// Ignore anonymous scripts | ||
if (!header.sourceURL) | ||
return; | ||
try { | ||
const response = await this._client.send('CSS.getStyleSheetText', {styleSheetId: header.styleSheetId}); | ||
this._stylesheetURLs.set(header.styleSheetId, header.sourceURL); | ||
this._stylesheetSources.set(header.styleSheetId, response.text); | ||
} catch (e) { | ||
// This might happen if the page has already navigated away. | ||
debugError(e); | ||
} | ||
} | ||
/** | ||
* @return {!Promise<!Array<!CoverageEntry>>} | ||
*/ | ||
async stop() { | ||
console.assert(this._enabled, 'CSSCoverage is not enabled'); | ||
this._enabled = false; | ||
const [ruleTrackingResponse] = await Promise.all([ | ||
this._client.send('CSS.stopRuleUsageTracking'), | ||
this._client.send('CSS.disable'), | ||
this._client.send('DOM.disable'), | ||
]); | ||
helper.removeEventListeners(this._eventListeners); | ||
// aggregarte by styleSheetId | ||
const styleSheetIdToCoverage = new Map(); | ||
for (const entry of ruleTrackingResponse.ruleUsage) { | ||
let ranges = styleSheetIdToCoverage.get(entry.styleSheetId); | ||
if (!ranges) { | ||
ranges = []; | ||
styleSheetIdToCoverage.set(entry.styleSheetId, ranges); | ||
} | ||
ranges.push({ | ||
startOffset: entry.startOffset, | ||
endOffset: entry.endOffset, | ||
count: entry.used ? 1 : 0, | ||
}); | ||
} | ||
const coverage = []; | ||
for (const styleSheetId of this._stylesheetURLs.keys()) { | ||
const url = this._stylesheetURLs.get(styleSheetId); | ||
const text = this._stylesheetSources.get(styleSheetId); | ||
const ranges = convertToDisjointRanges(styleSheetIdToCoverage.get(styleSheetId) || []); | ||
coverage.push({url, ranges, text}); | ||
} | ||
return coverage; | ||
} | ||
} | ||
/** | ||
@@ -136,0 +259,0 @@ * @param {!Array<!{startOffset:number, endOffset:number, count:number}>} nestedRanges |
{ | ||
"name": "puppeteer", | ||
"version": "1.0.0-next.1515022965203", | ||
"version": "1.0.0-next.1515032607003", | ||
"description": "A high-level API to control headless Chrome over the DevTools Protocol", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
250014
6318