How to use startJSCoverage method in Puppeteer

Best JavaScript code snippet using puppeteer

Run Puppeteer automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

coverage.spec.js

Source: coverage.spec.js Github

copy
1/**
2 * Copyright 2018 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17module.exports.addTests = function({testRunner, expect}) {
18  const {describe, xdescribe, fdescribe} = testRunner;
19  const {it, fit, xit} = testRunner;
20  const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
21
22  describe('JSCoverage', function() {
23    it('should work', async function({page, server}) {
24      await page.coverage.startJSCoverage();
25      await page.goto(server.PREFIX + '/jscoverage/simple.html', {waitUntil: 'networkidle0'});
26      const coverage = await page.coverage.stopJSCoverage();
27      expect(coverage.length).toBe(1);
28      expect(coverage[0].url).toContain('/jscoverage/simple.html');
29      expect(coverage[0].ranges).toEqual([
30        { start: 0, end: 17 },
31        { start: 35, end: 61 },
32      ]);
33    });
34    it('should report sourceURLs', async function({page, server}) {
35      await page.coverage.startJSCoverage();
36      await page.goto(server.PREFIX + '/jscoverage/sourceurl.html');
37      const coverage = await page.coverage.stopJSCoverage();
38      expect(coverage.length).toBe(1);
39      expect(coverage[0].url).toBe('nicename.js');
40    });
41    it('should ignore eval() scripts by default', async function({page, server}) {
42      await page.coverage.startJSCoverage();
43      await page.goto(server.PREFIX + '/jscoverage/eval.html');
44      const coverage = await page.coverage.stopJSCoverage();
45      expect(coverage.length).toBe(1);
46    });
47    it('shouldn\'t ignore eval() scripts if reportAnonymousScripts is true', async function({page, server}) {
48      await page.coverage.startJSCoverage({reportAnonymousScripts: true});
49      await page.goto(server.PREFIX + '/jscoverage/eval.html');
50      const coverage = await page.coverage.stopJSCoverage();
51      expect(coverage.find(entry => entry.url.startsWith('debugger://'))).not.toBe(null);
52      expect(coverage.length).toBe(2);
53    });
54    it('should ignore pptr internal scripts if reportAnonymousScripts is true', async function({page, server}) {
55      await page.coverage.startJSCoverage({reportAnonymousScripts: true});
56      await page.goto(server.EMPTY_PAGE);
57      await page.evaluate('console.log("foo")');
58      await page.evaluate(() => console.log('bar'));
59      const coverage = await page.coverage.stopJSCoverage();
60      expect(coverage.length).toBe(0);
61    });
62    it('should report multiple scripts', async function({page, server}) {
63      await page.coverage.startJSCoverage();
64      await page.goto(server.PREFIX + '/jscoverage/multiple.html');
65      const coverage = await page.coverage.stopJSCoverage();
66      expect(coverage.length).toBe(2);
67      coverage.sort((a, b) => a.url.localeCompare(b.url));
68      expect(coverage[0].url).toContain('/jscoverage/script1.js');
69      expect(coverage[1].url).toContain('/jscoverage/script2.js');
70    });
71    it('should report right ranges', async function({page, server}) {
72      await page.coverage.startJSCoverage();
73      await page.goto(server.PREFIX + '/jscoverage/ranges.html');
74      const coverage = await page.coverage.stopJSCoverage();
75      expect(coverage.length).toBe(1);
76      const entry = coverage[0];
77      expect(entry.ranges.length).toBe(1);
78      const range = entry.ranges[0];
79      expect(entry.text.substring(range.start, range.end)).toBe(`console.log('used!');`);
80    });
81    it('should report scripts that have no coverage', async function({page, server}) {
82      await page.coverage.startJSCoverage();
83      await page.goto(server.PREFIX + '/jscoverage/unused.html');
84      const coverage = await page.coverage.stopJSCoverage();
85      expect(coverage.length).toBe(1);
86      const entry = coverage[0];
87      expect(entry.url).toContain('unused.html');
88      expect(entry.ranges.length).toBe(0);
89    });
90    it('should work with conditionals', async function({page, server}) {
91      await page.coverage.startJSCoverage();
92      await page.goto(server.PREFIX + '/jscoverage/involved.html');
93      const coverage = await page.coverage.stopJSCoverage();
94      expect(JSON.stringify(coverage, null, 2).replace(/:\d{4}\//g, ':<PORT>/')).toBeGolden('jscoverage-involved.txt');
95    });
96    describe('resetOnNavigation', function() {
97      it('should report scripts across navigations when disabled', async function({page, server}) {
98        await page.coverage.startJSCoverage({resetOnNavigation: false});
99        await page.goto(server.PREFIX + '/jscoverage/multiple.html');
100        await page.goto(server.EMPTY_PAGE);
101        const coverage = await page.coverage.stopJSCoverage();
102        expect(coverage.length).toBe(2);
103      });
104      it('should NOT report scripts across navigations when enabled', async function({page, server}) {
105        await page.coverage.startJSCoverage(); // Enabled by default.
106        await page.goto(server.PREFIX + '/jscoverage/multiple.html');
107        await page.goto(server.EMPTY_PAGE);
108        const coverage = await page.coverage.stopJSCoverage();
109        expect(coverage.length).toBe(0);
110      });
111    });
112    // @see https://crbug.com/990945
113    xit('should not hang when there is a debugger statement', async function({page, server}) {
114      await page.coverage.startJSCoverage();
115      await page.goto(server.EMPTY_PAGE);
116      await page.evaluate(() => {
117        debugger; // eslint-disable-line no-debugger
118      });
119      await page.coverage.stopJSCoverage();
120    });
121  });
122
123  describe('CSSCoverage', function() {
124    it('should work', async function({page, server}) {
125      await page.coverage.startCSSCoverage();
126      await page.goto(server.PREFIX + '/csscoverage/simple.html');
127      const coverage = await page.coverage.stopCSSCoverage();
128      expect(coverage.length).toBe(1);
129      expect(coverage[0].url).toContain('/csscoverage/simple.html');
130      expect(coverage[0].ranges).toEqual([
131        {start: 1, end: 22}
132      ]);
133      const range = coverage[0].ranges[0];
134      expect(coverage[0].text.substring(range.start, range.end)).toBe('div { color: green; }');
135    });
136    it('should report sourceURLs', async function({page, server}) {
137      await page.coverage.startCSSCoverage();
138      await page.goto(server.PREFIX + '/csscoverage/sourceurl.html');
139      const coverage = await page.coverage.stopCSSCoverage();
140      expect(coverage.length).toBe(1);
141      expect(coverage[0].url).toBe('nicename.css');
142    });
143    it('should report multiple stylesheets', async function({page, server}) {
144      await page.coverage.startCSSCoverage();
145      await page.goto(server.PREFIX + '/csscoverage/multiple.html');
146      const coverage = await page.coverage.stopCSSCoverage();
147      expect(coverage.length).toBe(2);
148      coverage.sort((a, b) => a.url.localeCompare(b.url));
149      expect(coverage[0].url).toContain('/csscoverage/stylesheet1.css');
150      expect(coverage[1].url).toContain('/csscoverage/stylesheet2.css');
151    });
152    it('should report stylesheets that have no coverage', async function({page, server}) {
153      await page.coverage.startCSSCoverage();
154      await page.goto(server.PREFIX + '/csscoverage/unused.html');
155      const coverage = await page.coverage.stopCSSCoverage();
156      expect(coverage.length).toBe(1);
157      expect(coverage[0].url).toBe('unused.css');
158      expect(coverage[0].ranges.length).toBe(0);
159    });
160    it('should work with media queries', async function({page, server}) {
161      await page.coverage.startCSSCoverage();
162      await page.goto(server.PREFIX + '/csscoverage/media.html');
163      const coverage = await page.coverage.stopCSSCoverage();
164      expect(coverage.length).toBe(1);
165      expect(coverage[0].url).toContain('/csscoverage/media.html');
166      expect(coverage[0].ranges).toEqual([
167        {start: 17, end: 38}
168      ]);
169    });
170    it('should work with complicated usecases', async function({page, server}) {
171      await page.coverage.startCSSCoverage();
172      await page.goto(server.PREFIX + '/csscoverage/involved.html');
173      const coverage = await page.coverage.stopCSSCoverage();
174      expect(JSON.stringify(coverage, null, 2).replace(/:\d{4}\//g, ':<PORT>/')).toBeGolden('csscoverage-involved.txt');
175    });
176    it('should ignore injected stylesheets', async function({page, server}) {
177      await page.coverage.startCSSCoverage();
178      await page.addStyleTag({content: 'body { margin: 10px;}'});
179      // trigger style recalc
180      const margin = await page.evaluate(() => window.getComputedStyle(document.body).margin);
181      expect(margin).toBe('10px');
182      const coverage = await page.coverage.stopCSSCoverage();
183      expect(coverage.length).toBe(0);
184    });
185    describe('resetOnNavigation', function() {
186      it('should report stylesheets across navigations', async function({page, server}) {
187        await page.coverage.startCSSCoverage({resetOnNavigation: false});
188        await page.goto(server.PREFIX + '/csscoverage/multiple.html');
189        await page.goto(server.EMPTY_PAGE);
190        const coverage = await page.coverage.stopCSSCoverage();
191        expect(coverage.length).toBe(2);
192      });
193      it('should NOT report scripts across navigations', async function({page, server}) {
194        await page.coverage.startCSSCoverage(); // Enabled by default.
195        await page.goto(server.PREFIX + '/csscoverage/multiple.html');
196        await page.goto(server.EMPTY_PAGE);
197        const coverage = await page.coverage.stopCSSCoverage();
198        expect(coverage.length).toBe(0);
199      });
200    });
201    it('should work with a recently loaded stylesheet', async function({page, server}) {
202      await page.coverage.startCSSCoverage();
203      await page.evaluate(async url => {
204        document.body.textContent = 'hello, world';
205
206        const link = document.createElement('link');
207        link.rel = 'stylesheet';
208        link.href = url;
209        document.head.appendChild(link);
210        await new Promise(x => link.onload = x);
211      }, server.PREFIX + '/csscoverage/stylesheet1.css');
212      const coverage = await page.coverage.stopCSSCoverage();
213      expect(coverage.length).toBe(1);
214    });
215  });
216};
217
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Puppeteer on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)