Skip to content

Commit 2c707de

Browse files
committed
Rewrote to use lower fidelity but easier URL updating mechanism
1 parent c6644f9 commit 2c707de

File tree

6 files changed

+57
-64
lines changed

6 files changed

+57
-64
lines changed

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import type * as actions from '@recorder/actions';
3232
import { stringifySelector } from '../utils/isomorphic/selectorParser';
3333
import type { Frame } from './frames';
3434
import type { AriaTemplateNode } from '@isomorphic/ariaSnapshot';
35-
import { Page } from './page';
35+
import type { Page } from './page';
3636

3737
const recorderSymbol = Symbol('recorderSymbol');
3838

@@ -150,30 +150,6 @@ export class Recorder implements InstrumentationListener, IRecorder {
150150
this._recorderApp?.close().catch(() => {});
151151
});
152152

153-
// Report the URL of the first page in the context
154-
let primaryPage: Page | undefined;
155-
const registerNavigationListener = (page: Page) => {
156-
if (primaryPage === undefined) {
157-
primaryPage = page;
158-
recorderApp.setBasePageURL(page.mainFrame().url());
159-
}
160-
161-
page.on(Page.Events.InternalFrameNavigatedToNewDocument, (frame: Frame) => {
162-
if (page === primaryPage && page.mainFrame() === frame)
163-
recorderApp.setBasePageURL(frame.url());
164-
});
165-
page.on(Page.Events.Close, () => {
166-
if (page === primaryPage) {
167-
primaryPage = this._context.pages()[0];
168-
if (primaryPage)
169-
recorderApp.setBasePageURL(primaryPage.mainFrame().url());
170-
}
171-
});
172-
};
173-
for (const page of this._context.pages())
174-
registerNavigationListener(page);
175-
this._context.on(BrowserContext.Events.Page, registerNavigationListener);
176-
177153
this._contextRecorder.on(ContextRecorder.Events.Change, (data: { sources: Source[], actions: actions.ActionInContext[] }) => {
178154
this._recorderSources = data.sources;
179155
recorderApp.setActions(data.actions, data.sources);
@@ -372,7 +348,8 @@ export class Recorder implements InstrumentationListener, IRecorder {
372348
}
373349

374350
private _pushAllSources() {
375-
this._recorderApp?.setSources([...this._recorderSources, ...this._userSources.values()]);
351+
const primaryPage: Page | undefined = this._context.pages()[0];
352+
this._recorderApp?.setSources([...this._recorderSources, ...this._userSources.values()], primaryPage?.mainFrame().url());
376353
}
377354

378355
async onBeforeInputAction(sdkObject: SdkObject, metadata: CallMetadata) {

packages/playwright-core/src/server/recorder/recorderApp.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ export class EmptyRecorderApp extends EventEmitter implements IRecorderApp {
3737
async setRunningFile(file: string | undefined): Promise<void> {}
3838
async elementPicked(elementInfo: ElementInfo, userGesture?: boolean): Promise<void> {}
3939
async updateCallLogs(callLogs: CallLog[]): Promise<void> {}
40-
async setSources(sources: Source[]): Promise<void> {}
40+
async setSources(sources: Source[], primaryPageURL: string | undefined): Promise<void> {}
4141
async setActions(actions: actions.ActionInContext[], sources: Source[]): Promise<void> {}
42-
async setBasePageURL(url: string): Promise<void> {}
4342
}
4443

4544
export class RecorderApp extends EventEmitter implements IRecorderApp {
@@ -144,10 +143,10 @@ export class RecorderApp extends EventEmitter implements IRecorderApp {
144143
}).toString(), { isFunction: true }, paused).catch(() => {});
145144
}
146145

147-
async setSources(sources: Source[]): Promise<void> {
148-
await this._page.mainFrame().evaluateExpression(((sources: Source[]) => {
149-
window.playwrightSetSources(sources);
150-
}).toString(), { isFunction: true }, sources).catch(() => {});
146+
async setSources(sources: Source[], primaryPageURL: string | undefined): Promise<void> {
147+
await this._page.mainFrame().evaluateExpression((({ sources, primaryPageURL }: { sources: Source[], primaryPageURL: string | undefined }) => {
148+
window.playwrightSetSources(sources, primaryPageURL);
149+
}).toString(), { isFunction: true }, { sources, primaryPageURL }).catch(() => {});
151150

152151
// Testing harness for runCLI mode.
153152
if (process.env.PWTEST_CLI_IS_UNDER_TEST && sources.length) {
@@ -159,12 +158,6 @@ export class RecorderApp extends EventEmitter implements IRecorderApp {
159158
async setActions(actions: actions.ActionInContext[], sources: Source[]): Promise<void> {
160159
}
161160

162-
async setBasePageURL(url: string): Promise<void> {
163-
await this._page.mainFrame().evaluateExpression(((url: string) => {
164-
window.playwrightSetBasePageURL(url);
165-
}).toString(), { isFunction: true }, url).catch(() => {});
166-
}
167-
168161
async elementPicked(elementInfo: ElementInfo, userGesture?: boolean): Promise<void> {
169162
if (userGesture)
170163
this._page.bringToFront();

packages/playwright-core/src/server/recorder/recorderFrontend.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ export interface IRecorderApp extends EventEmitter {
3232
setRunningFile(file: string | undefined): Promise<void>;
3333
elementPicked(elementInfo: ElementInfo, userGesture?: boolean): Promise<void>;
3434
updateCallLogs(callLogs: CallLog[]): Promise<void>;
35-
setSources(sources: Source[]): Promise<void>;
35+
setSources(sources: Source[], primaryPageURL: string | undefined): Promise<void>;
3636
setActions(actions: actions.ActionInContext[], sources: Source[]): Promise<void>;
37-
setBasePageURL(url: string): Promise<void>;
3837
}
3938

4039
export type IRecorderAppFactory = (recorder: IRecorder) => Promise<IRecorderApp>;

packages/recorder/src/main.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ import * as React from 'react';
1919
import { Recorder } from './recorder';
2020
import './recorder.css';
2121

22-
export const Main: React.FC = ({
23-
}) => {
22+
export const Main: React.FC = ({}) => {
2423
const [sources, setSources] = React.useState<Source[]>([]);
2524
const [paused, setPaused] = React.useState(false);
2625
const [log, setLog] = React.useState(new Map<string, CallLog>());
2726
const [mode, setMode] = React.useState<Mode>('none');
2827

2928
React.useLayoutEffect(() => {
3029
window.playwrightSetMode = setMode;
31-
window.playwrightSetSources = (sources: Source[]) => {
30+
window.playwrightSetSources = (sources, primaryPageURL) => {
3231
setSources(sources);
3332
window.playwrightSourcesEchoForTest = sources;
33+
document.title = primaryPageURL
34+
? `Playwright Inspector - ${primaryPageURL}`
35+
: `Playwright Inspector`;
3436
};
3537
window.playwrightSetPaused = setPaused;
3638
window.playwrightUpdateLogs = callLogs => {
@@ -43,14 +45,7 @@ export const Main: React.FC = ({
4345
return newLog;
4446
});
4547
};
46-
window.playwrightSetBasePageURL = (url: string) => {
47-
if (url)
48-
document.title = `Playwright Inspector - ${url}`;
49-
else
50-
document.title = `Playwright Inspector`;
51-
};
5248
}, []);
5349

54-
5550
return <Recorder sources={sources} paused={paused} log={log} mode={mode} />;
5651
};

packages/recorder/src/recorderTypes.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ declare global {
101101
interface Window {
102102
playwrightSetMode: (mode: Mode) => void;
103103
playwrightSetPaused: (paused: boolean) => void;
104-
playwrightSetSources: (sources: Source[]) => void;
104+
playwrightSetSources: (sources: Source[], primaryPageURL: string | undefined) => void;
105105
playwrightSetOverlayVisible: (visible: boolean) => void;
106-
playwrightSetBasePageURL: (url: string) => void;
107106
playwrightUpdateLogs: (callLogs: CallLog[]) => void;
108107
playwrightSetRunningFile: (file: string | undefined) => void;
109108
playwrightElementPicked: (elementInfo: ElementInfo, userGesture?: boolean) => void;

tests/library/inspector/title.spec.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,68 @@
1616

1717
import { test, expect } from './inspectorTest';
1818

19-
test('should reflect formatted URL of the page', async ({ openRecorder, server }) => {
19+
test('should reflect formatted URL of the page', async ({
20+
openRecorder,
21+
server,
22+
}) => {
2023
const { recorder } = await openRecorder();
2124
await recorder.setContentAndWait('');
22-
await expect(await recorder.recorderPage.title()).toBe('Playwright Inspector - about:blank');
25+
await expect(recorder.recorderPage).toHaveTitle(
26+
'Playwright Inspector - about:blank',
27+
);
2328

2429
await recorder.setContentAndWait('', server.EMPTY_PAGE);
25-
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.EMPTY_PAGE}`);
30+
await expect(recorder.recorderPage).toHaveTitle(
31+
`Playwright Inspector - ${server.EMPTY_PAGE}`,
32+
);
2633
});
2734

28-
test('should update primary page URL when original primary closes', async ({ context, openRecorder, server }) => {
35+
test('should update primary page URL when original primary closes', async ({
36+
context,
37+
openRecorder,
38+
server,
39+
}) => {
2940
const { recorder } = await openRecorder();
30-
await recorder.setContentAndWait('', `${server.PREFIX}/background-color.html`);
31-
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);
41+
await recorder.setContentAndWait(
42+
'',
43+
`${server.PREFIX}/background-color.html`,
44+
);
45+
await expect(recorder.recorderPage).toHaveTitle(
46+
`Playwright Inspector - ${server.PREFIX}/background-color.html`,
47+
);
3248

3349
const page2 = await context.newPage();
3450
await page2.goto(`${server.PREFIX}/empty.html`);
35-
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);
51+
await expect(recorder.recorderPage).toHaveTitle(
52+
`Playwright Inspector - ${server.PREFIX}/background-color.html`,
53+
);
3654

3755
const page3 = await context.newPage();
3856
await page3.goto(`${server.PREFIX}/dom.html`);
39-
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);
57+
await expect(recorder.recorderPage).toHaveTitle(
58+
`Playwright Inspector - ${server.PREFIX}/background-color.html`,
59+
);
4060

4161
const page4 = await context.newPage();
4262
await page4.goto(`${server.PREFIX}/grid.html`);
43-
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);
63+
await expect(recorder.recorderPage).toHaveTitle(
64+
`Playwright Inspector - ${server.PREFIX}/background-color.html`,
65+
);
4466

4567
await page2.close();
46-
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);
68+
await expect(recorder.recorderPage).toHaveTitle(
69+
`Playwright Inspector - ${server.PREFIX}/background-color.html`,
70+
);
4771

4872
await recorder.page.close();
49-
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/dom.html`);
73+
// URL will not update without performing some action
74+
await page3.getByRole('checkbox').click();
75+
await expect(recorder.recorderPage).toHaveTitle(
76+
`Playwright Inspector - ${server.PREFIX}/dom.html`,
77+
);
5078

5179
await page3.close();
52-
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/grid.html`);
80+
await expect(recorder.recorderPage).toHaveTitle(
81+
`Playwright Inspector - ${server.PREFIX}/grid.html`,
82+
);
5383
});

0 commit comments

Comments
 (0)