Skip to content

Commit 345f7da

Browse files
authored
fix(codegen): move injected recorder scripts to utility world (#6187)
1 parent b52cbfd commit 345f7da

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

src/dispatchers/browserContextDispatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
100100
const binding = new BindingCallDispatcher(this._scope, params.name, !!params.needsHandle, source, args);
101101
this._dispatchEvent('bindingCall', { binding });
102102
return binding.promise();
103-
});
103+
}, 'main');
104104
}
105105

106106
async newPage(params: channels.BrowserContextNewPageParams, metadata: CallMetadata): Promise<channels.BrowserContextNewPageResult> {

src/server/browserContext.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export abstract class BrowserContext extends SdkObject {
108108
});
109109

110110
if (debugMode() === 'console')
111-
await this.extendInjectedScript(consoleApiSource.source);
111+
await this.extendInjectedScript('main', consoleApiSource.source);
112112
}
113113

114114
async _ensureVideosPath() {
@@ -163,15 +163,15 @@ export abstract class BrowserContext extends SdkObject {
163163
return this._doSetHTTPCredentials(httpCredentials);
164164
}
165165

166-
async exposeBinding(name: string, needsHandle: boolean, playwrightBinding: frames.FunctionWithSource): Promise<void> {
167-
const identifier = PageBinding.identifier(name, 'main');
166+
async exposeBinding(name: string, needsHandle: boolean, playwrightBinding: frames.FunctionWithSource, world: types.World): Promise<void> {
167+
const identifier = PageBinding.identifier(name, world);
168168
if (this._pageBindings.has(identifier))
169169
throw new Error(`Function "${name}" has been already registered`);
170170
for (const page of this.pages()) {
171-
if (page.getBinding(name, 'main'))
171+
if (page.getBinding(name, world))
172172
throw new Error(`Function "${name}" has been already registered in one of the pages`);
173173
}
174-
const binding = new PageBinding(name, playwrightBinding, needsHandle, 'main');
174+
const binding = new PageBinding(name, playwrightBinding, needsHandle, world);
175175
this._pageBindings.set(identifier, binding);
176176
await this._doExposeBinding(binding);
177177
}
@@ -369,8 +369,8 @@ export abstract class BrowserContext extends SdkObject {
369369
}
370370
}
371371

372-
async extendInjectedScript(source: string, arg?: any) {
373-
const installInFrame = (frame: frames.Frame) => frame.extendInjectedScript(source, arg).catch(() => {});
372+
async extendInjectedScript(world: types.World, source: string, arg?: any) {
373+
const installInFrame = (frame: frames.Frame) => frame.extendInjectedScript(world, source, arg).catch(() => {});
374374
const installInPage = (page: Page) => {
375375
page.on(Page.Events.InternalFrameNavigatedToNewDocument, installInFrame);
376376
return Promise.all(page.frames().map(installInFrame));

src/server/frames.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,9 +1219,9 @@ export class Frame extends SdkObject {
12191219
this._networkIdleTimer = undefined;
12201220
}
12211221

1222-
async extendInjectedScript(source: string, arg?: any): Promise<js.JSHandle> {
1223-
const mainContext = await this._mainContext();
1224-
const injectedScriptHandle = await mainContext.injectedScript();
1222+
async extendInjectedScript(world: types.World, source: string, arg?: any): Promise<js.JSHandle> {
1223+
const context = await this._context(world);
1224+
const injectedScriptHandle = await context.injectedScript();
12251225
return injectedScriptHandle.evaluateHandle((injectedScript, {source, arg}) => {
12261226
return injectedScript.extend(source, arg);
12271227
}, { source, arg });

src/server/supplements/recorderSupplement.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ export class RecorderSupplement implements InstrumentationListener {
194194
// Input actions that potentially lead to navigation are intercepted on the page and are
195195
// performed by the Playwright.
196196
await this._context.exposeBinding('_playwrightRecorderPerformAction', false,
197-
(source: BindingSource, action: actions.Action) => this._performAction(source.frame, action));
197+
(source: BindingSource, action: actions.Action) => this._performAction(source.frame, action), 'utility');
198198

199199
// Other non-essential actions are simply being recorded.
200200
await this._context.exposeBinding('_playwrightRecorderRecordAction', false,
201-
(source: BindingSource, action: actions.Action) => this._recordAction(source.frame, action));
201+
(source: BindingSource, action: actions.Action) => this._recordAction(source.frame, action), 'utility');
202202

203203
await this._context.exposeBinding('_playwrightRecorderState', false, source => {
204204
let snapshotUrl: string | undefined;
@@ -223,21 +223,21 @@ export class RecorderSupplement implements InstrumentationListener {
223223
snapshotUrl,
224224
};
225225
return uiState;
226-
});
226+
}, 'utility');
227227

228228
await this._context.exposeBinding('_playwrightRecorderSetSelector', false, async (_, selector: string) => {
229229
this._setMode('none');
230230
await this._recorderApp?.setSelector(selector, true);
231231
await this._recorderApp?.bringToFront();
232-
});
232+
}, 'utility');
233233

234234
await this._context.exposeBinding('_playwrightResume', false, () => {
235235
this._debugger.resume(false);
236-
});
236+
}, 'main');
237237

238238
const snapshotBaseUrl = await this._snapshotter.initialize() + '/snapshot/';
239-
await this._context.extendInjectedScript(recorderSource.source, { isUnderTest: isUnderTest(), snapshotBaseUrl });
240-
await this._context.extendInjectedScript(consoleApiSource.source);
239+
await this._context.extendInjectedScript('utility', recorderSource.source, { isUnderTest: isUnderTest(), snapshotBaseUrl });
240+
await this._context.extendInjectedScript('main', consoleApiSource.source);
241241

242242
if (this._debugger.isPaused())
243243
this._pausedStateChanged();

src/server/trace/viewer/traceViewer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class TraceViewer {
138138
await controller.run(async progress => {
139139
await context._browser._defaultContext!._loadDefaultContextAsIs(progress);
140140
});
141-
await context.extendInjectedScript(consoleApiSource.source);
141+
await context.extendInjectedScript('main', consoleApiSource.source);
142142
const [page] = context.pages();
143143
page.on('close', () => process.exit(0));
144144
await page.mainFrame().goto(internalCallMetadata(), urlPrefix + '/traceviewer/traceViewer/index.html');

0 commit comments

Comments
 (0)