Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions packages/playwright-core/src/server/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type * as actions from '@recorder/actions';
import { stringifySelector } from '../utils/isomorphic/selectorParser';
import type { Frame } from './frames';
import type { AriaTemplateNode } from '@isomorphic/ariaSnapshot';
import { Page } from './page';

const recorderSymbol = Symbol('recorderSymbol');

Expand Down Expand Up @@ -148,6 +149,31 @@ export class Recorder implements InstrumentationListener, IRecorder {
this._context.instrumentation.removeListener(this);
this._recorderApp?.close().catch(() => {});
});

// Report the URL of the first page in the context
let primaryPage: Page | undefined;
const registerNavigationListener = (page: Page) => {
if (primaryPage === undefined) {
primaryPage = page;
recorderApp.setBasePageURL(page.mainFrame().url());
}

page.on(Page.Events.InternalFrameNavigatedToNewDocument, (frame: Frame) => {
if (page === primaryPage && page.mainFrame() === frame)
recorderApp.setBasePageURL(frame.url());
});
page.on(Page.Events.Close, () => {
if (page === primaryPage) {
primaryPage = this._context.pages()[0];
if (primaryPage)
recorderApp.setBasePageURL(primaryPage.mainFrame().url());
}
});
};
for (const page of this._context.pages())
registerNavigationListener(page);
this._context.on(BrowserContext.Events.Page, registerNavigationListener);

this._contextRecorder.on(ContextRecorder.Events.Change, (data: { sources: Source[], actions: actions.ActionInContext[] }) => {
this._recorderSources = data.sources;
recorderApp.setActions(data.actions, data.sources);
Expand Down
7 changes: 7 additions & 0 deletions packages/playwright-core/src/server/recorder/recorderApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class EmptyRecorderApp extends EventEmitter implements IRecorderApp {
async updateCallLogs(callLogs: CallLog[]): Promise<void> {}
async setSources(sources: Source[]): Promise<void> {}
async setActions(actions: actions.ActionInContext[], sources: Source[]): Promise<void> {}
async setBasePageURL(url: string): Promise<void> {}
}

export class RecorderApp extends EventEmitter implements IRecorderApp {
Expand Down Expand Up @@ -158,6 +159,12 @@ export class RecorderApp extends EventEmitter implements IRecorderApp {
async setActions(actions: actions.ActionInContext[], sources: Source[]): Promise<void> {
}

async setBasePageURL(url: string): Promise<void> {
await this._page.mainFrame().evaluateExpression(((url: string) => {
window.playwrightSetBasePageURL(url);
}).toString(), { isFunction: true }, url).catch(() => {});
}

async elementPicked(elementInfo: ElementInfo, userGesture?: boolean): Promise<void> {
if (userGesture)
this._page.bringToFront();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface IRecorderApp extends EventEmitter {
updateCallLogs(callLogs: CallLog[]): Promise<void>;
setSources(sources: Source[]): Promise<void>;
setActions(actions: actions.ActionInContext[], sources: Source[]): Promise<void>;
setBasePageURL(url: string): Promise<void>;
}

export type IRecorderAppFactory = (recorder: IRecorder) => Promise<IRecorderApp>;
39 changes: 24 additions & 15 deletions packages/recorder/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,31 @@ export const Main: React.FC = ({
const [log, setLog] = React.useState(new Map<string, CallLog>());
const [mode, setMode] = React.useState<Mode>('none');

window.playwrightSetMode = setMode;
window.playwrightSetSources = React.useCallback((sources: Source[]) => {
setSources(sources);
window.playwrightSourcesEchoForTest = sources;
React.useLayoutEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

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

why blocking effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the way these side effects are supposed to be triggered in React (though ideally it would be useEffect, but I figured people would complain about it potentially being delayed too long). Prior to this change, they were being set on each render of the main component (which happens quite often since it receives the data from the bridge). Unnecessarily inefficient, and for no gain.

window.playwrightSetMode = setMode;
window.playwrightSetSources = (sources: Source[]) => {
setSources(sources);
window.playwrightSourcesEchoForTest = sources;
};
window.playwrightSetPaused = setPaused;
window.playwrightUpdateLogs = callLogs => {
setLog(log => {
const newLog = new Map<string, CallLog>(log);
for (const callLog of callLogs) {
callLog.reveal = !log.has(callLog.id);
newLog.set(callLog.id, callLog);
}
return newLog;
});
};
window.playwrightSetBasePageURL = (url: string) => {
if (url)
document.title = `Playwright Inspector - ${url}`;
else
document.title = `Playwright Inspector`;
};
}, []);
window.playwrightSetPaused = setPaused;
window.playwrightUpdateLogs = callLogs => {
setLog(log => {
const newLog = new Map<string, CallLog>(log);
for (const callLog of callLogs) {
callLog.reveal = !log.has(callLog.id);
newLog.set(callLog.id, callLog);
}
return newLog;
});
};


return <Recorder sources={sources} paused={paused} log={log} mode={mode} />;
};
1 change: 1 addition & 0 deletions packages/recorder/src/recorderTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ declare global {
playwrightSetPaused: (paused: boolean) => void;
playwrightSetSources: (sources: Source[]) => void;
playwrightSetOverlayVisible: (visible: boolean) => void;
playwrightSetBasePageURL: (url: string) => void;
playwrightUpdateLogs: (callLogs: CallLog[]) => void;
playwrightSetRunningFile: (file: string | undefined) => void;
playwrightElementPicked: (elementInfo: ElementInfo, userGesture?: boolean) => void;
Expand Down
53 changes: 53 additions & 0 deletions tests/library/inspector/title.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

test('should reflect formatted URL of the page', async ({ openRecorder, server }) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if a new file is the best place for these tests. It seemed like there aren't any tests of the actual Recorder UI itself, only of specific code gen functionality.

const { recorder } = await openRecorder();
await recorder.setContentAndWait('');
await expect(await recorder.recorderPage.title()).toBe('Playwright Inspector - about:blank');

await recorder.setContentAndWait('', server.EMPTY_PAGE);
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.EMPTY_PAGE}`);
});

test('should update primary page URL when original primary closes', async ({ context, openRecorder, server }) => {
const { recorder } = await openRecorder();
await recorder.setContentAndWait('', `${server.PREFIX}/background-color.html`);
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);

const page2 = await context.newPage();
await page2.goto(`${server.PREFIX}/empty.html`);
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);

const page3 = await context.newPage();
await page3.goto(`${server.PREFIX}/dom.html`);
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);

const page4 = await context.newPage();
await page4.goto(`${server.PREFIX}/grid.html`);
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);

await page2.close();
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/background-color.html`);

await recorder.page.close();
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/dom.html`);

await page3.close();
await expect(await recorder.recorderPage.title()).toBe(`Playwright Inspector - ${server.PREFIX}/grid.html`);
});
Loading