Skip to content

Commit

Permalink
chore(player): redirect player events in incognito to main recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifigueira committed Dec 21, 2024
1 parent 3a999ff commit 7cdf7ea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
13 changes: 11 additions & 2 deletions src/server/crx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { generateCode } from 'playwright-core/lib/server/codegen/language';
import { languageSet } from 'playwright-core/lib/server/codegen/languages';
import { deviceDescriptors } from 'playwright-core/lib/server/deviceDescriptors';
import type { DeviceDescriptor } from 'playwright-core/lib/server/types';
import { RecorderApp } from 'playwright-core/lib/server/recorder/recorderApp';
import { EmptyRecorderApp, RecorderApp } from 'playwright-core/lib/server/recorder/recorderApp';
import type { LanguageGeneratorOptions } from 'playwright-core/lib/server/codegen/types';

const kTabIdSymbol = Symbol('kTabIdSymbol');
Expand Down Expand Up @@ -120,7 +120,12 @@ export class Crx extends SdkObject {
});
// override factory otherwise it will fail because the default factory tries to launch a new playwright app
RecorderApp.factory = (): IRecorderAppFactory => {
return recorder => crxApp._createRecorderApp(recorder);
return async recorder => {
if (recorder instanceof Recorder && recorder._context === context)
return await crxApp._createRecorderApp(recorder);
else
return new EmptyRecorderApp();
};
};
return crxApp;
}
Expand Down Expand Up @@ -363,6 +368,10 @@ export class CrxApplication extends SdkObject {
return this._recorderApp;
}

_recorder() {
return this._recorderApp?._recorder;
}

private onWindowRemoved = async () => {
const windows = await chrome.windows.getAll();
if (this.isIncognito() && windows.every(w => !w.incognito))
Expand Down
26 changes: 22 additions & 4 deletions src/server/recorder/crxPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { toClickOptions } from 'playwright-core/lib/server/recorder/recorderRunn
import { parseAriaSnapshot } from 'playwright-core/lib/server/ariaSnapshot';
import { serverSideCallMetadata } from 'playwright-core/lib/server';
import type { Crx } from '../crx';
import type { InstrumentationListener } from 'playwright-core/lib/server/instrumentation';

class Stopped extends Error {}

Expand All @@ -53,10 +54,9 @@ export default class CrxPlayer extends EventEmitter {
this._crx = crx;
}

async pause(context?: BrowserContext) {
async pause() {
if (!this._pause) {
if (!context)
context = (await this._crx.get({ incognito: false }))!._context;
const context = (await this._crx.get({ incognito: false }))!._context;
const pauseAction = {
action: { name: 'pause' },
frame: { pageAlias: 'page', framePath: [] },
Expand Down Expand Up @@ -84,6 +84,22 @@ export default class CrxPlayer extends EventEmitter {
page = context.pages()[0] ?? await context.newPage(serverSideCallMetadata());
}

const crxApp = await this._crx.get({ incognito: false });
const recorder = crxApp?._recorder();
let instrumentationListener: InstrumentationListener | undefined;

if (recorder && crxApp && crxApp._context !== context) {
// we intercept incognito call logs and forward them into the recorder
const instrumentationListener: InstrumentationListener = {
onBeforeCall: recorder.onBeforeCall.bind(recorder),
onBeforeInputAction: recorder.onBeforeInputAction.bind(recorder),
onCallLog: recorder.onCallLog.bind(recorder),
onAfterCall: recorder.onAfterCall.bind(recorder),
};
if (instrumentationListener)
context.instrumentation.addListener(instrumentationListener, context);
}

this._pageAliases.clear();
this._pageAliases.set(page, 'page');
this.emit('start');
Expand All @@ -101,7 +117,9 @@ export default class CrxPlayer extends EventEmitter {
throw e;
} finally {
this._currAction = undefined;
this.pause(context).catch(() => {});
this.pause().catch(() => {});
if (instrumentationListener)
context.instrumentation.removeListener(instrumentationListener);
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/server/recorder/crxRecorderApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ export class CrxRecorderApp extends EventEmitter implements IRecorderApp {
});
}

setPlayInIncognito(playInIncognito: boolean) {
this._playInIncognito = playInIncognito;
}

async open(options?: channels.CrxApplicationShowRecorderParams) {
const mode = options?.mode ?? 'none';
const language = options?.language ?? 'playwright-test';
Expand Down

0 comments on commit 7cdf7ea

Please sign in to comment.