playwright/test/chromium/tracing.spec.js

94 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-11-19 05:18:28 +03:00
/**
* Copyright 2017 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.
*/
const fs = require('fs');
const path = require('path');
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, ASSETS_DIR}) {
2019-11-19 05:18:28 +03:00
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit, dit} = testRunner;
2019-11-19 05:18:28 +03:00
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
describe('Chromium.startTracing', function() {
2019-11-19 05:18:28 +03:00
beforeEach(async function(state) {
state.outputFile = path.join(ASSETS_DIR, `trace-${state.parallelIndex}.json`);
2019-11-19 05:18:28 +03:00
state.browser = await playwright.launch(defaultBrowserOptions);
state.page = await state.browser.defaultContext().newPage();
2019-11-19 05:18:28 +03:00
});
afterEach(async function(state) {
await state.browser.close();
state.browser = null;
state.page = null;
if (fs.existsSync(state.outputFile)) {
fs.unlinkSync(state.outputFile);
state.outputFile = null;
}
});
it('should output a trace', async({browser, page, server, outputFile}) => {
2019-12-20 01:51:49 +03:00
await browser.startTracing(page, {screenshots: true, path: outputFile});
2019-11-19 05:18:28 +03:00
await page.goto(server.PREFIX + '/grid.html');
2019-12-20 01:51:49 +03:00
await browser.stopTracing();
2019-11-19 05:18:28 +03:00
expect(fs.existsSync(outputFile)).toBe(true);
});
it('should run with custom categories if provided', async({browser, page, outputFile}) => {
2019-12-20 01:51:49 +03:00
await browser.startTracing(page, {path: outputFile, categories: ['disabled-by-default-v8.cpu_profiler.hires']});
await browser.stopTracing();
2019-11-19 05:18:28 +03:00
const traceJson = JSON.parse(fs.readFileSync(outputFile).toString());
expect(traceJson.metadata['trace-config']).toContain('disabled-by-default-v8.cpu_profiler.hires', 'Does not contain expected category');
2019-11-19 05:18:28 +03:00
});
it('should throw if tracing on two pages', async({browser, page, server, outputFile}) => {
2019-12-20 01:51:49 +03:00
await browser.startTracing(page, {path: outputFile});
const newPage = await browser.defaultContext().newPage();
2019-11-19 05:18:28 +03:00
let error = null;
2019-12-20 01:51:49 +03:00
await browser.startTracing(newPage, {path: outputFile}).catch(e => error = e);
2019-11-19 05:18:28 +03:00
await newPage.close();
expect(error).toBeTruthy();
2019-12-20 01:51:49 +03:00
await browser.stopTracing();
2019-11-19 05:18:28 +03:00
});
it('should return a buffer', async({browser, page, server, outputFile}) => {
2019-12-20 01:51:49 +03:00
await browser.startTracing(page, {screenshots: true, path: outputFile});
2019-11-19 05:18:28 +03:00
await page.goto(server.PREFIX + '/grid.html');
2019-12-20 01:51:49 +03:00
const trace = await browser.stopTracing();
2019-11-19 05:18:28 +03:00
const buf = fs.readFileSync(outputFile);
expect(trace.toString()).toEqual(buf.toString(), 'Tracing buffer mismatch');
2019-11-19 05:18:28 +03:00
});
it('should work without options', async({browser, page, server, outputFile}) => {
2019-12-20 01:51:49 +03:00
await browser.startTracing(page);
2019-11-19 05:18:28 +03:00
await page.goto(server.PREFIX + '/grid.html');
2019-12-20 01:51:49 +03:00
const trace = await browser.stopTracing();
2019-11-19 05:18:28 +03:00
expect(trace).toBeTruthy();
});
it('should return null in case of Buffer error', async({browser, page, server}) => {
2019-12-20 01:51:49 +03:00
await browser.startTracing(page, {screenshots: true});
2019-11-19 05:18:28 +03:00
await page.goto(server.PREFIX + '/grid.html');
const oldBufferConcat = Buffer.concat;
Buffer.concat = bufs => {
throw 'error';
};
2019-12-20 01:51:49 +03:00
const trace = await browser.stopTracing();
2019-11-19 05:18:28 +03:00
expect(trace).toEqual(null);
Buffer.concat = oldBufferConcat;
});
it('should support a buffer without a path', async({browser, page, server}) => {
2019-12-20 01:51:49 +03:00
await browser.startTracing(page, {screenshots: true});
2019-11-19 05:18:28 +03:00
await page.goto(server.PREFIX + '/grid.html');
2019-12-20 01:51:49 +03:00
const trace = await browser.stopTracing();
expect(trace.toString()).toContain('screenshot', 'Does not contain screenshot');
2019-11-19 05:18:28 +03:00
});
});
};