crCoverage.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.CRCoverage = void 0;
  6. var _eventsHelper = require("../../utils/eventsHelper");
  7. var _utils = require("../../utils");
  8. /**
  9. * Copyright 2017 Google Inc. All rights reserved.
  10. * Modifications copyright (c) Microsoft Corporation.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. class CRCoverage {
  25. constructor(client) {
  26. this._jsCoverage = void 0;
  27. this._cssCoverage = void 0;
  28. this._jsCoverage = new JSCoverage(client);
  29. this._cssCoverage = new CSSCoverage(client);
  30. }
  31. async startJSCoverage(options) {
  32. return await this._jsCoverage.start(options);
  33. }
  34. async stopJSCoverage() {
  35. return await this._jsCoverage.stop();
  36. }
  37. async startCSSCoverage(options) {
  38. return await this._cssCoverage.start(options);
  39. }
  40. async stopCSSCoverage() {
  41. return await this._cssCoverage.stop();
  42. }
  43. }
  44. exports.CRCoverage = CRCoverage;
  45. class JSCoverage {
  46. constructor(client) {
  47. this._client = void 0;
  48. this._enabled = void 0;
  49. this._scriptIds = void 0;
  50. this._scriptSources = void 0;
  51. this._eventListeners = void 0;
  52. this._resetOnNavigation = void 0;
  53. this._reportAnonymousScripts = false;
  54. this._client = client;
  55. this._enabled = false;
  56. this._scriptIds = new Set();
  57. this._scriptSources = new Map();
  58. this._eventListeners = [];
  59. this._resetOnNavigation = false;
  60. }
  61. async start(options) {
  62. (0, _utils.assert)(!this._enabled, 'JSCoverage is already enabled');
  63. const {
  64. resetOnNavigation = true,
  65. reportAnonymousScripts = false
  66. } = options;
  67. this._resetOnNavigation = resetOnNavigation;
  68. this._reportAnonymousScripts = reportAnonymousScripts;
  69. this._enabled = true;
  70. this._scriptIds.clear();
  71. this._scriptSources.clear();
  72. this._eventListeners = [_eventsHelper.eventsHelper.addEventListener(this._client, 'Debugger.scriptParsed', this._onScriptParsed.bind(this)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.executionContextsCleared', this._onExecutionContextsCleared.bind(this)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Debugger.paused', this._onDebuggerPaused.bind(this))];
  73. await Promise.all([this._client.send('Profiler.enable'), this._client.send('Profiler.startPreciseCoverage', {
  74. callCount: true,
  75. detailed: true
  76. }), this._client.send('Debugger.enable'), this._client.send('Debugger.setSkipAllPauses', {
  77. skip: true
  78. })]);
  79. }
  80. _onDebuggerPaused() {
  81. this._client.send('Debugger.resume');
  82. }
  83. _onExecutionContextsCleared() {
  84. if (!this._resetOnNavigation) return;
  85. this._scriptIds.clear();
  86. this._scriptSources.clear();
  87. }
  88. async _onScriptParsed(event) {
  89. this._scriptIds.add(event.scriptId);
  90. // Ignore other anonymous scripts unless the reportAnonymousScripts option is true.
  91. if (!event.url && !this._reportAnonymousScripts) return;
  92. // This might fail if the page has already navigated away.
  93. const response = await this._client._sendMayFail('Debugger.getScriptSource', {
  94. scriptId: event.scriptId
  95. });
  96. if (response) this._scriptSources.set(event.scriptId, response.scriptSource);
  97. }
  98. async stop() {
  99. (0, _utils.assert)(this._enabled, 'JSCoverage is not enabled');
  100. this._enabled = false;
  101. 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')]);
  102. _eventsHelper.eventsHelper.removeEventListeners(this._eventListeners);
  103. const coverage = {
  104. entries: []
  105. };
  106. for (const entry of profileResponse.result) {
  107. if (!this._scriptIds.has(entry.scriptId)) continue;
  108. if (!entry.url && !this._reportAnonymousScripts) continue;
  109. const source = this._scriptSources.get(entry.scriptId);
  110. if (source) coverage.entries.push({
  111. ...entry,
  112. source
  113. });else coverage.entries.push(entry);
  114. }
  115. return coverage;
  116. }
  117. }
  118. class CSSCoverage {
  119. constructor(client) {
  120. this._client = void 0;
  121. this._enabled = void 0;
  122. this._stylesheetURLs = void 0;
  123. this._stylesheetSources = void 0;
  124. this._eventListeners = void 0;
  125. this._resetOnNavigation = void 0;
  126. this._client = client;
  127. this._enabled = false;
  128. this._stylesheetURLs = new Map();
  129. this._stylesheetSources = new Map();
  130. this._eventListeners = [];
  131. this._resetOnNavigation = false;
  132. }
  133. async start(options) {
  134. (0, _utils.assert)(!this._enabled, 'CSSCoverage is already enabled');
  135. const {
  136. resetOnNavigation = true
  137. } = options;
  138. this._resetOnNavigation = resetOnNavigation;
  139. this._enabled = true;
  140. this._stylesheetURLs.clear();
  141. this._stylesheetSources.clear();
  142. this._eventListeners = [_eventsHelper.eventsHelper.addEventListener(this._client, 'CSS.styleSheetAdded', this._onStyleSheet.bind(this)), _eventsHelper.eventsHelper.addEventListener(this._client, 'Runtime.executionContextsCleared', this._onExecutionContextsCleared.bind(this))];
  143. await Promise.all([this._client.send('DOM.enable'), this._client.send('CSS.enable'), this._client.send('CSS.startRuleUsageTracking')]);
  144. }
  145. _onExecutionContextsCleared() {
  146. if (!this._resetOnNavigation) return;
  147. this._stylesheetURLs.clear();
  148. this._stylesheetSources.clear();
  149. }
  150. async _onStyleSheet(event) {
  151. const header = event.header;
  152. // Ignore anonymous scripts
  153. if (!header.sourceURL) return;
  154. // This might fail if the page has already navigated away.
  155. const response = await this._client._sendMayFail('CSS.getStyleSheetText', {
  156. styleSheetId: header.styleSheetId
  157. });
  158. if (response) {
  159. this._stylesheetURLs.set(header.styleSheetId, header.sourceURL);
  160. this._stylesheetSources.set(header.styleSheetId, response.text);
  161. }
  162. }
  163. async stop() {
  164. (0, _utils.assert)(this._enabled, 'CSSCoverage is not enabled');
  165. this._enabled = false;
  166. const ruleTrackingResponse = await this._client.send('CSS.stopRuleUsageTracking');
  167. await Promise.all([this._client.send('CSS.disable'), this._client.send('DOM.disable')]);
  168. _eventsHelper.eventsHelper.removeEventListeners(this._eventListeners);
  169. // aggregate by styleSheetId
  170. const styleSheetIdToCoverage = new Map();
  171. for (const entry of ruleTrackingResponse.ruleUsage) {
  172. let ranges = styleSheetIdToCoverage.get(entry.styleSheetId);
  173. if (!ranges) {
  174. ranges = [];
  175. styleSheetIdToCoverage.set(entry.styleSheetId, ranges);
  176. }
  177. ranges.push({
  178. startOffset: entry.startOffset,
  179. endOffset: entry.endOffset,
  180. count: entry.used ? 1 : 0
  181. });
  182. }
  183. const coverage = {
  184. entries: []
  185. };
  186. for (const styleSheetId of this._stylesheetURLs.keys()) {
  187. const url = this._stylesheetURLs.get(styleSheetId);
  188. const text = this._stylesheetSources.get(styleSheetId);
  189. const ranges = convertToDisjointRanges(styleSheetIdToCoverage.get(styleSheetId) || []);
  190. coverage.entries.push({
  191. url,
  192. ranges,
  193. text
  194. });
  195. }
  196. return coverage;
  197. }
  198. }
  199. function convertToDisjointRanges(nestedRanges) {
  200. const points = [];
  201. for (const range of nestedRanges) {
  202. points.push({
  203. offset: range.startOffset,
  204. type: 0,
  205. range
  206. });
  207. points.push({
  208. offset: range.endOffset,
  209. type: 1,
  210. range
  211. });
  212. }
  213. // Sort points to form a valid parenthesis sequence.
  214. points.sort((a, b) => {
  215. // Sort with increasing offsets.
  216. if (a.offset !== b.offset) return a.offset - b.offset;
  217. // All "end" points should go before "start" points.
  218. if (a.type !== b.type) return b.type - a.type;
  219. const aLength = a.range.endOffset - a.range.startOffset;
  220. const bLength = b.range.endOffset - b.range.startOffset;
  221. // For two "start" points, the one with longer range goes first.
  222. if (a.type === 0) return bLength - aLength;
  223. // For two "end" points, the one with shorter range goes first.
  224. return aLength - bLength;
  225. });
  226. const hitCountStack = [];
  227. const results = [];
  228. let lastOffset = 0;
  229. // Run scanning line to intersect all ranges.
  230. for (const point of points) {
  231. if (hitCountStack.length && lastOffset < point.offset && hitCountStack[hitCountStack.length - 1] > 0) {
  232. const lastResult = results.length ? results[results.length - 1] : null;
  233. if (lastResult && lastResult.end === lastOffset) lastResult.end = point.offset;else results.push({
  234. start: lastOffset,
  235. end: point.offset
  236. });
  237. }
  238. lastOffset = point.offset;
  239. if (point.type === 0) hitCountStack.push(point.range.count);else hitCountStack.pop();
  240. }
  241. // Filter out empty ranges.
  242. return results.filter(range => range.end - range.start > 1);
  243. }