Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/playwright-core/src/client/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type * as channels from '@protocol/channels';

export class Tracing extends ChannelOwner<channels.TracingChannel> implements api.Tracing {
private _includeSources = false;
private _additionalSources = new Set<string>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clean it up in stop/stopChunk.

private _isLive = false;
_tracesDir: string | undefined;
private _stacksId: string | undefined;
Expand Down Expand Up @@ -58,6 +59,8 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
}

async group(name: string, options: { location?: { file: string, line?: number, column?: number } } = {}) {
if (options.location)
this._additionalSources.add(options.location.file);
await this._channel.tracingGroup({ name, location: options.location });
}

Expand Down Expand Up @@ -90,6 +93,9 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
private async _doStopChunk(filePath: string | undefined) {
this._resetStackCounter();

const additionalSources = [...this._additionalSources];
this._additionalSources.clear();

if (!filePath) {
// Not interested in artifacts.
await this._channel.tracingStopChunk({ mode: 'discard' });
Expand All @@ -106,7 +112,7 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap

if (isLocal) {
const result = await this._channel.tracingStopChunk({ mode: 'entries' });
await localUtils.zip({ zipFile: filePath, entries: result.entries!, mode: 'write', stacksId: this._stacksId, includeSources: this._includeSources });
await localUtils.zip({ zipFile: filePath, entries: result.entries!, mode: 'write', stacksId: this._stacksId, includeSources: this._includeSources, additionalSources });
return;
}

Expand All @@ -124,7 +130,7 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
await artifact.saveAs(filePath);
await artifact.delete();

await localUtils.zip({ zipFile: filePath, entries: [], mode: 'append', stacksId: this._stacksId, includeSources: this._includeSources });
await localUtils.zip({ zipFile: filePath, entries: [], mode: 'append', stacksId: this._stacksId, includeSources: this._includeSources, additionalSources });
}

_resetStackCounter() {
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ scheme.LocalUtilsZipParams = tObject({
stacksId: tOptional(tString),
mode: tEnum(['write', 'append']),
includeSources: tBoolean,
additionalSources: tOptional(tArray(tString)),
});
scheme.LocalUtilsZipResult = tOptional(tObject({}));
scheme.LocalUtilsHarOpenParams = tObject({
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/server/localUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ export async function zip(progress: Progress, stackSessions: Map<string, StackSe

// Collect sources from stacks.
if (params.includeSources) {
const sourceFiles = new Set<string>();
const sourceFiles = new Set<string>(params.additionalSources);
for (const { stack } of stackSession?.callStacks || []) {
if (!stack)
continue;
for (const { file } of stack)
sourceFiles.add(file);
}
for (const sourceFile of sourceFiles)
addFile(sourceFile, 'resources/src@' + await calculateSha1(sourceFile) + '.txt');
addFile(sourceFile, 'resources/src@' + calculateSha1(sourceFile) + '.txt');
}

if (params.mode === 'write') {
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/src/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,11 @@ export type LocalUtilsZipParams = {
stacksId?: string,
mode: 'write' | 'append',
includeSources: boolean,
additionalSources?: string[],
};
export type LocalUtilsZipOptions = {
stacksId?: string,
additionalSources?: string[],
};
export type LocalUtilsZipResult = void;
export type LocalUtilsHarOpenParams = {
Expand Down
3 changes: 3 additions & 0 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,9 @@ LocalUtils:
- write
- append
includeSources: boolean
additionalSources:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to say - make this one optional please.

type: array?
items: string

harOpen:
internal: true
Expand Down
14 changes: 14 additions & 0 deletions tests/library/trace-viewer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ test('should open trace viewer on specific host', async ({ showTraceViewer }, te

test('should show tracing.group in the action list with location', async ({ runAndTrace, page, context }) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36483' });
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/39302' });

const sourceFile = test.info().outputPath('source.js');
await fs.promises.writeFile(sourceFile, 'buddy beaver');

const traceViewer = await test.step('create trace with groups', async () => {
await page.context().tracing.group('ignored group');
Expand All @@ -125,15 +129,21 @@ test('should show tracing.group in the action list with location', async ({ runA
await context.tracing.group('inner group 2');
await expect(page.getByText('Hello')).toBeVisible();
await context.tracing.groupEnd();
await context.tracing.group('inner group 3', { location: { file: sourceFile, line: 1, column: 1 } });
await expect(page.getByText('Hello')).toBeVisible();
await context.tracing.groupEnd();
await context.tracing.groupEnd();
});
});

await fs.promises.rm(sourceFile);

await expect(traceViewer.actionTitles).toHaveText([
/outer group/,
/Navigate/,
/inner group 1 {{ eager_beaver }}/,
/inner group 2/,
/inner group 3/,
/toBeVisible/,
]);

Expand All @@ -145,12 +155,16 @@ test('should show tracing.group in the action list with location', async ({ runA
/inner group 1 {{ eager_beaver }}/,
/Click.*locator/,
/inner group 2/,
/inner group 3/,
]);
await traceViewer.showSourceTab();
await expect(traceViewer.sourceCodeTab.locator('.source-line-running')).toHaveText(/DO NOT TOUCH THIS LINE/);

await traceViewer.selectAction('inner group 2');
await expect(traceViewer.sourceCodeTab.locator('.source-line-running')).toContainText("await context.tracing.group('inner group 2');");

await traceViewer.selectAction('inner group 3');
await expect(traceViewer.sourceCodeTab.locator('.source-line-running')).toContainText('buddy beaver');
});

test('should open simple trace viewer', async ({ showTraceViewer }) => {
Expand Down
Loading