Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

api(tracing): export -> stop({path}) (#6802)

Pavel Feldmand28f45b6ee4a
diff --git a/docs/src/api/class-tracing.md b/docs/src/api/class-tracing.md
index be5879c37a..a7a008d2dd 100644
--- a/docs/src/api/class-tracing.md
+++ b/docs/src/api/class-tracing.md
@@ -12,8 +12,7 @@ const context = await browser.newContext();
await context.tracing.start({ name: 'trace', screenshots: true, snapshots: true });
const page = await context.newPage();
await page.goto('https://playwright.dev');
-await context.tracing.stop();
-await context.tracing.export('trace.zip');
+await context.tracing.stop({ path: 'trace.zip' });
```
```java
@@ -25,8 +24,8 @@ context.tracing.start(page, new Tracing.StartOptions()
.setSnapshots(true);
Page page = context.newPage();
page.goto("https://playwright.dev");
-context.tracing.stop();
-context.tracing.export(Paths.get("trace.zip")))
+context.tracing.stop(new Tracing.StopOptions()
+ .setSaveAs(Paths.get("trace.zip")));
```
```python async
@@ -34,8 +33,7 @@ browser = await chromium.launch(traceDir='traces')
context = await browser.new_context()
await context.tracing.start(name="trace", screenshots=True, snapshots=True)
await page.goto("https://playwright.dev")
-await context.tracing.stop()
-await context.tracing.export("trace.zip")
+await context.tracing.stop(save_as = "trace.zip")
```
```python sync
@@ -43,20 +41,9 @@ browser = chromium.launch(traceDir='traces')
context = browser.new_context()
context.tracing.start(name="trace", screenshots=True, snapshots=True)
page.goto("https://playwright.dev")
-context.tracing.stop()
-context.tracing.export("trace.zip")
+context.tracing.stop(save_as = "trace.zip")
```
-## async method: Tracing.export
-
-Export trace into the file with the given name. Should be called after the
-tracing has stopped.
-
-### param: Tracing.export.path
-- `path` <[path]>
-
-File to save the trace into.
-
## async method: Tracing.start
Start tracing.
@@ -114,3 +101,8 @@ Whether to capture DOM snapshot on every action.
## async method: Tracing.stop
Stop tracing.
+
+### option: Tracing.stop.path
+- `path` <[path]>
+
+Export trace into the file with the given name.
diff --git a/src/client/tracing.ts b/src/client/tracing.ts
index 644ebbac6a..eb65a4c66b 100644
--- a/src/client/tracing.ts
+++ b/src/client/tracing.ts
@@ -32,20 +32,17 @@ export class Tracing implements api.Tracing {
});
}
- async stop() {
+ async stop(options: { path?: string } = {}) {
await this._context._wrapApiCall('tracing.stop', async (channel: channels.BrowserContextChannel) => {
await channel.tracingStop();
+ if (options.path) {
+ const result = await channel.tracingExport();
+ const artifact = Artifact.from(result.artifact);
+ if (this._context.browser()?._remoteType)
+ artifact._isRemote = true;
+ await artifact.saveAs(options.path);
+ await artifact.delete();
+ }
});
}
-
- async export(path: string): Promise<void> {
- const result = await this._context._wrapApiCall('tracing.export', async (channel: channels.BrowserContextChannel) => {
- return await channel.tracingExport();
- });
- const artifact = Artifact.from(result.artifact);
- if (this._context.browser()?._remoteType)
- artifact._isRemote = true;
- await artifact.saveAs(path);
- await artifact.delete();
- }
}
diff --git a/tests/tracing.spec.ts b/tests/tracing.spec.ts
index 47a2862e50..346c48309a 100644
--- a/tests/tracing.spec.ts
+++ b/tests/tracing.spec.ts
@@ -34,8 +34,7 @@ test('should collect trace', async ({ context, page, server, browserName }, test
await page.setContent('<button>Click</button>');
await page.click('"Click"');
await page.close();
- await context.tracing.stop();
- await context.tracing.export(testInfo.outputPath('trace.zip'));
+ await context.tracing.stop({ path: testInfo.outputPath('trace.zip') });
const { events } = await parseTrace(testInfo.outputPath('trace.zip'));
expect(events[0].type).toBe('context-options');
@@ -50,13 +49,12 @@ test('should collect trace', async ({ context, page, server, browserName }, test
});
test('should collect trace', async ({ context, page, server }, testInfo) => {
- await context.tracing.start({ name: 'test' });
+ await context.tracing.start();
await page.goto(server.EMPTY_PAGE);
await page.setContent('<button>Click</button>');
await page.click('"Click"');
await page.close();
- await context.tracing.stop();
- await context.tracing.export(testInfo.outputPath('trace.zip'));
+ await context.tracing.stop({ path: testInfo.outputPath('trace.zip') });
const { events } = await parseTrace(testInfo.outputPath('trace.zip'));
expect(events.some(e => e.type === 'frame-snapshot')).toBeFalsy();
@@ -67,11 +65,10 @@ test('should exclude internal pages', async ({ browserName, context, page, serve
test.fixme(true, 'https://github.com/microsoft/playwright/issues/6743');
await page.goto(server.EMPTY_PAGE);
- await context.tracing.start({ name: 'test' });
+ await context.tracing.start();
await context.storageState();
await page.close();
- await context.tracing.stop();
- await context.tracing.export(testInfo.outputPath('trace.zip'));
+ await context.tracing.stop({ path: testInfo.outputPath('trace.zip') });
const trace = await parseTrace(testInfo.outputPath('trace.zip'));
const pageIds = new Set();
@@ -84,18 +81,16 @@ test('should exclude internal pages', async ({ browserName, context, page, serve
});
test('should collect two traces', async ({ context, page, server }, testInfo) => {
- await context.tracing.start({ name: 'test1', screenshots: true, snapshots: true });
+ await context.tracing.start({ screenshots: true, snapshots: true });
await page.goto(server.EMPTY_PAGE);
await page.setContent('<button>Click</button>');
await page.click('"Click"');
- await context.tracing.stop();
- await context.tracing.export(testInfo.outputPath('trace1.zip'));
+ await context.tracing.stop({ path: testInfo.outputPath('trace1.zip') });
- await context.tracing.start({ name: 'test2', screenshots: true, snapshots: true });
+ await context.tracing.start({ screenshots: true, snapshots: true });
await page.dblclick('"Click"');
await page.close();
- await context.tracing.stop();
- await context.tracing.export(testInfo.outputPath('trace2.zip'));
+ await context.tracing.stop({ path: testInfo.outputPath('trace2.zip') });
{
const { events } = await parseTrace(testInfo.outputPath('trace1.zip'));
@@ -145,15 +140,14 @@ for (const params of [
const previewHeight = params.height * scale;
const context = await contextFactory({ viewport: { width: params.width, height: params.height }});
- await context.tracing.start({ name: 'test', screenshots: true, snapshots: true });
+ await context.tracing.start({ screenshots: true, snapshots: true });
const page = await context.newPage();
// Make sure we have a chance to paint.
for (let i = 0; i < 10; ++i) {
await page.setContent('<body style="box-sizing: border-box; width: 100%; height: 100%; margin:0; background: red; border: 50px solid blue"></body>');
await page.evaluate(() => new Promise(requestAnimationFrame));
}
- await context.tracing.stop();
- await context.tracing.export(testInfo.outputPath('trace.zip'));
+ await context.tracing.stop({ path: testInfo.outputPath('trace.zip') });
const { events, resources } = await parseTrace(testInfo.outputPath('trace.zip'));
const frames = events.filter(e => e.type === 'screencast-frame');
diff --git a/types/types.d.ts b/types/types.d.ts
index 8639414078..6bc6c0cab1 100644
--- a/types/types.d.ts
+++ b/types/types.d.ts
@@ -10609,18 +10609,11 @@ export interface Touchscreen {
* await context.tracing.start({ name: 'trace', screenshots: true, snapshots: true });
* const page = await context.newPage();
* await page.goto('https://playwright.dev');
- * await context.tracing.stop();
- * await context.tracing.export('trace.zip');
+ * await context.tracing.stop({ path: 'trace.zip' });
* ```
*
*/
export interface Tracing {
- /**
- * Export trace into the file with the given name. Should be called after the tracing has stopped.
- * @param path File to save the trace into.
- */
- export(path: string): Promise<void>;
-
/**
* Start tracing.
*
@@ -10654,8 +10647,14 @@ export interface Tracing {
/**
* Stop tracing.
+ * @param options
*/
- stop(): Promise<void>;
+ stop(options?: {
+ /**
+ * Export trace into the file with the given name.
+ */
+ path?: string;
+ }): Promise<void>;
}
/**