Skip to content

Commit

Permalink
modify and export recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
kyungeunni authored and devcorpio committed Mar 26, 2024
1 parent 82804cc commit 7f80cfc
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ yarn.lock
/packages/playwright-core/src/generated
/packages/playwright-ct-core/src/generated
packages/*/lib/
# !packages/playwright-core/lib/
drivers/
.android-sdk/
.gradle/
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"./lib/remote/playwrightServer": "./lib/remote/playwrightServer.js",
"./lib/server": "./lib/server/index.js",
"./lib/utils": "./lib/utils/index.js",
"./lib/server/recorder/javascript": "./lib/server/recorder/javascript.js",
"./lib/utilsBundle": "./lib/utilsBundle.js",
"./lib/zipBundle": "./lib/zipBundle.js",
"./types/protocol": "./types/protocol.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/cli/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ async function codegen(options: Options & { target: string, output?: string, tes
testIdAttributeName,
outputFile: outputFile ? path.resolve(outputFile) : undefined,
handleSIGINT: false,
showRecorder: true,
});
await openPage(context, url);
}
Expand Down
12 changes: 1 addition & 11 deletions packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,17 +459,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
await this._closedPromise;
}

async _enableRecorder(params: {
language: string,
launchOptions?: LaunchOptions,
contextOptions?: BrowserContextOptions,
device?: string,
saveStorage?: string,
mode?: 'recording' | 'inspecting',
testIdAttributeName?: string,
outputFile?: string,
handleSIGINT?: boolean,
}) {
async _enableRecorder(params: channels.BrowserContextRecorderSupplementEnableParams) {
await this._channel.recorderSupplementEnable(params);
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,8 @@ scheme.BrowserContextRecorderSupplementEnableParams = tObject({
outputFile: tOptional(tString),
handleSIGINT: tOptional(tBoolean),
omitCallTracking: tOptional(tBoolean),
showRecorder: tOptional(tBoolean),
actionListener: tOptional(tAny),
});
scheme.BrowserContextRecorderSupplementEnableResult = tOptional(tObject({}));
scheme.BrowserContextNewCDPSessionParams = tObject({
Expand Down
27 changes: 21 additions & 6 deletions packages/playwright-core/src/server/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Recorder implements InstrumentationListener {
let recorderPromise = (context as any)[recorderSymbol] as Promise<Recorder>;
if (!recorderPromise) {
const recorder = new Recorder(context, params);
recorderPromise = recorder.install().then(() => recorder);
recorderPromise = recorder.install(Boolean(params.showRecorder)).then(() => recorder);
(context as any)[recorderSymbol] = recorderPromise;
}
return recorderPromise;
Expand All @@ -105,13 +105,14 @@ export class Recorder implements InstrumentationListener {
}
}


private static async defaultRecorderAppFactory(recorder: Recorder) {
if (process.env.PW_CODEGEN_NO_INSPECTOR)
return new EmptyRecorderApp();
return await RecorderApp.open(recorder, recorder._context, recorder._handleSIGINT);
}

async install() {
async installRecorder() {
const recorderApp = await (Recorder.recorderAppFactory || Recorder.defaultRecorderAppFactory)(this);
this._recorderApp = recorderApp;
recorderApp.once('close', () => {
Expand Down Expand Up @@ -156,10 +157,16 @@ export class Recorder implements InstrumentationListener {
this._pushAllSources()
]);

(this._context as any).recorderAppForTest = this._recorderApp;
}

async install(showRecorder: Boolean) {
if (showRecorder)
await this.installRecorder();
this._context.once(BrowserContext.Events.Close, () => {
this._contextRecorder.dispose();
this._context.instrumentation.removeListener(this);
recorderApp.close().catch(() => {});
this._recorderApp?.close().catch(() => {});
});
this._contextRecorder.on(ContextRecorder.Events.Change, (data: { sources: Source[], primaryFileName: string }) => {
this._recorderSources = data.sources;
Expand Down Expand Up @@ -200,6 +207,7 @@ export class Recorder implements InstrumentationListener {
}
const fullSelector = (await Promise.all(selectorPromises)).filter(Boolean);
fullSelector.push(selector);
this._contextRecorder.emitSelector(selector);
await this._recorderApp?.setSelector(fullSelector.join(' >> internal:control=enter-frame >> '), true);
});

Expand All @@ -215,6 +223,11 @@ export class Recorder implements InstrumentationListener {
this._overlayState = state;
});

// added for synthetics
await this._context.exposeBinding('__pw_setMode', false, async (_, mode: Mode) => {
this.setMode(mode);
});

await this._context.exposeBinding('__pw_resume', false, () => {
this._debugger.resume(false);
});
Expand All @@ -225,8 +238,6 @@ export class Recorder implements InstrumentationListener {
if (this._debugger.isPaused())
this._pausedStateChanged();
this._debugger.on(Debugger.Events.PausedStateChanged, () => this._pausedStateChanged());

(this._context as any).recorderAppForTest = recorderApp;
}

_pausedStateChanged() {
Expand Down Expand Up @@ -395,7 +406,7 @@ class ContextRecorder extends EventEmitter {
this._recorderSources = [];
const language = params.language || context.attribution.playwright.options.sdkLanguage;
this.setOutput(language, params.outputFile);
const generator = new CodeGenerator(context._browser.options.name, params.mode === 'recording', params.launchOptions || {}, params.contextOptions || {}, params.device, params.saveStorage);
const generator = new CodeGenerator(context._browser.options.name, params.mode === 'recording', params.launchOptions || {}, params.contextOptions || {}, params.device, params.saveStorage, params.actionListener);
generator.on('change', () => {
this._recorderSources = [];
for (const languageGenerator of this._orderedLanguages) {
Expand Down Expand Up @@ -491,6 +502,10 @@ class ContextRecorder extends EventEmitter {
eventsHelper.removeEventListeners(this._listeners);
}

emitSelector(selector: string) {
this._params.actionListener?.emit('selector', selector);
}

private async _onPage(page: Page) {
// First page is called page, others are called popup1, popup2, etc.
const frame = page.mainFrame();
Expand Down
6 changes: 4 additions & 2 deletions packages/playwright-core/src/server/recorder/codeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export class CodeGenerator extends EventEmitter {
private _enabled: boolean;
private _options: LanguageGeneratorOptions;

constructor(browserName: string, enabled: boolean, launchOptions: LaunchOptions, contextOptions: BrowserContextOptions, deviceName: string | undefined, saveStorage: string | undefined) {
constructor(browserName: string, enabled: boolean, launchOptions: LaunchOptions, contextOptions: BrowserContextOptions, deviceName: string | undefined, saveStorage: string | undefined, actionListener: EventEmitter | undefined) {
super();

// Make a copy of options to modify them later.
launchOptions = { headless: false, ...launchOptions };
contextOptions = { ...contextOptions };
this._enabled = enabled;
this._options = { browserName, launchOptions, contextOptions, deviceName, saveStorage };
this._options = { browserName, launchOptions, contextOptions, deviceName, saveStorage, actionListener };
this.restart();
}

Expand Down Expand Up @@ -111,6 +111,7 @@ export class CodeGenerator extends EventEmitter {
this._actions.pop();
this._actions.push(actionInContext);
this.emit('change');
this._options.actionListener?.emit('actions', this._actions);
}

commitLastAction() {
Expand Down Expand Up @@ -138,6 +139,7 @@ export class CodeGenerator extends EventEmitter {
signals.length = signals.length - 1;
this._lastAction.action.signals.push(signal);
this.emit('change');
this._options.actionListener?.emit('actions', this._actions);
return;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/playwright-core/src/server/recorder/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import { EventEmitter } from 'stream';
import type { BrowserContextOptions, LaunchOptions } from '../../..';
import type { Language } from '../../utils/isomorphic/locatorGenerators';
import type { ActionInContext } from './codeGenerator';
Expand All @@ -26,6 +27,7 @@ export type LanguageGeneratorOptions = {
contextOptions: BrowserContextOptions;
deviceName?: string;
saveStorage?: string;
actionListener?: EventEmitter;
};

export type LocatorType = 'default' | 'role' | 'text' | 'label' | 'placeholder' | 'alt' | 'title' | 'test-id' | 'nth' | 'first' | 'last' | 'has-text';
Expand Down
4 changes: 4 additions & 0 deletions packages/protocol/src/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,8 @@ export type BrowserContextRecorderSupplementEnableParams = {
outputFile?: string,
handleSIGINT?: boolean,
omitCallTracking?: boolean,
showRecorder?: boolean,
actionListener?: any,
};
export type BrowserContextRecorderSupplementEnableOptions = {
language?: string,
Expand All @@ -1678,6 +1680,8 @@ export type BrowserContextRecorderSupplementEnableOptions = {
outputFile?: string,
handleSIGINT?: boolean,
omitCallTracking?: boolean,
showRecorder?: boolean,
actionListener?: any,
};
export type BrowserContextRecorderSupplementEnableResult = void;
export type BrowserContextNewCDPSessionParams = {
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,8 @@ BrowserContext:
outputFile: string?
handleSIGINT: boolean?
omitCallTracking: boolean?
showRecorder: boolean?
actionListener: json?

newCDPSession:
parameters:
Expand Down

0 comments on commit 7f80cfc

Please sign in to comment.