1/** 2 * Copyright 2017 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 17import fs from 'fs'; 18import path from 'path'; 19import expect from 'expect'; 20import { getTestState, describeChromeOnly } from './mocha-utils'; // eslint-disable-line import/extensions 21 22describeChromeOnly('Tracing', function () { 23 let outputFile; 24 let browser; 25 let page; 26 27 /* we manually manage the browser here as we want a new browser for each 28 * individual test, which isn't the default behaviour of getTestState() 29 */ 30 31 beforeEach(async () => { 32 const { defaultBrowserOptions, puppeteer } = getTestState(); 33 browser = await puppeteer.launch(defaultBrowserOptions); 34 page = await browser.newPage(); 35 outputFile = path.join(__dirname, 'assets', 'trace.json'); 36 }); 37 38 afterEach(async () => { 39 await browser.close(); 40 browser = null; 41 page = null; 42 if (fs.existsSync(outputFile)) { 43 fs.unlinkSync(outputFile); 44 outputFile = null; 45 } 46 }); 47 it('should output a trace', async () => { 48 const { server } = getTestState(); 49 50 await page.tracing.start({ screenshots: true, path: outputFile }); 51 await page.goto(server.PREFIX + '/grid.html'); 52 await page.tracing.stop(); 53 expect(fs.existsSync(outputFile)).toBe(true); 54 }); 55 56 it('should run with custom categories if provided', async () => { 57 await page.tracing.start({ 58 path: outputFile, 59 categories: ['disabled-by-default-v8.cpu_profiler.hires'], 60 }); 61 await page.tracing.stop(); 62 63 const traceJson = JSON.parse( 64 fs.readFileSync(outputFile, { encoding: 'utf8' }) 65 ); 66 expect(traceJson.metadata['trace-config']).toContain( 67 'disabled-by-default-v8.cpu_profiler.hires' 68 ); 69 }); 70 it('should throw if tracing on two pages', async () => { 71 await page.tracing.start({ path: outputFile }); 72 const newPage = await browser.newPage(); 73 let error = null; 74 await newPage.tracing 75 .start({ path: outputFile }) 76 .catch((error_) => (error = error_)); 77 await newPage.close(); 78 expect(error).toBeTruthy(); 79 await page.tracing.stop(); 80 }); 81 it('should return a buffer', async () => { 82 const { server } = getTestState(); 83 84 await page.tracing.start({ screenshots: true, path: outputFile }); 85 await page.goto(server.PREFIX + '/grid.html'); 86 const trace = await page.tracing.stop(); 87 const buf = fs.readFileSync(outputFile); 88 expect(trace.toString()).toEqual(buf.toString()); 89 }); 90 it('should work without options', async () => { 91 const { server } = getTestState(); 92 93 await page.tracing.start(); 94 await page.goto(server.PREFIX + '/grid.html'); 95 const trace = await page.tracing.stop(); 96 expect(trace).toBeTruthy(); 97 }); 98 99 it('should return null in case of Buffer error', async () => { 100 const { server } = getTestState(); 101 102 await page.tracing.start({ screenshots: true }); 103 await page.goto(server.PREFIX + '/grid.html'); 104 const oldBufferConcat = Buffer.concat; 105 Buffer.concat = () => { 106 throw 'error'; 107 }; 108 const trace = await page.tracing.stop(); 109 expect(trace).toEqual(null); 110 Buffer.concat = oldBufferConcat; 111 }); 112 113 it('should support a buffer without a path', async () => { 114 const { server } = getTestState(); 115 116 await page.tracing.start({ screenshots: true }); 117 await page.goto(server.PREFIX + '/grid.html'); 118 const trace = await page.tracing.stop(); 119 expect(trace.toString()).toContain('screenshot'); 120 }); 121 122 it('should properly fail if readProtocolStream errors out', async () => { 123 await page.tracing.start({ path: __dirname }); 124 125 let error: Error = null; 126 try { 127 await page.tracing.stop(); 128 } catch (error_) { 129 error = error_; 130 } 131 expect(error).toBeDefined(); 132 }); 133}); 134