Skip to content

Commit ff3199a

Browse files
authored
fix(trace): don't replace unknown braces in action title (#36920)
1 parent 0385672 commit ff3199a

File tree

5 files changed

+54
-40
lines changed

5 files changed

+54
-40
lines changed

packages/playwright-core/src/utils/isomorphic/protocolFormatter.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,51 @@
1616

1717
import { methodMetainfo } from './protocolMetainfo';
1818

19-
export function formatProtocolParam(params: Record<string, string> | undefined, name: string): string {
19+
export function formatProtocolParam(params: Record<string, string> | undefined, alternatives: string): string | undefined {
2020
if (!params)
21-
return '';
22-
if (name === 'url') {
23-
try {
24-
const urlObject = new URL(params[name]);
25-
if (urlObject.protocol === 'data:')
26-
return urlObject.protocol;
27-
if (urlObject.protocol === 'about:')
28-
return params[name];
29-
return urlObject.pathname + urlObject.search;
30-
} catch (error) {
31-
return params[name];
21+
return undefined;
22+
23+
for (const name of alternatives.split('|')) {
24+
if (name === 'url') {
25+
try {
26+
const urlObject = new URL(params[name]);
27+
if (urlObject.protocol === 'data:')
28+
return urlObject.protocol;
29+
if (urlObject.protocol === 'about:')
30+
return params[name];
31+
return urlObject.pathname + urlObject.search;
32+
} catch (error) {
33+
if (params[name] !== undefined)
34+
return params[name];
35+
}
3236
}
37+
if (name === 'timeNumber' && params[name] !== undefined) {
38+
// eslint-disable-next-line no-restricted-globals
39+
return new Date(params[name]).toString();
40+
}
41+
42+
const value = deepParam(params, name);
43+
if (value !== undefined)
44+
return value;
3345
}
34-
if (name === 'timeNumber') {
35-
// eslint-disable-next-line no-restricted-globals
36-
return new Date(params[name]).toString();
37-
}
38-
return deepParam(params, name);
3946
}
4047

41-
function deepParam(params: Record<string, any>, name: string): string {
48+
function deepParam(params: Record<string, any>, name: string): string | undefined {
4249
const tokens = name.split('.');
4350
let current = params;
4451
for (const token of tokens) {
4552
if (typeof current !== 'object' || current === null)
46-
return '';
53+
return undefined;
4754
current = current[token];
4855
}
4956
if (current === undefined)
50-
return '';
57+
return undefined;
5158
return String(current);
5259
}
5360

5461
export function renderTitleForCall(metadata: { title?: string, type: string, method: string, params: Record<string, string> | undefined }) {
5562
const titleFormat = metadata.title ?? methodMetainfo.get(metadata.type + '.' + metadata.method)?.title ?? metadata.method;
56-
return titleFormat.replace(/\{([^}]+)\}/g, (_, p1) => {
57-
return formatProtocolParam(metadata.params, p1);
63+
return titleFormat.replace(/\{([^}]+)\}/g, (fullMatch, p1) => {
64+
return formatProtocolParam(metadata.params, p1) ?? fullMatch;
5865
});
5966
}

packages/playwright-core/src/utils/isomorphic/protocolMetainfo.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ export const methodMetainfo = new Map<string, { internal?: boolean, title?: stri
9191
['BrowserContext.harExport', { internal: true, }],
9292
['BrowserContext.createTempFiles', { internal: true, }],
9393
['BrowserContext.updateSubscription', { internal: true, }],
94-
['BrowserContext.clockFastForward', { title: 'Fast forward clock "{ticksNumber}{ticksString}"', }],
95-
['BrowserContext.clockInstall', { title: 'Install clock "{timeNumber}{timeString}"', }],
96-
['BrowserContext.clockPauseAt', { title: 'Pause clock "{timeNumber}{timeString}"', }],
94+
['BrowserContext.clockFastForward', { title: 'Fast forward clock "{ticksNumber|ticksString}"', }],
95+
['BrowserContext.clockInstall', { title: 'Install clock "{timeNumber|timeString}"', }],
96+
['BrowserContext.clockPauseAt', { title: 'Pause clock "{timeNumber|timeString}"', }],
9797
['BrowserContext.clockResume', { title: 'Resume clock', }],
98-
['BrowserContext.clockRunFor', { title: 'Run clock "{ticksNumber}{ticksString}"', }],
99-
['BrowserContext.clockSetFixedTime', { title: 'Set fixed time "{timeNumber}{timeString}"', }],
100-
['BrowserContext.clockSetSystemTime', { title: 'Set system time "{timeNumber}{timeString}"', }],
98+
['BrowserContext.clockRunFor', { title: 'Run clock "{ticksNumber|ticksString}"', }],
99+
['BrowserContext.clockSetFixedTime', { title: 'Set fixed time "{timeNumber|timeString}"', }],
100+
['BrowserContext.clockSetSystemTime', { title: 'Set system time "{timeNumber|timeString}"', }],
101101
['BrowserContext.clockUninstall', { title: 'Uninstall clock', }],
102102
['Page.addInitScript', { }],
103103
['Page.close', { title: 'Close page', }],

packages/protocol/src/protocol.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,19 +1425,19 @@ BrowserContext:
14251425
enabled: boolean
14261426

14271427
clockFastForward:
1428-
title: Fast forward clock "{ticksNumber}{ticksString}"
1428+
title: Fast forward clock "{ticksNumber|ticksString}"
14291429
parameters:
14301430
ticksNumber: float?
14311431
ticksString: string?
14321432

14331433
clockInstall:
1434-
title: Install clock "{timeNumber}{timeString}"
1434+
title: Install clock "{timeNumber|timeString}"
14351435
parameters:
14361436
timeNumber: float?
14371437
timeString: string?
14381438

14391439
clockPauseAt:
1440-
title: Pause clock "{timeNumber}{timeString}"
1440+
title: Pause clock "{timeNumber|timeString}"
14411441
parameters:
14421442
timeNumber: float?
14431443
timeString: string?
@@ -1446,19 +1446,19 @@ BrowserContext:
14461446
title: Resume clock
14471447

14481448
clockRunFor:
1449-
title: Run clock "{ticksNumber}{ticksString}"
1449+
title: Run clock "{ticksNumber|ticksString}"
14501450
parameters:
14511451
ticksNumber: float?
14521452
ticksString: string?
14531453

14541454
clockSetFixedTime:
1455-
title: Set fixed time "{timeNumber}{timeString}"
1455+
title: Set fixed time "{timeNumber|timeString}"
14561456
parameters:
14571457
timeNumber: float?
14581458
timeString: string?
14591459

14601460
clockSetSystemTime:
1461-
title: Set system time "{timeNumber}{timeString}"
1461+
title: Set system time "{timeNumber|timeString}"
14621462
parameters:
14631463
timeNumber: float?
14641464
timeString: string?

packages/trace-viewer/src/ui/actionList.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,16 @@ export function renderTitleForCall(action: ActionTraceEvent): { elements: React.
164164
title.push(chunk);
165165

166166
const param = formatProtocolParam(action.params, quotedText);
167-
if (match.index === 0)
167+
if (param === undefined) {
168+
elements.push(fullMatch);
169+
title.push(fullMatch);
170+
} else if (match.index === 0) {
168171
elements.push(param);
169-
else
172+
title.push(param);
173+
} else {
170174
elements.push(<span className='action-title-param'>{param}</span>);
171-
title.push(param);
175+
title.push(param);
176+
}
172177
currentIndex = match.index + fullMatch.length;
173178
}
174179

tests/library/trace-viewer.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,14 @@ test('should open trace viewer on specific host', async ({ showTraceViewer }, te
110110
});
111111

112112
test('should show tracing.group in the action list with location', async ({ runAndTrace, page, context }) => {
113+
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36483' });
114+
113115
const traceViewer = await test.step('create trace with groups', async () => {
114116
await page.context().tracing.group('ignored group');
115117
return await runAndTrace(async () => {
116118
await context.tracing.group('outer group');
117119
await page.goto(`data:text/html,<!DOCTYPE html><body><div>Hello world</div></body>`);
118-
await context.tracing.group('inner group 1', { location: { file: __filename, line: 17, column: 1 } });
120+
await context.tracing.group('inner group 1 {{ eager_beaver }}', { location: { file: __filename, line: 17, column: 1 } });
119121
await page.locator('body').click();
120122
await context.tracing.groupEnd();
121123
await context.tracing.group('inner group 2');
@@ -128,7 +130,7 @@ test('should show tracing.group in the action list with location', async ({ runA
128130
await expect(traceViewer.actionTitles).toHaveText([
129131
/outer group/,
130132
/Navigate/,
131-
/inner group 1/,
133+
/inner group 1 {{ eager_beaver }}/,
132134
/inner group 2/,
133135
/toBeVisible/,
134136
]);
@@ -138,7 +140,7 @@ test('should show tracing.group in the action list with location', async ({ runA
138140
await expect(traceViewer.actionTitles).toHaveText([
139141
/outer group/,
140142
/Navigate/,
141-
/inner group 1/,
143+
/inner group 1 {{ eager_beaver }}/,
142144
/Click.*locator/,
143145
/inner group 2/,
144146
]);

0 commit comments

Comments
 (0)