2018-03-29 17:42:23 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 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.
|
|
|
|
*/
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-07-03 12:01:29 +00:00
|
|
|
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Coverage specs', function () {
|
2023-07-03 12:01:29 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('JSCoverage', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startJSCoverage();
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.goto(server.PREFIX + '/jscoverage/simple.html', {
|
2023-06-28 07:54:16 +00:00
|
|
|
waitUntil: 'load',
|
2020-05-07 10:54:55 +00:00
|
|
|
});
|
2018-03-29 17:42:23 +00:00
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(coverage[0]!.url).toContain('/jscoverage/simple.html');
|
|
|
|
expect(coverage[0]!.ranges).toEqual([
|
2022-06-22 13:25:44 +00:00
|
|
|
{start: 0, end: 17},
|
|
|
|
{start: 35, end: 61},
|
2018-03-29 17:42:23 +00:00
|
|
|
]);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report sourceURLs', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/sourceurl.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(coverage[0]!.url).toBe('nicename.js');
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should ignore eval() scripts by default', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-07-12 04:05:16 +00:00
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/eval.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2018-07-12 04:05:16 +00:00
|
|
|
});
|
2023-06-28 07:54:16 +00:00
|
|
|
it('should not ignore eval() scripts if reportAnonymousScripts is true', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.coverage.startJSCoverage({reportAnonymousScripts: true});
|
2018-07-12 04:05:16 +00:00
|
|
|
await page.goto(server.PREFIX + '/jscoverage/eval.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-22 13:25:44 +00:00
|
|
|
coverage.find(entry => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return entry.url.startsWith('debugger://');
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).not.toBe(null);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(2);
|
2018-07-12 04:05:16 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should ignore pptr internal scripts if reportAnonymousScripts is true', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.coverage.startJSCoverage({reportAnonymousScripts: true});
|
2018-07-12 04:05:16 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate('console.log("foo")');
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return console.log('bar');
|
|
|
|
});
|
2018-07-12 04:05:16 +00:00
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(0);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report multiple scripts', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/multiple.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(2);
|
2022-06-15 10:09:22 +00:00
|
|
|
coverage.sort((a, b) => {
|
|
|
|
return a.url.localeCompare(b.url);
|
|
|
|
});
|
|
|
|
expect(coverage[0]!.url).toContain('/jscoverage/script1.js');
|
|
|
|
expect(coverage[1]!.url).toContain('/jscoverage/script2.js');
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report right ranges', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/ranges.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
const entry = coverage[0]!;
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(entry.ranges).toHaveLength(2);
|
2022-09-07 07:10:07 +00:00
|
|
|
const range1 = entry.ranges[0]!;
|
|
|
|
expect(entry.text.substring(range1.start, range1.end)).toBe('\n');
|
|
|
|
const range2 = entry.ranges[1]!;
|
|
|
|
expect(entry.text.substring(range2.start, range2.end)).toBe(
|
2022-10-06 21:22:44 +00:00
|
|
|
`console.log('used!');if(true===false)`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('should report right ranges for "per function" scope', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2022-10-06 21:22:44 +00:00
|
|
|
|
|
|
|
const coverageOptions = {
|
|
|
|
useBlockCoverage: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
await page.coverage.startJSCoverage(coverageOptions);
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/ranges.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-10-06 21:22:44 +00:00
|
|
|
const entry = coverage[0]!;
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(entry.ranges).toHaveLength(2);
|
2022-10-06 21:22:44 +00:00
|
|
|
const range1 = entry.ranges[0]!;
|
|
|
|
expect(entry.text.substring(range1.start, range1.end)).toBe('\n');
|
|
|
|
const range2 = entry.ranges[1]!;
|
|
|
|
expect(entry.text.substring(range2.start, range2.end)).toBe(
|
|
|
|
`console.log('used!');if(true===false)console.log('unused!');`
|
2020-05-07 10:54:55 +00:00
|
|
|
);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report scripts that have no coverage', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/unused.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
const entry = coverage[0]!;
|
2018-03-29 17:42:23 +00:00
|
|
|
expect(entry.url).toContain('unused.html');
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(entry.ranges).toHaveLength(0);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with conditionals', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/involved.html');
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-07-05 12:42:55 +00:00
|
|
|
JSON.stringify(coverage, null, 2).replace(/:\d{4,5}\//g, ':<PORT>/')
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBeGolden('jscoverage-involved.txt');
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2021-05-25 07:14:44 +00:00
|
|
|
// @see https://crbug.com/990945
|
2022-09-09 09:12:18 +00:00
|
|
|
it.skip('should not hang when there is a debugger statement', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-05-25 07:14:44 +00:00
|
|
|
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => {
|
|
|
|
debugger; // eslint-disable-line no-debugger
|
|
|
|
});
|
|
|
|
await page.coverage.stopJSCoverage();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('resetOnNavigation', function () {
|
|
|
|
it('should report scripts across navigations when disabled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.coverage.startJSCoverage({resetOnNavigation: false});
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.goto(server.PREFIX + '/jscoverage/multiple.html');
|
2023-09-28 09:44:04 +00:00
|
|
|
// TODO: navigating too fast might loose JS coverage data in the browser.
|
|
|
|
await page.waitForNetworkIdle();
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(2);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should NOT report scripts across navigations when enabled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startJSCoverage(); // Enabled by default.
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/multiple.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(0);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
|
|
|
});
|
2021-09-15 18:54:24 +00:00
|
|
|
describe('includeRawScriptCoverage', function () {
|
|
|
|
it('should not include rawScriptCoverage field when disabled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-09-15 18:54:24 +00:00
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/simple.html', {
|
2023-06-28 07:54:16 +00:00
|
|
|
waitUntil: 'load',
|
2021-09-15 18:54:24 +00:00
|
|
|
});
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(coverage[0]!.rawScriptCoverage).toBeUndefined();
|
2021-09-15 18:54:24 +00:00
|
|
|
});
|
|
|
|
it('should include rawScriptCoverage field when enabled', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-09-15 18:54:24 +00:00
|
|
|
await page.coverage.startJSCoverage({
|
|
|
|
includeRawScriptCoverage: true,
|
|
|
|
});
|
|
|
|
await page.goto(server.PREFIX + '/jscoverage/simple.html', {
|
2023-06-28 07:54:16 +00:00
|
|
|
waitUntil: 'load',
|
2021-09-15 18:54:24 +00:00
|
|
|
});
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(coverage[0]!.rawScriptCoverage).toBeTruthy();
|
2021-09-15 18:54:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
// @see https://crbug.com/990945
|
2022-09-09 09:12:18 +00:00
|
|
|
it.skip('should not hang when there is a debugger statement', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-09-15 18:54:24 +00:00
|
|
|
|
|
|
|
await page.coverage.startJSCoverage();
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => {
|
|
|
|
debugger; // eslint-disable-line no-debugger
|
|
|
|
});
|
|
|
|
await page.coverage.stopJSCoverage();
|
|
|
|
});
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('CSSCoverage', function () {
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/simple.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(coverage[0]!.url).toContain('/csscoverage/simple.html');
|
2022-06-22 13:25:44 +00:00
|
|
|
expect(coverage[0]!.ranges).toEqual([{start: 1, end: 22}]);
|
2022-06-15 10:09:22 +00:00
|
|
|
const range = coverage[0]!.ranges[0]!;
|
|
|
|
expect(coverage[0]!.text.substring(range.start, range.end)).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
'div { color: green; }'
|
|
|
|
);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report sourceURLs', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/sourceurl.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(coverage[0]!.url).toBe('nicename.css');
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report multiple stylesheets', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/multiple.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(2);
|
2022-06-15 10:09:22 +00:00
|
|
|
coverage.sort((a, b) => {
|
|
|
|
return a.url.localeCompare(b.url);
|
|
|
|
});
|
|
|
|
expect(coverage[0]!.url).toContain('/csscoverage/stylesheet1.css');
|
|
|
|
expect(coverage[1]!.url).toContain('/csscoverage/stylesheet2.css');
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report stylesheets that have no coverage', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/unused.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(coverage[0]!.url).toBe('unused.css');
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage[0]!.ranges).toHaveLength(0);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with media queries', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/media.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(coverage[0]!.url).toContain('/csscoverage/media.html');
|
2023-07-19 08:11:45 +00:00
|
|
|
expect(coverage[0]!.ranges).toEqual([
|
|
|
|
{start: 8, end: 15},
|
|
|
|
{start: 17, end: 38},
|
|
|
|
]);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with complicated usecases', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/involved.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-07-05 12:42:55 +00:00
|
|
|
JSON.stringify(coverage, null, 2).replace(/:\d{4,5}\//g, ':<PORT>/')
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBeGolden('csscoverage-involved.txt');
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2022-06-24 12:17:16 +00:00
|
|
|
it('should work with empty stylesheets', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2022-06-24 12:17:16 +00:00
|
|
|
|
|
|
|
await page.coverage.startCSSCoverage();
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/empty.html');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2022-06-24 12:17:16 +00:00
|
|
|
expect(coverage[0]!.text).toEqual('');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should ignore injected stylesheets', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startCSSCoverage();
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.addStyleTag({content: 'body { margin: 10px;}'});
|
2018-03-29 17:42:23 +00:00
|
|
|
// trigger style recalc
|
2022-06-15 10:09:22 +00:00
|
|
|
const margin = await page.evaluate(() => {
|
|
|
|
return window.getComputedStyle(document.body).margin;
|
|
|
|
});
|
2018-03-29 17:42:23 +00:00
|
|
|
expect(margin).toBe('10px');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(0);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2021-04-06 09:30:55 +00:00
|
|
|
it('should work with a recently loaded stylesheet', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2021-04-06 09:30:55 +00:00
|
|
|
|
|
|
|
await page.coverage.startCSSCoverage();
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
await page.evaluate(async url => {
|
2021-04-06 09:30:55 +00:00
|
|
|
document.body.textContent = 'hello, world';
|
|
|
|
|
|
|
|
const link = document.createElement('link');
|
|
|
|
link.rel = 'stylesheet';
|
|
|
|
link.href = url;
|
|
|
|
document.head.appendChild(link);
|
2022-06-22 13:25:44 +00:00
|
|
|
await new Promise(x => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (link.onload = x);
|
|
|
|
});
|
2021-04-06 09:30:55 +00:00
|
|
|
}, server.PREFIX + '/csscoverage/stylesheet1.css');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(1);
|
2021-04-06 09:30:55 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('resetOnNavigation', function () {
|
|
|
|
it('should report stylesheets across navigations', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.coverage.startCSSCoverage({resetOnNavigation: false});
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.goto(server.PREFIX + '/csscoverage/multiple.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(2);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should NOT report scripts across navigations', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startCSSCoverage(); // Enabled by default.
|
|
|
|
await page.goto(server.PREFIX + '/csscoverage/multiple.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(coverage).toHaveLength(0);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|