Skip to content

Commit ccaac10

Browse files
authored
fix(tracing): include source for tracing.group(location) (#39307)
1 parent 6ba0459 commit ccaac10

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

packages/playwright-core/src/client/tracing.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type * as channels from '@protocol/channels';
2222

2323
export class Tracing extends ChannelOwner<channels.TracingChannel> implements api.Tracing {
2424
private _includeSources = false;
25+
private _additionalSources = new Set<string>();
2526
private _isLive = false;
2627
_tracesDir: string | undefined;
2728
private _stacksId: string | undefined;
@@ -58,6 +59,8 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
5859
}
5960

6061
async group(name: string, options: { location?: { file: string, line?: number, column?: number } } = {}) {
62+
if (options.location)
63+
this._additionalSources.add(options.location.file);
6164
await this._channel.tracingGroup({ name, location: options.location });
6265
}
6366

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

96+
const additionalSources = [...this._additionalSources];
97+
this._additionalSources.clear();
98+
9399
if (!filePath) {
94100
// Not interested in artifacts.
95101
await this._channel.tracingStopChunk({ mode: 'discard' });
@@ -106,7 +112,7 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
106112

107113
if (isLocal) {
108114
const result = await this._channel.tracingStopChunk({ mode: 'entries' });
109-
await localUtils.zip({ zipFile: filePath, entries: result.entries!, mode: 'write', stacksId: this._stacksId, includeSources: this._includeSources });
115+
await localUtils.zip({ zipFile: filePath, entries: result.entries!, mode: 'write', stacksId: this._stacksId, includeSources: this._includeSources, additionalSources });
110116
return;
111117
}
112118

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

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

130136
_resetStackCounter() {

packages/playwright-core/src/protocol/validator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ scheme.LocalUtilsZipParams = tObject({
278278
stacksId: tOptional(tString),
279279
mode: tEnum(['write', 'append']),
280280
includeSources: tBoolean,
281+
additionalSources: tOptional(tArray(tString)),
281282
});
282283
scheme.LocalUtilsZipResult = tOptional(tObject({}));
283284
scheme.LocalUtilsHarOpenParams = tObject({

packages/playwright-core/src/server/localUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ export async function zip(progress: Progress, stackSessions: Map<string, StackSe
6767

6868
// Collect sources from stacks.
6969
if (params.includeSources) {
70-
const sourceFiles = new Set<string>();
70+
const sourceFiles = new Set<string>(params.additionalSources);
7171
for (const { stack } of stackSession?.callStacks || []) {
7272
if (!stack)
7373
continue;
7474
for (const { file } of stack)
7575
sourceFiles.add(file);
7676
}
7777
for (const sourceFile of sourceFiles)
78-
addFile(sourceFile, 'resources/src@' + await calculateSha1(sourceFile) + '.txt');
78+
addFile(sourceFile, 'resources/src@' + calculateSha1(sourceFile) + '.txt');
7979
}
8080

8181
if (params.mode === 'write') {

packages/protocol/src/channels.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,11 @@ export type LocalUtilsZipParams = {
477477
stacksId?: string,
478478
mode: 'write' | 'append',
479479
includeSources: boolean,
480+
additionalSources?: string[],
480481
};
481482
export type LocalUtilsZipOptions = {
482483
stacksId?: string,
484+
additionalSources?: string[],
483485
};
484486
export type LocalUtilsZipResult = void;
485487
export type LocalUtilsHarOpenParams = {

packages/protocol/src/protocol.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,9 @@ LocalUtils:
652652
- write
653653
- append
654654
includeSources: boolean
655+
additionalSources:
656+
type: array?
657+
items: string
655658

656659
harOpen:
657660
internal: true

tests/library/trace-viewer.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ test('should open trace viewer on specific host', async ({ showTraceViewer }, te
113113

114114
test('should show tracing.group in the action list with location', async ({ runAndTrace, page, context }) => {
115115
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36483' });
116+
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/39302' });
117+
118+
const sourceFile = test.info().outputPath('source.js');
119+
await fs.promises.writeFile(sourceFile, 'buddy beaver');
116120

117121
const traceViewer = await test.step('create trace with groups', async () => {
118122
await page.context().tracing.group('ignored group');
@@ -125,15 +129,21 @@ test('should show tracing.group in the action list with location', async ({ runA
125129
await context.tracing.group('inner group 2');
126130
await expect(page.getByText('Hello')).toBeVisible();
127131
await context.tracing.groupEnd();
132+
await context.tracing.group('inner group 3', { location: { file: sourceFile, line: 1, column: 1 } });
133+
await expect(page.getByText('Hello')).toBeVisible();
134+
await context.tracing.groupEnd();
128135
await context.tracing.groupEnd();
129136
});
130137
});
131138

139+
await fs.promises.rm(sourceFile);
140+
132141
await expect(traceViewer.actionTitles).toHaveText([
133142
/outer group/,
134143
/Navigate/,
135144
/inner group 1 {{ eager_beaver }}/,
136145
/inner group 2/,
146+
/inner group 3/,
137147
/toBeVisible/,
138148
]);
139149

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

152163
await traceViewer.selectAction('inner group 2');
153164
await expect(traceViewer.sourceCodeTab.locator('.source-line-running')).toContainText("await context.tracing.group('inner group 2');");
165+
166+
await traceViewer.selectAction('inner group 3');
167+
await expect(traceViewer.sourceCodeTab.locator('.source-line-running')).toContainText('buddy beaver');
154168
});
155169

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

0 commit comments

Comments
 (0)