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';
|
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestPageAndContextHooks,
|
|
|
|
setupTestBrowserHooks,
|
2020-06-23 05:18:46 +00:00
|
|
|
describeChromeOnly,
|
|
|
|
} from './mocha-utils';
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Coverage specs', function () {
|
|
|
|
describeChromeOnly('JSCoverage', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = 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', {
|
|
|
|
waitUntil: 'networkidle0',
|
|
|
|
});
|
2018-03-29 17:42:23 +00:00
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toContain('/jscoverage/simple.html');
|
|
|
|
expect(coverage[0].ranges).toEqual([
|
2020-05-07 10:54:55 +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 () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toBe('nicename.js');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should ignore eval() scripts by default', async () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it("shouldn't ignore eval() scripts if reportAnonymousScripts is true", async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +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(
|
|
|
|
coverage.find((entry) => entry.url.startsWith('debugger://'))
|
|
|
|
).not.toBe(null);
|
2018-07-12 04:05:16 +00:00
|
|
|
expect(coverage.length).toBe(2);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should ignore pptr internal scripts if reportAnonymousScripts is true', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +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")');
|
|
|
|
await page.evaluate(() => console.log('bar'));
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(0);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report multiple scripts', async () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(2);
|
|
|
|
coverage.sort((a, b) => a.url.localeCompare(b.url));
|
|
|
|
expect(coverage[0].url).toContain('/jscoverage/script1.js');
|
|
|
|
expect(coverage[1].url).toContain('/jscoverage/script2.js');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report right ranges', async () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
const entry = coverage[0];
|
|
|
|
expect(entry.ranges.length).toBe(1);
|
|
|
|
const range = entry.ranges[0];
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(entry.text.substring(range.start, range.end)).toBe(
|
|
|
|
`console.log('used!');`
|
|
|
|
);
|
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 () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
const entry = coverage[0];
|
|
|
|
expect(entry.url).toContain('unused.html');
|
|
|
|
expect(entry.ranges.length).toBe(0);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with conditionals', async () => {
|
|
|
|
const { page, server } = 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(
|
|
|
|
JSON.stringify(coverage, null, 2).replace(/:\d{4}\//g, ':<PORT>/')
|
2020-06-23 05:18:46 +00:00
|
|
|
// TODO (@jackfranklin) extend the Expect typedefs to fix this
|
|
|
|
// @ts-expect-error toBeGolden matcher
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBeGolden('jscoverage-involved.txt');
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('resetOnNavigation', function () {
|
|
|
|
it('should report scripts across navigations when disabled', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.coverage.startJSCoverage({ resetOnNavigation: false });
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.goto(server.PREFIX + '/jscoverage/multiple.html');
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const coverage = await page.coverage.stopJSCoverage();
|
|
|
|
expect(coverage.length).toBe(2);
|
|
|
|
});
|
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 () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
2019-08-05 22:22:01 +00:00
|
|
|
// @see https://crbug.com/990945
|
2020-05-07 10:54:55 +00:00
|
|
|
xit('should not hang when there is a debugger statement', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-01-15 22:49:57 +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
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describeChromeOnly('CSSCoverage', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work', async () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toContain('/csscoverage/simple.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(coverage[0].ranges).toEqual([{ start: 1, end: 22 }]);
|
2018-03-29 17:42:23 +00:00
|
|
|
const range = coverage[0].ranges[0];
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(coverage[0].text.substring(range.start, range.end)).toBe(
|
|
|
|
'div { color: green; }'
|
|
|
|
);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report sourceURLs', async () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toBe('nicename.css');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report multiple stylesheets', async () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(2);
|
|
|
|
coverage.sort((a, b) => a.url.localeCompare(b.url));
|
|
|
|
expect(coverage[0].url).toContain('/csscoverage/stylesheet1.css');
|
|
|
|
expect(coverage[1].url).toContain('/csscoverage/stylesheet2.css');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report stylesheets that have no coverage', async () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toBe('unused.css');
|
|
|
|
expect(coverage[0].ranges.length).toBe(0);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with media queries', async () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
expect(coverage[0].url).toContain('/csscoverage/media.html');
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(coverage[0].ranges).toEqual([{ 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 () => {
|
|
|
|
const { page, server } = 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(
|
|
|
|
JSON.stringify(coverage, null, 2).replace(/:\d{4}\//g, ':<PORT>/')
|
2020-06-23 05:18:46 +00:00
|
|
|
// TODO (@jackfranklin) extend the Expect typedefs to fix this
|
|
|
|
// @ts-expect-error toBeGolden matcher
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBeGolden('csscoverage-involved.txt');
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should ignore injected stylesheets', async () => {
|
|
|
|
const { page } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.coverage.startCSSCoverage();
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.addStyleTag({ content: 'body { margin: 10px;}' });
|
2018-03-29 17:42:23 +00:00
|
|
|
// trigger style recalc
|
2020-05-07 10:54:55 +00:00
|
|
|
const margin = await page.evaluate(
|
|
|
|
() => window.getComputedStyle(document.body).margin
|
|
|
|
);
|
2018-03-29 17:42:23 +00:00
|
|
|
expect(margin).toBe('10px');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(0);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('resetOnNavigation', function () {
|
|
|
|
it('should report stylesheets across navigations', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +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();
|
|
|
|
expect(coverage.length).toBe(2);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should NOT report scripts across navigations', async () => {
|
|
|
|
const { page, server } = 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();
|
|
|
|
expect(coverage.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should work with a recently loaded stylesheet', async () => {
|
|
|
|
const { page, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-10-12 20:49:59 +00:00
|
|
|
await page.coverage.startCSSCoverage();
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.evaluate(async (url) => {
|
2018-10-12 20:49:59 +00:00
|
|
|
document.body.textContent = 'hello, world';
|
|
|
|
|
|
|
|
const link = document.createElement('link');
|
|
|
|
link.rel = 'stylesheet';
|
|
|
|
link.href = url;
|
|
|
|
document.head.appendChild(link);
|
2020-05-07 10:54:55 +00:00
|
|
|
await new Promise((x) => (link.onload = x));
|
2018-10-12 20:49:59 +00:00
|
|
|
}, server.PREFIX + '/csscoverage/stylesheet1.css');
|
|
|
|
const coverage = await page.coverage.stopCSSCoverage();
|
|
|
|
expect(coverage.length).toBe(1);
|
|
|
|
});
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|