Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: work towards streaming recorder backend (#36484)

Pavel Feldman7fd8ec2291bc
diff --git a/packages/playwright-core/src/server/recorder/contextRecorder.ts b/packages/playwright-core/src/server/recorder/contextRecorder.ts
index f83e8fab25..d00bed9f6e 100644
--- a/packages/playwright-core/src/server/recorder/contextRecorder.ts
+++ b/packages/playwright-core/src/server/recorder/contextRecorder.ts
@@ -16,7 +16,7 @@
import { EventEmitter } from 'events';
-import { RecorderCollection } from './recorderCollection';
+import { RecorderSignalProcessor } from './recorderSignalProcessor';
import * as rawRecorderSource from '../../generated/pollingRecorderSource';
import { eventsHelper, monotonicTime, quoteCSSAttributeValue } from '../../utils';
import { raceAgainstDeadline } from '../../utils/isomorphic/timeoutRunner';
@@ -26,6 +26,8 @@ import { Frame } from '../frames';
import { Page } from '../page';
import { ThrottledFile } from './throttledFile';
import { generateCode } from '../codegen/language';
+import { performAction } from './recorderRunner';
+import { collapseActions } from './recorderUtils';
import type { RegisteredListener } from '../../utils';
import type { Language, LanguageGenerator, LanguageGeneratorOptions } from '../codegen/types';
@@ -33,6 +35,7 @@ import type * as channels from '@protocol/channels';
import type * as actions from '@recorder/actions';
import type { Source } from '@recorder/recorderTypes';
import type { Progress } from '@protocol/progress';
+import type { Signal } from '../../../../recorder/src/actions';
type BindingSource = { frame: Frame, page: Page };
@@ -45,7 +48,7 @@ export class ContextRecorder extends EventEmitter {
Change: 'change'
};
- private _collection: RecorderCollection;
+ private _collection: RecorderSignalProcessor;
private _pageAliases = new Map<Page, string>();
private _lastPopupOrdinal = 0;
private _lastDialogOrdinal = -1;
@@ -57,6 +60,9 @@ export class ContextRecorder extends EventEmitter {
private _throttledOutputFile: ThrottledFile | null = null;
private _orderedLanguages: LanguageGenerator[] = [];
private _listeners: RegisteredListener[] = [];
+ private _actions: actions.ActionInContext[] = [];
+ private _languageGeneratorOptions: LanguageGeneratorOptions;
+ private _enabled: boolean = false;
constructor(context: BrowserContext, params: channels.BrowserContextEnableRecorderParams, delegate: ContextRecorderDelegate) {
super();
@@ -65,10 +71,9 @@ export class ContextRecorder extends EventEmitter {
this._delegate = delegate;
this._recorderSources = [];
const language = params.language || context._browser.sdkLanguage();
- this.setOutput(language, params.outputFile);
// Make a copy of options to modify them later.
- const languageGeneratorOptions: LanguageGeneratorOptions = {
+ this._languageGeneratorOptions = {
browserName: context._browser.options.name,
launchOptions: { headless: false, ...params.launchOptions, tracesDir: undefined },
contextOptions: { ...params.contextOptions },
@@ -76,42 +81,66 @@ export class ContextRecorder extends EventEmitter {
saveStorage: params.saveStorage,
};
- this._collection = new RecorderCollection(this._pageAliases);
- this._collection.on('change', (actions: actions.ActionInContext[]) => {
- this._recorderSources = [];
- for (const languageGenerator of this._orderedLanguages) {
- const { header, footer, actionTexts, text } = generateCode(actions, languageGenerator, languageGeneratorOptions);
- const source: Source = {
- isRecorded: true,
- label: languageGenerator.name,
- group: languageGenerator.groupName,
- id: languageGenerator.id,
- text,
- header,
- footer,
- actions: actionTexts,
- language: languageGenerator.highlighter,
- highlight: []
- };
- source.revealLine = text.split('\n').length - 1;
- this._recorderSources.push(source);
- if (languageGenerator === this._orderedLanguages[0])
- this._throttledOutputFile?.setContent(source.text);
- }
- this.emit(ContextRecorder.Events.Change, {
- sources: this._recorderSources,
- actions
- });
+ this._collection = new RecorderSignalProcessor();
+ this._collection.on('action', (actionInContext: actions.ActionInContext) => {
+ if (!this._enabled)
+ return;
+ this._actions.push(actionInContext);
+ this._updateActions();
+ });
+ this._collection.on('signal', (signal: Signal) => {
+ if (!this._enabled)
+ return;
+ const lastAction = this._actions[this._actions.length - 1];
+ if (lastAction)
+ lastAction.action.signals.push(signal);
+ this._updateActions();
});
+
context.on(BrowserContext.Events.BeforeClose, () => {
this._throttledOutputFile?.flush();
});
this._listeners.push(eventsHelper.addEventListener(process, 'exit', () => {
this._throttledOutputFile?.flush();
}));
+
+ this.setOutput(language, params.outputFile);
this.setEnabled(params.mode === 'recording');
}
+ private _resetActions() {
+ this._actions = [];
+ this._updateActions();
+ }
+
+ private _updateActions() {
+ const actions = collapseActions(this._actions);
+ this._recorderSources = [];
+ for (const languageGenerator of this._orderedLanguages) {
+ const { header, footer, actionTexts, text } = generateCode(actions, languageGenerator, this._languageGeneratorOptions);
+ const source: Source = {
+ isRecorded: true,
+ label: languageGenerator.name,
+ group: languageGenerator.groupName,
+ id: languageGenerator.id,
+ text,
+ header,
+ footer,
+ actions: actionTexts,
+ language: languageGenerator.highlighter,
+ highlight: []
+ };
+ source.revealLine = text.split('\n').length - 1;
+ this._recorderSources.push(source);
+ if (languageGenerator === this._orderedLanguages[0])
+ this._throttledOutputFile?.setContent(source.text);
+ }
+ this.emit(ContextRecorder.Events.Change, {
+ sources: this._recorderSources,
+ actions
+ });
+ }
+
setOutput(codegenId: string, outputFile?: string) {
const languages = languageSet();
const primaryLanguage = [...languages].find(l => l.id === codegenId);
@@ -120,7 +149,7 @@ export class ContextRecorder extends EventEmitter {
languages.delete(primaryLanguage);
this._orderedLanguages = [primaryLanguage, ...languages];
this._throttledOutputFile = outputFile ? new ThrottledFile(outputFile) : null;
- this._collection?.restart();
+ this._resetActions();
}
languageName(id?: string): Language {
@@ -154,7 +183,7 @@ export class ContextRecorder extends EventEmitter {
}
setEnabled(enabled: boolean) {
- this._collection.setEnabled(enabled);
+ this._enabled = enabled;
}
dispose() {
@@ -165,7 +194,7 @@ export class ContextRecorder extends EventEmitter {
// First page is called page, others are called popup1, popup2, etc.
const frame = page.mainFrame();
page.on('close', () => {
- this._collection.addRecordedAction({
+ this._collection.addAction({
frame: this._describeMainFrame(page),
action: {
name: 'closePage',
@@ -187,7 +216,7 @@ export class ContextRecorder extends EventEmitter {
if (page.opener()) {
this._onPopup(page.opener()!, page);
} else {
- this._collection.addRecordedAction({
+ this._collection.addAction({
frame: this._describeMainFrame(page),
action: {
name: 'openPage',
@@ -200,7 +229,7 @@ export class ContextRecorder extends EventEmitter {
}
clearScript(): void {
- this._collection.restart();
+ this._resetActions();
if (this._params.mode === 'recording') {
for (const page of this._context.pages())
this._onFrameNavigated(page.mainFrame(), page);
@@ -242,11 +271,15 @@ export class ContextRecorder extends EventEmitter {
}
private async _performAction(frame: Frame, action: actions.PerformOnRecordAction) {
- await this._collection.performAction(await this._createActionInContext(frame, action));
+ const actionInContext = await this._createActionInContext(frame, action);
+ this._collection.addAction(actionInContext);
+ if (actionInContext.action.name !== 'openPage' && actionInContext.action.name !== 'closePage')
+ await performAction(this._pageAliases, actionInContext);
+ actionInContext.endTime = monotonicTime();
}
private async _recordAction(frame: Frame, action: actions.Action) {
- this._collection.addRecordedAction(await this._createActionInContext(frame, action));
+ this._collection.addAction(await this._createActionInContext(frame, action));
}
private _onFrameNavigated(frame: Frame, page: Page) {
diff --git a/packages/playwright-core/src/server/recorder/recorderCollection.ts b/packages/playwright-core/src/server/recorder/recorderCollection.ts
deleted file mode 100644
index b8802a920a..0000000000
--- a/packages/playwright-core/src/server/recorder/recorderCollection.ts
+++ /dev/null
@@ -1,126 +0,0 @@
-/**
- * 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 { EventEmitter } from 'events';
-
-import { performAction } from './recorderRunner';
-import { collapseActions } from './recorderUtils';
-import { isUnderTest } from '../utils/debug';
-import { monotonicTime } from '../../utils/isomorphic/time';
-
-import type { Signal } from '../../../../recorder/src/actions';
-import type { Frame } from '../frames';
-import type { Page } from '../page';
-import type * as actions from '@recorder/actions';
-
-export class RecorderCollection extends EventEmitter {
- private _actions: actions.ActionInContext[] = [];
- private _enabled = false;
- private _pageAliases: Map<Page, string>;
-
- constructor(pageAliases: Map<Page, string>) {
- super();
- this._pageAliases = pageAliases;
- }
-
- restart() {
- this._actions = [];
- this.emit('change', []);
- }
-
- setEnabled(enabled: boolean) {
- this._enabled = enabled;
- }
-
- async performAction(actionInContext: actions.ActionInContext) {
- await this._addAction(actionInContext, async () => {
- await performAction(this._pageAliases, actionInContext);
- });
- }
-
- addRecordedAction(actionInContext: actions.ActionInContext) {
- if (['openPage', 'closePage'].includes(actionInContext.action.name)) {
- this._actions.push(actionInContext);
- this._fireChange();
- return;
- }
- this._addAction(actionInContext).catch(() => {});
- }
-
- private async _addAction(actionInContext: actions.ActionInContext, callback?: () => Promise<void>) {
- if (!this._enabled)
- return;
- if (actionInContext.action.name === 'openPage' || actionInContext.action.name === 'closePage') {
- this._actions.push(actionInContext);
- this._fireChange();
- return;
- }
-
- this._actions.push(actionInContext);
- this._fireChange();
- await callback?.().catch();
- actionInContext.endTime = monotonicTime();
- }
-
- signal(pageAlias: string, frame: Frame, signal: Signal) {
- if (!this._enabled)
- return;
-
- if (signal.name === 'navigation' && frame._page.mainFrame() === frame) {
- const timestamp = monotonicTime();
- const lastAction = this._actions[this._actions.length - 1];
- const signalThreshold = isUnderTest() ? 500 : 5000;
-
- let generateGoto = false;
- if (!lastAction)
- generateGoto = true;
- else if (lastAction.action.name !== 'click' && lastAction.action.name !== 'press' && lastAction.action.name !== 'fill')
- generateGoto = true;
- else if (timestamp - lastAction.startTime > signalThreshold)
- generateGoto = true;
-
- if (generateGoto) {
- this.addRecordedAction({
- frame: {
- pageAlias,
- framePath: [],
- },
- action: {
- name: 'navigate',
- url: frame.url(),
- signals: [],
- },
- startTime: timestamp,
- endTime: timestamp,
- });
- }
- return;
- }
-
- if (this._actions.length) {
- this._actions[this._actions.length - 1].action.signals.push(signal);
- this._fireChange();
- return;
- }
- }
-
- private _fireChange() {
- if (!this._enabled)
- return;
-
- this.emit('change', collapseActions(this._actions));
- }
-}
diff --git a/packages/playwright-core/src/server/recorder/recorderSignalProcessor.ts b/packages/playwright-core/src/server/recorder/recorderSignalProcessor.ts
new file mode 100644
index 0000000000..05c07a89ae
--- /dev/null
+++ b/packages/playwright-core/src/server/recorder/recorderSignalProcessor.ts
@@ -0,0 +1,68 @@
+/**
+ * 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 { EventEmitter } from 'events';
+
+import { isUnderTest } from '../utils/debug';
+import { monotonicTime } from '../../utils/isomorphic/time';
+
+import type { Signal } from '../../../../recorder/src/actions';
+import type { Frame } from '../frames';
+import type * as actions from '@recorder/actions';
+
+export class RecorderSignalProcessor extends EventEmitter {
+ private _lastAction: actions.ActionInContext | null = null;
+
+ addAction(actionInContext: actions.ActionInContext, callback?: () => Promise<void>) {
+ this._lastAction = actionInContext;
+ this.emit('action', actionInContext);
+ }
+
+ signal(pageAlias: string, frame: Frame, signal: Signal) {
+ if (signal.name === 'navigation' && frame._page.mainFrame() === frame) {
+ const timestamp = monotonicTime();
+ const lastAction = this._lastAction;
+ const signalThreshold = isUnderTest() ? 500 : 5000;
+
+ let generateGoto = false;
+ if (!lastAction)
+ generateGoto = true;
+ else if (lastAction.action.name !== 'click' && lastAction.action.name !== 'press' && lastAction.action.name !== 'fill')
+ generateGoto = true;
+ else if (timestamp - lastAction.startTime > signalThreshold)
+ generateGoto = true;
+
+ if (generateGoto) {
+ this.addAction({
+ frame: {
+ pageAlias,
+ framePath: [],
+ },
+ action: {
+ name: 'navigate',
+ url: frame.url(),
+ signals: [],
+ },
+ startTime: timestamp,
+ endTime: timestamp,
+ });
+ }
+ return;
+ }
+
+ this.emit('signal', signal);
+ }
+}