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
178 changes: 78 additions & 100 deletions packages/playwright-core/src/server/recorder/recorderApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import { BrowserContext } from '../browserContext';

import type { Page } from '../page';
import type * as actions from '@recorder/actions';
import type { CallLog, ElementInfo, Mode, Source } from '@recorder/recorderTypes';
import type { CallLog, ElementInfo, Mode, RecorderBackend, RecorderFrontend, Source } from '@recorder/recorderTypes';
import type { Language, LanguageGeneratorOptions } from '../codegen/types';
import type * as channels from '@protocol/channels';
import type { Progress } from '../progress';
import type { AriaTemplateNode } from '@isomorphic/ariaSnapshot';

export type RecorderAppParams = channels.BrowserContextEnableRecorderParams & {
browserName: string;
Expand All @@ -54,10 +56,12 @@ export class RecorderApp {
private _recorderSources: Source[] = [];
private _primaryGeneratorId: string;
private _selectedGeneratorId: string;
private _frontend: RecorderFrontend;

private constructor(recorder: Recorder, params: RecorderAppParams, page: Page, wsEndpointForTest: string | undefined) {
this._page = page;
this._recorder = recorder;
this._frontend = createRecorderFrontend(page);
this.wsEndpointForTest = wsEndpointForTest;

// Make a copy of options to modify them later.
Expand Down Expand Up @@ -103,7 +107,7 @@ export class RecorderApp {
});
});

await this._page.exposeBinding(progress, 'dispatch', false, (_, data: any) => this._handleUIEvent(data));
await this._createDispatcher(progress);

