Best JavaScript code snippet using puppeteer
Coverage.js
Source:Coverage.js
...176 }177 /**178 * @param {!Object} event179 */180 async _onStyleSheet(event) {181 const header = event.header;182 // Ignore anonymous scripts183 if (!header.sourceURL)184 return;185 try {186 const response = await this._client.send('CSS.getStyleSheetText', {styleSheetId: header.styleSheetId});187 this._stylesheetURLs.set(header.styleSheetId, header.sourceURL);188 this._stylesheetSources.set(header.styleSheetId, response.text);189 } catch (e) {190 // This might happen if the page has already navigated away.191 debugError(e);192 }193 }194 /**...
crCoverage.js
Source:crCoverage.js
...141 if (!this._resetOnNavigation) return;142 this._stylesheetURLs.clear();143 this._stylesheetSources.clear();144 }145 async _onStyleSheet(event) {146 const header = event.header; // Ignore anonymous scripts147 if (!header.sourceURL) return; // This might fail if the page has already navigated away.148 const response = await this._client._sendMayFail('CSS.getStyleSheetText', {149 styleSheetId: header.styleSheetId150 });151 if (response) {152 this._stylesheetURLs.set(header.styleSheetId, header.sourceURL);153 this._stylesheetSources.set(header.styleSheetId, response.text);154 }155 }156 async stop() {157 (0, _utils.assert)(this._enabled, 'CSSCoverage is not enabled');158 this._enabled = false;159 const ruleTrackingResponse = await this._client.send('CSS.stopRuleUsageTracking');...
CSSCoverage.js
Source:CSSCoverage.js
1const { helper, debugError, assert } = require('../helper')2const { convertToDisjointRanges } = require('../__shared')3class CSSCoverage {4 /**5 * @param {!Chrome|CRIConnection|CDPSession|Object} client6 */7 constructor (client) {8 this._client = client9 this._enabled = false10 this._stylesheetURLs = new Map()11 this._stylesheetSources = new Map()12 this._eventListeners = []13 this._resetOnNavigation = false14 }15 /**16 * @param {{resetOnNavigation?: boolean}=} options17 */18 async start (options = {}) {19 assert(!this._enabled, 'CSSCoverage is already enabled')20 const { resetOnNavigation = true } = options21 this._resetOnNavigation = resetOnNavigation22 this._enabled = true23 this._stylesheetURLs.clear()24 this._stylesheetSources.clear()25 this._eventListeners = [26 helper.addEventListener(27 this._client,28 'CSS.styleSheetAdded',29 this._onStyleSheet.bind(this)30 ),31 helper.addEventListener(32 this._client,33 'Runtime.executionContextsCleared',34 this._onExecutionContextsCleared.bind(this)35 )36 ]37 await Promise.all([38 this._client.send('DOM.enable'),39 this._client.send('CSS.enable'),40 this._client.send('CSS.startRuleUsageTracking')41 ])42 }43 _onExecutionContextsCleared () {44 if (!this._resetOnNavigation) return45 this._stylesheetURLs.clear()46 this._stylesheetSources.clear()47 }48 /**49 * @param {!Object} event50 */51 async _onStyleSheet (event) {52 const header = event.header53 // Ignore anonymous scripts54 if (!header.sourceURL) return55 try {56 const response = await this._client.send('CSS.getStyleSheetText', {57 styleSheetId: header.styleSheetId58 })59 this._stylesheetURLs.set(header.styleSheetId, header.sourceURL)60 this._stylesheetSources.set(header.styleSheetId, response.text)61 } catch (e) {62 // This might happen if the page has already navigated away.63 debugError(e)64 }65 }66 /**67 * @return {Promise<Array<CoverageEntry>>}68 */69 async stop () {70 assert(this._enabled, 'CSSCoverage is not enabled')71 this._enabled = false72 const ruleTrackingResponse = await this._client.send(73 'CSS.stopRuleUsageTracking'74 )75 await Promise.all([76 this._client.send('CSS.disable'),77 this._client.send('DOM.disable')78 ])79 helper.removeEventListeners(this._eventListeners)80 // aggregate by styleSheetId81 const styleSheetIdToCoverage = new Map()82 for (const entry of ruleTrackingResponse.ruleUsage) {83 let ranges = styleSheetIdToCoverage.get(entry.styleSheetId)84 if (!ranges) {85 ranges = []86 styleSheetIdToCoverage.set(entry.styleSheetId, ranges)87 }88 ranges.push({89 startOffset: entry.startOffset,90 endOffset: entry.endOffset,91 count: entry.used ? 1 : 092 })93 }94 const coverage = []95 for (const styleSheetId of this._stylesheetURLs.keys()) {96 const url = this._stylesheetURLs.get(styleSheetId)97 const text = this._stylesheetSources.get(styleSheetId)98 const ranges = convertToDisjointRanges(99 styleSheetIdToCoverage.get(styleSheetId) || []100 )101 coverage.push({ url, ranges, text })102 }103 return coverage104 }105}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!