puppeteer/test/src/tracing.spec.ts

162 lines
4.6 KiB
TypeScript
Raw Normal View History

/**
2024-01-03 10:11:33 +00:00
* @license
* Copyright 2017 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'fs';
import path from 'path';
import expect from 'expect';
import * as utils from 'puppeteer-core/internal/common/util.js';
import sinon from 'sinon';
2023-06-21 19:41:09 +00:00
import {launch} from './mocha-utils.js';
describe('Tracing', function () {
let outputFile!: string;
2023-06-12 08:44:18 +00:00
let testState: Awaited<ReturnType<typeof launch>>;
/* we manually manage the browser here as we want a new browser for each
* individual test, which isn't the default behaviour of getTestState()
*/
2020-05-07 10:54:55 +00:00
beforeEach(async () => {
2023-06-21 19:41:09 +00:00
testState = await launch({});
outputFile = path.join(__dirname, 'trace.json');
});
2020-05-07 10:54:55 +00:00
afterEach(async () => {
2023-06-12 08:44:18 +00:00
await testState.close();
if (fs.existsSync(outputFile)) {
fs.unlinkSync(outputFile);
}
});
2020-05-07 10:54:55 +00:00
it('should output a trace', async () => {
2023-06-12 08:44:18 +00:00
const {server, page} = testState;
await page.tracing.start({screenshots: true, path: outputFile});
await page.goto(server.PREFIX + '/grid.html');
await page.tracing.stop();
expect(fs.existsSync(outputFile)).toBe(true);
});
2020-05-07 10:54:55 +00:00
it('should run with custom categories if provided', async () => {
2023-06-12 08:44:18 +00:00
const {page} = testState;
2020-05-07 10:54:55 +00:00
await page.tracing.start({
path: outputFile,
categories: ['-*', 'disabled-by-default-devtools.timeline.frame'],
2020-05-07 10:54:55 +00:00
});
await page.tracing.stop();
const traceJson = JSON.parse(
fs.readFileSync(outputFile, {encoding: 'utf8'})
);
const traceConfig = JSON.parse(traceJson.metadata['trace-config']);
expect(traceConfig.included_categories).toEqual([
'disabled-by-default-devtools.timeline.frame',
]);
expect(traceConfig.excluded_categories).toEqual(['*']);
expect(traceJson.traceEvents).not.toContainEqual(
expect.objectContaining({
cat: 'toplevel',
})
);
});
it('should run with default categories', async () => {
2023-06-12 08:44:18 +00:00
const {page} = testState;
await page.tracing.start({
path: outputFile,
});
await page.tracing.stop();
const traceJson = JSON.parse(
fs.readFileSync(outputFile, {encoding: 'utf8'})
);
expect(traceJson.traceEvents).toContainEqual(
expect.objectContaining({
cat: 'toplevel',
})
2020-05-07 10:54:55 +00:00
);
});
2020-05-07 10:54:55 +00:00
it('should throw if tracing on two pages', async () => {
2023-06-12 08:44:18 +00:00
const {page, browser} = testState;
await page.tracing.start({path: outputFile});
const newPage = await browser.newPage();
let error!: Error;
await newPage.tracing.start({path: outputFile}).catch(error_ => {
return (error = error_);
});
await newPage.close();
expect(error).toBeTruthy();
await page.tracing.stop();
});
2020-05-07 10:54:55 +00:00
it('should return a buffer', async () => {
2023-06-12 08:44:18 +00:00
const {page, server} = testState;
await page.tracing.start({screenshots: true, path: outputFile});
await page.goto(server.PREFIX + '/grid.html');
const trace = (await page.tracing.stop())!;
const buf = fs.readFileSync(outputFile);
expect(trace.toString()).toEqual(buf.toString());
});
2020-05-07 10:54:55 +00:00
it('should work without options', async () => {
2023-06-12 08:44:18 +00:00
const {page, server} = testState;
await page.tracing.start();
await page.goto(server.PREFIX + '/grid.html');
const trace = await page.tracing.stop();
expect(trace).toBeTruthy();
});
2022-05-31 14:34:16 +00:00
it('should return undefined in case of Buffer error', async () => {
2023-06-12 08:44:18 +00:00
const {page, server} = testState;
await page.tracing.start({screenshots: true});
await page.goto(server.PREFIX + '/grid.html');
const oldGetReadableAsBuffer = utils.getReadableAsBuffer;
sinon.stub(utils, 'getReadableAsBuffer').callsFake(() => {
return oldGetReadableAsBuffer({
getReader() {
return {
done: false,
read() {
if (!this.done) {
this.done = true;
return {done: false, value: 42};
}
return {done: true};
},
};
},
} as unknown as ReadableStream);
});
const trace = await page.tracing.stop();
expect(trace).toEqual(undefined);
});
2020-05-07 10:54:55 +00:00
it('should support a buffer without a path', async () => {
2023-06-12 08:44:18 +00:00
const {page, server} = testState;
await page.tracing.start({screenshots: true});
await page.goto(server.PREFIX + '/grid.html');
const trace = (await page.tracing.stop())!;
expect(trace.toString()).toContain('screenshot');
});
it('should properly fail if readProtocolStream errors out', async () => {
2023-06-12 08:44:18 +00:00
const {page} = testState;
await page.tracing.start({path: __dirname});
let error!: Error;
try {
await page.tracing.stop();
} catch (error_) {
error = error_ as Error;
}
expect(error).toBeDefined();
});
});