mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
parent
04991ad025
commit
98bb2615ad
@ -1602,7 +1602,7 @@ await page.tracing.stop();
|
|||||||
|
|
||||||
#### tracing.start(options)
|
#### tracing.start(options)
|
||||||
- `options` <[Object]>
|
- `options` <[Object]>
|
||||||
- `path` <[string]> A path to write the trace file to. **required**
|
- `path` <[string]> A path to write the trace file to.
|
||||||
- `screenshots` <[boolean]> captures screenshots in the trace.
|
- `screenshots` <[boolean]> captures screenshots in the trace.
|
||||||
- `categories` <[Array]<[string]>> specify custom categories to use instead of default.
|
- `categories` <[Array]<[string]>> specify custom categories to use instead of default.
|
||||||
- returns: <[Promise]>
|
- returns: <[Promise]>
|
||||||
@ -1610,7 +1610,7 @@ await page.tracing.stop();
|
|||||||
Only one trace can be active at a time per browser.
|
Only one trace can be active at a time per browser.
|
||||||
|
|
||||||
#### tracing.stop()
|
#### tracing.stop()
|
||||||
- returns: <[Promise]>
|
- returns: <[Promise]<[Buffer]>> Promise which resolves to buffer with trace data.
|
||||||
|
|
||||||
### class: Dialog
|
### class: Dialog
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ class Tracing {
|
|||||||
*/
|
*/
|
||||||
async start(options) {
|
async start(options) {
|
||||||
console.assert(!this._recording, 'Cannot start recording trace while already recording trace.');
|
console.assert(!this._recording, 'Cannot start recording trace while already recording trace.');
|
||||||
console.assert(options.path, 'Must specify a path to write trace file to.');
|
|
||||||
|
|
||||||
const defaultCategories = [
|
const defaultCategories = [
|
||||||
'-*', 'devtools.timeline', 'v8.execute', 'disabled-by-default-devtools.timeline',
|
'-*', 'devtools.timeline', 'v8.execute', 'disabled-by-default-devtools.timeline',
|
||||||
@ -73,15 +72,26 @@ class Tracing {
|
|||||||
*/
|
*/
|
||||||
async _readStream(handle, path) {
|
async _readStream(handle, path) {
|
||||||
let eof = false;
|
let eof = false;
|
||||||
const file = await openAsync(path, 'w');
|
let file;
|
||||||
|
if (path)
|
||||||
|
file = await openAsync(path, 'w');
|
||||||
|
const bufs = [];
|
||||||
while (!eof) {
|
while (!eof) {
|
||||||
const response = await this._client.send('IO.read', {handle});
|
const response = await this._client.send('IO.read', {handle});
|
||||||
eof = response.eof;
|
eof = response.eof;
|
||||||
|
bufs.push(new Buffer(response.data));
|
||||||
if (path)
|
if (path)
|
||||||
await writeAsync(file, response.data);
|
await writeAsync(file, response.data);
|
||||||
}
|
}
|
||||||
|
if (path)
|
||||||
await closeAsync(file);
|
await closeAsync(file);
|
||||||
await this._client.send('IO.close', {handle});
|
await this._client.send('IO.close', {handle});
|
||||||
|
let resultBuffer = null;
|
||||||
|
try {
|
||||||
|
resultBuffer = Buffer.concat(bufs);
|
||||||
|
} finally {
|
||||||
|
return resultBuffer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
helper.tracePublicAPI(Tracing);
|
helper.tracePublicAPI(Tracing);
|
||||||
|
@ -27,8 +27,10 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
state.outputFile = path.join(__dirname, 'assets', `trace-${state.parallelIndex}.json`);
|
state.outputFile = path.join(__dirname, 'assets', `trace-${state.parallelIndex}.json`);
|
||||||
});
|
});
|
||||||
afterEach(function(state) {
|
afterEach(function(state) {
|
||||||
|
if (fs.existsSync(state.outputFile)) {
|
||||||
fs.unlinkSync(state.outputFile);
|
fs.unlinkSync(state.outputFile);
|
||||||
state.outputFile = null;
|
state.outputFile = null;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
it('should output a trace', async({page, server, outputFile}) => {
|
it('should output a trace', async({page, server, outputFile}) => {
|
||||||
await page.tracing.start({screenshots: true, path: outputFile});
|
await page.tracing.start({screenshots: true, path: outputFile});
|
||||||
@ -52,5 +54,29 @@ module.exports.addTests = function({testRunner, expect}) {
|
|||||||
expect(error).toBeTruthy();
|
expect(error).toBeTruthy();
|
||||||
await page.tracing.stop();
|
await page.tracing.stop();
|
||||||
});
|
});
|
||||||
|
it('should return a buffer', async({page, server, outputFile}) => {
|
||||||
|
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());
|
||||||
|
});
|
||||||
|
it('should return null in case of Buffer error', async({page, server}) => {
|
||||||
|
await page.tracing.start({screenshots: true});
|
||||||
|
await page.goto(server.PREFIX + '/grid.html');
|
||||||
|
const oldBufferConcat = Buffer.concat;
|
||||||
|
Buffer.concat = bufs => {
|
||||||
|
throw 'error';
|
||||||
|
};
|
||||||
|
const trace = await page.tracing.stop();
|
||||||
|
expect(trace).toEqual(null);
|
||||||
|
Buffer.concat = oldBufferConcat;
|
||||||
|
});
|
||||||
|
it('should support a buffer without a path', async({page, server}) => {
|
||||||
|
await page.tracing.start({screenshots: true});
|
||||||
|
await page.goto(server.PREFIX + '/grid.html');
|
||||||
|
const trace = await page.tracing.stop();
|
||||||
|
expect(trace.toString()).toContain('screenshot');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user