this._page.once('close', () => {
this._recorder.close();
Expand All @@ -116,61 +120,59 @@ export class RecorderApp {

const url = this._recorder.url();
if (url)
this._onPageNavigated(url);
this._onModeChanged(this._recorder.mode());
this._onPausedStateChanged(this._recorder.paused());
this._frontend.pageNavigated({ url });
this._frontend.modeChanged({ mode: this._recorder.mode() });
this._frontend.pauseStateChanged({ paused: this._recorder.paused() });
this._updateActions('reveal');
// Update paused sources *after* generated ones, to reveal the currently paused source if any.
this._onUserSourcesChanged(this._recorder.userSources(), this._recorder.pausedSourceId());
this._onCallLogsUpdated(this._recorder.callLog());
this._frontend.callLogsUpdated({ callLogs: this._recorder.callLog() });
this._wireListeners(this._recorder);
}

private _handleUIEvent(data: any) {
if (data.event === 'clear') {
this._actions = [];
this._updateActions('reveal');
this._recorder.clear();
return;
}
if (data.event === 'fileChanged') {
const source = [...this._recorderSources, ...this._userSources].find(s => s.id === data.params.fileId);
if (source) {
if (source.isRecorded)
this._selectedGeneratorId = source.id;
this._recorder.setLanguage(source.language);
}
return;
}
if (data.event === 'setAutoExpect') {
this._languageGeneratorOptions.generateAutoExpect = data.params.autoExpect;
this._updateActions();
return;
}
if (data.event === 'setMode') {
this._recorder.setMode(data.params.mode);
return;
}
if (data.event === 'resume') {
this._recorder.resume();
return;
}
if (data.event === 'pause') {
this._recorder.pause();
return;
}
if (data.event === 'step') {
this._recorder.step();
return;
}
if (data.event === 'highlightRequested') {
if (data.params.selector)
this._recorder.setHighlightedSelector(data.params.selector);
if (data.params.ariaTemplate)
this._recorder.setHighlightedAriaTemplate(data.params.ariaTemplate);
return;
}
throw new Error(`Unknown event: ${data.event}`);
private async _createDispatcher(progress: Progress) {
const dispatcher: RecorderBackend = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why not make this implement RecorderBackend?

clear: async () => {
this._actions = [];
this._updateActions('reveal');
this._recorder.clear();
},
fileChanged: async (params: { fileId: string }) => {
const source = [...this._recorderSources, ...this._userSources].find(s => s.id === params.fileId);
if (source) {
if (source.isRecorded)
this._selectedGeneratorId = source.id;
this._recorder.setLanguage(source.language);
}
},
setAutoExpect: async (params: { autoExpect: boolean }) => {
this._languageGeneratorOptions.generateAutoExpect = params.autoExpect;
this._updateActions();
},
setMode: async (params: { mode: Mode }) => {
this._recorder.setMode(params.mode);
},
resume: async () => {
this._recorder.resume();
},
pause: async () => {
this._recorder.pause();
},
step: async () => {
this._recorder.step();
},
highlightRequested: async (params: { selector?: string; ariaTemplate?: AriaTemplateNode }) => {
if (params.selector)
this._recorder.setHighlightedSelector(params.selector);
if (params.ariaTemplate)
this._recorder.setHighlightedAriaTemplate(params.ariaTemplate);
},
};

await this._page.exposeBinding(progress, 'sendCommand', false, async (_, data: any) => {
const { method, params } = data as { method: string; params: any };
return await (dispatcher as any)[method].call(dispatcher, params);
});
}

static async show(context: BrowserContext, params: channels.BrowserContextEnableRecorderParams) {
Expand Down Expand Up @@ -246,31 +248,34 @@ export class RecorderApp {
});

recorder.on(RecorderEvent.PageNavigated, (url: string) => {
this._onPageNavigated(url);
this._frontend.pageNavigated({ url });
});

recorder.on(RecorderEvent.ContextClosed, () => {
this._onContextClosed();
this._throttledOutputFile?.flush();
this._page.browserContext.close({ reason: 'Recorder window closed' }).catch(() => {});
});

recorder.on(RecorderEvent.ModeChanged, (mode: Mode) => {
this._onModeChanged(mode);
this._frontend.modeChanged({ mode });
});

recorder.on(RecorderEvent.PausedStateChanged, (paused: boolean) => {
this._onPausedStateChanged(paused);
this._frontend.pauseStateChanged({ paused });
});

recorder.on(RecorderEvent.UserSourcesChanged, (sources: Source[], pausedSourceId?: string) => {
this._onUserSourcesChanged(sources, pausedSourceId);
});

recorder.on(RecorderEvent.ElementPicked, (elementInfo: ElementInfo, userGesture?: boolean) => {
this._onElementPicked(elementInfo, userGesture);
if (userGesture)
this._page.bringToFront();
this._frontend.elementPicked({ elementInfo, userGesture });
});

recorder.on(RecorderEvent.CallLogsUpdated, (callLogs: CallLog[]) => {
this._onCallLogsUpdated(callLogs);
this._frontend.callLogsUpdated({ callLogs });
});
}

Expand All @@ -286,29 +291,6 @@ export class RecorderApp {
this._updateActions();
}

private _onPageNavigated(url: string) {
this._page.mainFrame().evaluateExpression((({ url }: { url: string }) => {
window.playwrightSetPageURL(url);
}).toString(), { isFunction: true }, { url }).catch(() => {});
}

private _onContextClosed() {
this._throttledOutputFile?.flush();
this._page.browserContext.close({ reason: 'Recorder window closed' }).catch(() => {});
}

private _onModeChanged(mode: Mode) {
this._page.mainFrame().evaluateExpression(((mode: Mode) => {
window.playwrightSetMode(mode);
}).toString(), { isFunction: true }, mode).catch(() => {});
}

private _onPausedStateChanged(paused: boolean) {
this._page.mainFrame().evaluateExpression(((paused: boolean) => {
window.playwrightSetPaused(paused);
}).toString(), { isFunction: true }, paused).catch(() => {});
}

private _onUserSourcesChanged(sources: Source[], pausedSourceId: string | undefined) {
if (!sources.length && !this._userSources.length)
return;
Expand All @@ -317,33 +299,15 @@ export class RecorderApp {
this._revealSource(pausedSourceId);
}

private _onElementPicked(elementInfo: ElementInfo, userGesture?: boolean) {
if (userGesture)
this._page.bringToFront();
this._page.mainFrame().evaluateExpression(((param: { elementInfo: ElementInfo, userGesture?: boolean }) => {
window.playwrightElementPicked(param.elementInfo, param.userGesture);
}).toString(), { isFunction: true }, { elementInfo, userGesture }).catch(() => {});
}

private _onCallLogsUpdated(callLogs: CallLog[]) {
this._page.mainFrame().evaluateExpression(((callLogs: CallLog[]) => {
window.playwrightUpdateLogs(callLogs);
}).toString(), { isFunction: true }, callLogs).catch(() => {});
}

private _pushAllSources() {
const sources = [...this._userSources, ...this._recorderSources];
this._page.mainFrame().evaluateExpression((({ sources }: { sources: Source[] }) => {
window.playwrightSetSources(sources);
}).toString(), { isFunction: true }, { sources }).catch(() => {});
this._frontend.sourcesUpdated({ sources });
}

private _revealSource(sourceId: string | undefined) {
if (!sourceId)
return;
this._page.mainFrame().evaluateExpression((({ sourceId }: { sourceId: string }) => {
window.playwrightSelectSource(sourceId);
}).toString(), { isFunction: true }, { sourceId }).catch(() => {});
this._frontend.sourceRevealRequested({ sourceId });
}

private _updateActions(reveal?: 'reveal') {
Expand Down Expand Up @@ -426,4 +390,18 @@ function findPageByGuid(context: BrowserContext, guid: string) {
return context.pages().find(p => p.guid === guid);
}

function createRecorderFrontend(page: Page): RecorderFrontend {
return new Proxy({} as RecorderFrontend, {
get: (_target, prop: string | symbol) => {
if (typeof prop !== 'string')
return undefined;
return (params: any) => {
page.mainFrame().evaluateExpression(((event: { method: string, params?: any }) => {
window.dispatch(event);
}).toString(), { isFunction: true }, { method: prop, params }).catch(() => {});
};
},
});
}

const recorderAppSymbol = Symbol('recorderApp');
4 changes: 2 additions & 2 deletions packages/recorder/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import '@web/common.css';
import { applyTheme } from '@web/theme';
import '@web/third_party/vscode/codicon.css';
import * as ReactDOM from 'react-dom/client';
import { Main } from './main';
import { Recorder } from './recorder';

(async () => {
applyTheme();
ReactDOM.createRoot(document.querySelector('#root')!).render(<Main/>);
ReactDOM.createRoot(document.querySelector('#root')!).render(<Recorder/>);
})();
53 changes: 0 additions & 53 deletions packages/recorder/src/main.tsx

This file was deleted.

Loading