2020-12-28 14:50:12 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-01-24 19:21:19 -08:00
|
|
|
import * as actions from './recorder/recorderActions';
|
2021-01-25 14:49:26 -08:00
|
|
|
import type * as channels from '../../protocol/channels';
|
|
|
|
import { CodeGenerator, ActionInContext } from './recorder/codeGenerator';
|
2021-01-27 13:19:36 -08:00
|
|
|
import { describeFrame, toClickOptions, toModifiers } from './recorder/utils';
|
2021-01-24 19:21:19 -08:00
|
|
|
import { Page } from '../page';
|
|
|
|
import { Frame } from '../frames';
|
|
|
|
import { BrowserContext } from '../browserContext';
|
|
|
|
import { LanguageGenerator } from './recorder/language';
|
|
|
|
import { JavaScriptLanguageGenerator } from './recorder/javascript';
|
|
|
|
import { CSharpLanguageGenerator } from './recorder/csharp';
|
|
|
|
import { PythonLanguageGenerator } from './recorder/python';
|
|
|
|
import * as recorderSource from '../../generated/recorderSource';
|
2021-01-25 14:49:26 -08:00
|
|
|
import * as consoleApiSource from '../../generated/consoleApiSource';
|
2021-02-11 17:46:54 -08:00
|
|
|
import { BufferedOutput, FileOutput, OutputMultiplexer, RecorderOutput } from './recorder/outputs';
|
2021-02-05 14:24:27 -08:00
|
|
|
import { EventData, Mode, RecorderApp } from './recorder/recorderApp';
|
2021-02-09 14:44:48 -08:00
|
|
|
import { internalCallMetadata } from '../instrumentation';
|
2020-12-28 14:50:12 -08:00
|
|
|
|
|
|
|
type BindingSource = { frame: Frame, page: Page };
|
2021-01-25 14:49:26 -08:00
|
|
|
|
|
|
|
const symbol = Symbol('RecorderSupplement');
|
|
|
|
|
2021-01-24 19:21:19 -08:00
|
|
|
export class RecorderSupplement {
|
2020-12-28 14:50:12 -08:00
|
|
|
private _generator: CodeGenerator;
|
|
|
|
private _pageAliases = new Map<Page, string>();
|
|
|
|
private _lastPopupOrdinal = 0;
|
|
|
|
private _lastDialogOrdinal = 0;
|
|
|
|
private _timers = new Set<NodeJS.Timeout>();
|
2021-01-24 19:21:19 -08:00
|
|
|
private _context: BrowserContext;
|
2021-01-25 14:49:26 -08:00
|
|
|
private _resumeCallback: (() => void) | null = null;
|
2021-02-05 14:24:27 -08:00
|
|
|
private _mode: Mode;
|
2021-01-25 14:49:26 -08:00
|
|
|
private _paused = false;
|
|
|
|
private _output: OutputMultiplexer;
|
2021-01-27 17:02:09 -08:00
|
|
|
private _bufferedOutput: BufferedOutput;
|
2021-02-05 14:24:27 -08:00
|
|
|
private _recorderApp: RecorderApp | null = null;
|
2021-01-31 16:37:13 -08:00
|
|
|
private _highlighterType: string;
|
2021-02-03 16:01:51 -08:00
|
|
|
private _params: channels.BrowserContextRecorderSupplementEnableParams;
|
2021-01-25 14:49:26 -08:00
|
|
|
|
2021-02-11 17:46:54 -08:00
|
|
|
static getOrCreate(context: BrowserContext, params: channels.BrowserContextRecorderSupplementEnableParams = {}): Promise<RecorderSupplement> {
|
2021-01-25 14:49:26 -08:00
|
|
|
let recorderPromise = (context as any)[symbol] as Promise<RecorderSupplement>;
|
|
|
|
if (!recorderPromise) {
|
2021-02-03 16:01:51 -08:00
|
|
|
const recorder = new RecorderSupplement(context, params);
|
2021-01-25 14:49:26 -08:00
|
|
|
recorderPromise = recorder.install().then(() => recorder);
|
|
|
|
(context as any)[symbol] = recorderPromise;
|
|
|
|
}
|
|
|
|
return recorderPromise;
|
|
|
|
}
|
2021-01-24 19:21:19 -08:00
|
|
|
|
2021-02-03 16:01:51 -08:00
|
|
|
constructor(context: BrowserContext, params: channels.BrowserContextRecorderSupplementEnableParams) {
|
2021-01-24 19:21:19 -08:00
|
|
|
this._context = context;
|
2021-02-03 16:01:51 -08:00
|
|
|
this._params = params;
|
2021-02-05 14:24:27 -08:00
|
|
|
this._mode = params.startRecording ? 'recording' : 'none';
|
2021-01-24 19:21:19 -08:00
|
|
|
let languageGenerator: LanguageGenerator;
|
2021-02-11 17:46:54 -08:00
|
|
|
const language = params.language || context._options.sdkLanguage;
|
|
|
|
switch (language) {
|
2021-01-24 19:21:19 -08:00
|
|
|
case 'javascript': languageGenerator = new JavaScriptLanguageGenerator(); break;
|
|
|
|
case 'csharp': languageGenerator = new CSharpLanguageGenerator(); break;
|
|
|
|
case 'python':
|
|
|
|
case 'python-async': languageGenerator = new PythonLanguageGenerator(params.language === 'python-async'); break;
|
|
|
|
default: throw new Error(`Invalid target: '${params.language}'`);
|
|
|
|
}
|
2021-02-11 17:46:54 -08:00
|
|
|
let highlighterType = language;
|
2021-01-25 14:49:26 -08:00
|
|
|
if (highlighterType === 'python-async')
|
|
|
|
highlighterType = 'python';
|
|
|
|
|
2021-02-11 17:46:54 -08:00
|
|
|
const outputs: RecorderOutput[] = [];
|
2021-01-31 16:37:13 -08:00
|
|
|
this._highlighterType = highlighterType;
|
|
|
|
this._bufferedOutput = new BufferedOutput(async text => {
|
2021-02-05 14:24:27 -08:00
|
|
|
if (this._recorderApp)
|
|
|
|
this._recorderApp.setSource(text, highlighterType);
|
2021-01-31 16:37:13 -08:00
|
|
|
});
|
2021-01-27 17:02:09 -08:00
|
|
|
outputs.push(this._bufferedOutput);
|
2021-01-25 14:49:26 -08:00
|
|
|
if (params.outputFile)
|
|
|
|
outputs.push(new FileOutput(params.outputFile));
|
|
|
|
this._output = new OutputMultiplexer(outputs);
|
2021-02-03 16:01:51 -08:00
|
|
|
this._output.setEnabled(!!params.startRecording);
|
2021-01-25 14:49:26 -08:00
|
|
|
context.on(BrowserContext.Events.BeforeClose, () => this._output.flush());
|
|
|
|
|
2021-02-03 16:01:51 -08:00
|
|
|
const generator = new CodeGenerator(context._browser.options.name, !!params.startRecording, params.launchOptions || {}, params.contextOptions || {}, this._output, languageGenerator, params.device, params.saveStorage);
|
2021-01-24 19:21:19 -08:00
|
|
|
this._generator = generator;
|
|
|
|
}
|
2020-12-28 14:50:12 -08:00
|
|
|
|
2021-01-24 19:21:19 -08:00
|
|
|
async install() {
|
2021-02-11 17:46:54 -08:00
|
|
|
const recorderApp = await RecorderApp.open(this._context);
|
2021-02-05 14:24:27 -08:00
|
|
|
this._recorderApp = recorderApp;
|
|
|
|
recorderApp.once('close', () => {
|
|
|
|
this._recorderApp = null;
|
|
|
|
});
|
|
|
|
recorderApp.on('event', (data: EventData) => {
|
|
|
|
if (data.event === 'setMode') {
|
|
|
|
this._mode = data.params.mode;
|
|
|
|
recorderApp.setMode(this._mode);
|
|
|
|
this._output.setEnabled(this._mode === 'recording');
|
|
|
|
if (this._mode !== 'none')
|
|
|
|
this._context.pages()[0].bringToFront().catch(() => {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (data.event === 'resume') {
|
|
|
|
this._resume();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (data.event === 'clear') {
|
|
|
|
this._clearScript();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
recorderApp.setMode(this._mode),
|
|
|
|
recorderApp.setPaused(this._paused),
|
|
|
|
recorderApp.setSource(this._bufferedOutput.buffer(), this._highlighterType)
|
|
|
|
]);
|
|
|
|
|
|
|
|
this._context.on(BrowserContext.Events.Page, page => this._onPage(page));
|
2021-01-24 19:21:19 -08:00
|
|
|
for (const page of this._context.pages())
|
|
|
|
this._onPage(page);
|
2020-12-28 14:50:12 -08:00
|
|
|
|
2021-02-05 14:24:27 -08:00
|
|
|
this._context.once(BrowserContext.Events.Close, () => {
|
2021-01-24 19:21:19 -08:00
|
|
|
for (const timer of this._timers)
|
|
|
|
clearTimeout(timer);
|
|
|
|
this._timers.clear();
|
2021-02-05 14:24:27 -08:00
|
|
|
recorderApp.close().catch(() => {});
|
2021-01-24 19:21:19 -08:00
|
|
|
});
|
2020-12-28 14:50:12 -08:00
|
|
|
|
|
|
|
// Input actions that potentially lead to navigation are intercepted on the page and are
|
|
|
|
// performed by the Playwright.
|
2021-02-03 16:01:51 -08:00
|
|
|
await this._context.exposeBinding('_playwrightRecorderPerformAction', false,
|
2021-01-24 19:21:19 -08:00
|
|
|
(source: BindingSource, action: actions.Action) => this._performAction(source.frame, action));
|
2020-12-28 14:50:12 -08:00
|
|
|
|
|
|
|
// Other non-essential actions are simply being recorded.
|
2021-02-03 16:01:51 -08:00
|
|
|
await this._context.exposeBinding('_playwrightRecorderRecordAction', false,
|
2021-01-24 19:21:19 -08:00
|
|
|
(source: BindingSource, action: actions.Action) => this._recordAction(source.frame, action));
|
2020-12-28 14:50:12 -08:00
|
|
|
|
|
|
|
// Commits last action so that no further signals are added to it.
|
2021-02-03 16:01:51 -08:00
|
|
|
await this._context.exposeBinding('_playwrightRecorderCommitAction', false,
|
2021-01-24 19:21:19 -08:00
|
|
|
(source: BindingSource, action: actions.Action) => this._generator.commitLastAction());
|
2020-12-28 14:50:12 -08:00
|
|
|
|
2021-02-03 16:01:51 -08:00
|
|
|
await this._context.exposeBinding('_playwrightRecorderState', false, () => {
|
2021-02-05 14:24:27 -08:00
|
|
|
return { mode: this._mode };
|
2021-01-25 14:49:26 -08:00
|
|
|
});
|
|
|
|
|
2021-02-03 16:01:51 -08:00
|
|
|
await this._context.exposeBinding('_playwrightResume', false, () => {
|
2021-02-05 14:24:27 -08:00
|
|
|
this._resume().catch(() => {});
|
2021-01-25 14:49:26 -08:00
|
|
|
});
|
|
|
|
|
2021-01-24 19:21:19 -08:00
|
|
|
await this._context.extendInjectedScript(recorderSource.source);
|
2021-01-25 14:49:26 -08:00
|
|
|
await this._context.extendInjectedScript(consoleApiSource.source);
|
2021-02-05 14:24:27 -08:00
|
|
|
|
|
|
|
(this._context as any).recorderAppForTest = recorderApp;
|
2021-01-25 14:49:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async pause() {
|
|
|
|
this._paused = true;
|
2021-02-05 14:24:27 -08:00
|
|
|
this._recorderApp!.setPaused(true);
|
2021-01-31 16:37:13 -08:00
|
|
|
return new Promise<void>(f => this._resumeCallback = f);
|
2020-12-28 14:50:12 -08:00
|
|
|
}
|
|
|
|
|
2021-02-05 14:24:27 -08:00
|
|
|
private async _resume() {
|
|
|
|
if (this._resumeCallback)
|
|
|
|
this._resumeCallback();
|
|
|
|
this._resumeCallback = null;
|
|
|
|
this._paused = false;
|
|
|
|
if (this._recorderApp)
|
|
|
|
this._recorderApp.setPaused(this._paused);
|
|
|
|
}
|
|
|
|
|
2020-12-28 14:50:12 -08:00
|
|
|
private async _onPage(page: Page) {
|
|
|
|
// First page is called page, others are called popup1, popup2, etc.
|
2021-01-24 19:21:19 -08:00
|
|
|
const frame = page.mainFrame();
|
2020-12-28 14:50:12 -08:00
|
|
|
page.on('close', () => {
|
|
|
|
this._pageAliases.delete(page);
|
|
|
|
this._generator.addAction({
|
|
|
|
pageAlias,
|
2021-01-27 13:19:36 -08:00
|
|
|
...describeFrame(page.mainFrame()),
|
2020-12-28 14:50:12 -08:00
|
|
|
committed: true,
|
|
|
|
action: {
|
|
|
|
name: 'closePage',
|
|
|
|
signals: [],
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2021-01-24 19:21:19 -08:00
|
|
|
frame.on(Frame.Events.Navigation, () => this._onFrameNavigated(frame, page));
|
|
|
|
page.on(Page.Events.Download, () => this._onDownload(page));
|
|
|
|
page.on(Page.Events.Popup, popup => this._onPopup(page, popup));
|
|
|
|
page.on(Page.Events.Dialog, () => this._onDialog(page));
|
2020-12-28 14:50:12 -08:00
|
|
|
const suffix = this._pageAliases.size ? String(++this._lastPopupOrdinal) : '';
|
|
|
|
const pageAlias = 'page' + suffix;
|
|
|
|
this._pageAliases.set(page, pageAlias);
|
|
|
|
|
|
|
|
const isPopup = !!await page.opener();
|
|
|
|
// Could happen due to the await above.
|
|
|
|
if (page.isClosed())
|
|
|
|
return;
|
|
|
|
if (!isPopup) {
|
|
|
|
this._generator.addAction({
|
|
|
|
pageAlias,
|
2021-01-27 13:19:36 -08:00
|
|
|
...describeFrame(page.mainFrame()),
|
2020-12-28 14:50:12 -08:00
|
|
|
committed: true,
|
|
|
|
action: {
|
|
|
|
name: 'openPage',
|
2021-01-24 19:21:19 -08:00
|
|
|
url: page.mainFrame().url(),
|
2020-12-28 14:50:12 -08:00
|
|
|
signals: [],
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:37:13 -08:00
|
|
|
private _clearScript(): void {
|
|
|
|
this._bufferedOutput.clear();
|
|
|
|
this._generator.restart();
|
2021-02-03 16:01:51 -08:00
|
|
|
if (!!this._params.startRecording) {
|
2021-01-31 16:37:13 -08:00
|
|
|
for (const page of this._context.pages())
|
|
|
|
this._onFrameNavigated(page.mainFrame(), page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 14:50:12 -08:00
|
|
|
private async _performAction(frame: Frame, action: actions.Action) {
|
2021-01-24 19:21:19 -08:00
|
|
|
const page = frame._page;
|
2020-12-28 14:50:12 -08:00
|
|
|
const actionInContext: ActionInContext = {
|
|
|
|
pageAlias: this._pageAliases.get(page)!,
|
2021-01-27 13:19:36 -08:00
|
|
|
...describeFrame(frame),
|
2020-12-28 14:50:12 -08:00
|
|
|
action
|
|
|
|
};
|
|
|
|
this._generator.willPerformAction(actionInContext);
|
2021-02-09 14:44:48 -08:00
|
|
|
const noCallMetadata = internalCallMetadata();
|
2021-01-27 15:57:28 -08:00
|
|
|
try {
|
|
|
|
const kActionTimeout = 5000;
|
|
|
|
if (action.name === 'click') {
|
|
|
|
const { options } = toClickOptions(action);
|
2021-02-09 14:44:48 -08:00
|
|
|
await frame.click(noCallMetadata, action.selector, { ...options, timeout: kActionTimeout });
|
2021-01-27 15:57:28 -08:00
|
|
|
}
|
|
|
|
if (action.name === 'press') {
|
|
|
|
const modifiers = toModifiers(action.modifiers);
|
|
|
|
const shortcut = [...modifiers, action.key].join('+');
|
2021-02-09 14:44:48 -08:00
|
|
|
await frame.press(noCallMetadata, action.selector, shortcut, { timeout: kActionTimeout });
|
2021-01-27 15:57:28 -08:00
|
|
|
}
|
|
|
|
if (action.name === 'check')
|
2021-02-09 14:44:48 -08:00
|
|
|
await frame.check(noCallMetadata, action.selector, { timeout: kActionTimeout });
|
2021-01-27 15:57:28 -08:00
|
|
|
if (action.name === 'uncheck')
|
2021-02-09 14:44:48 -08:00
|
|
|
await frame.uncheck(noCallMetadata, action.selector, { timeout: kActionTimeout });
|
2021-01-27 15:57:28 -08:00
|
|
|
if (action.name === 'select')
|
2021-02-09 14:44:48 -08:00
|
|
|
await frame.selectOption(noCallMetadata, action.selector, [], action.options.map(value => ({ value })), { timeout: kActionTimeout });
|
2021-01-27 15:57:28 -08:00
|
|
|
} catch (e) {
|
|
|
|
this._generator.performedActionFailed(actionInContext);
|
|
|
|
return;
|
2020-12-28 14:50:12 -08:00
|
|
|
}
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
actionInContext.committed = true;
|
|
|
|
this._timers.delete(timer);
|
|
|
|
}, 5000);
|
|
|
|
this._generator.didPerformAction(actionInContext);
|
|
|
|
this._timers.add(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _recordAction(frame: Frame, action: actions.Action) {
|
|
|
|
this._generator.addAction({
|
2021-01-24 19:21:19 -08:00
|
|
|
pageAlias: this._pageAliases.get(frame._page)!,
|
2021-01-27 13:19:36 -08:00
|
|
|
...describeFrame(frame),
|
2020-12-28 14:50:12 -08:00
|
|
|
action
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onFrameNavigated(frame: Frame, page: Page) {
|
|
|
|
const pageAlias = this._pageAliases.get(page);
|
|
|
|
this._generator.signal(pageAlias!, frame, { name: 'navigation', url: frame.url() });
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onPopup(page: Page, popup: Page) {
|
|
|
|
const pageAlias = this._pageAliases.get(page)!;
|
|
|
|
const popupAlias = this._pageAliases.get(popup)!;
|
|
|
|
this._generator.signal(pageAlias, page.mainFrame(), { name: 'popup', popupAlias });
|
|
|
|
}
|
2021-01-24 19:21:19 -08:00
|
|
|
private _onDownload(page: Page) {
|
2020-12-28 14:50:12 -08:00
|
|
|
const pageAlias = this._pageAliases.get(page)!;
|
|
|
|
this._generator.signal(pageAlias, page.mainFrame(), { name: 'download' });
|
|
|
|
}
|
|
|
|
|
2021-01-24 19:21:19 -08:00
|
|
|
private _onDialog(page: Page) {
|
2020-12-28 14:50:12 -08:00
|
|
|
const pageAlias = this._pageAliases.get(page)!;
|
|
|
|
this._generator.signal(pageAlias, page.mainFrame(), { name: 'dialog', dialogAlias: String(++this._lastDialogOrdinal) });
|
|
|
|
}
|
|
|
|
}
|