Skip to content

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

fix(codegen): move injected recorder scripts to utility world (#6187)

Yury Semikhatsky345f7da57349
diff --git a/src/dispatchers/browserContextDispatcher.ts b/src/dispatchers/browserContextDispatcher.ts
index 231aa59323..535e15c6c4 100644
--- a/src/dispatchers/browserContextDispatcher.ts
+++ b/src/dispatchers/browserContextDispatcher.ts
@@ -100,7 +100,7 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
const binding = new BindingCallDispatcher(this._scope, params.name, !!params.needsHandle, source, args);
this._dispatchEvent('bindingCall', { binding });
return binding.promise();
- });
+ }, 'main');
}
async newPage(params: channels.BrowserContextNewPageParams, metadata: CallMetadata): Promise<channels.BrowserContextNewPageResult> {
diff --git a/src/server/browserContext.ts b/src/server/browserContext.ts
index 0849dfd968..6e08002b5a 100644
--- a/src/server/browserContext.ts
+++ b/src/server/browserContext.ts
@@ -108,7 +108,7 @@ export abstract class BrowserContext extends SdkObject {
});
if (debugMode() === 'console')
- await this.extendInjectedScript(consoleApiSource.source);
+ await this.extendInjectedScript('main', consoleApiSource.source);
}
async _ensureVideosPath() {
@@ -163,15 +163,15 @@ export abstract class BrowserContext extends SdkObject {
return this._doSetHTTPCredentials(httpCredentials);
}
- async exposeBinding(name: string, needsHandle: boolean, playwrightBinding: frames.FunctionWithSource): Promise<void> {
- const identifier = PageBinding.identifier(name, 'main');
+ async exposeBinding(name: string, needsHandle: boolean, playwrightBinding: frames.FunctionWithSource, world: types.World): Promise<void> {
+ const identifier = PageBinding.identifier(name, world);
if (this._pageBindings.has(identifier))
throw new Error(`Function "${name}" has been already registered`);
for (const page of this.pages()) {
- if (page.getBinding(name, 'main'))
+ if (page.getBinding(name, world))
throw new Error(`Function "${name}" has been already registered in one of the pages`);
}
- const binding = new PageBinding(name, playwrightBinding, needsHandle, 'main');
+ const binding = new PageBinding(name, playwrightBinding, needsHandle, world);
this._pageBindings.set(identifier, binding);
await this._doExposeBinding(binding);
}
@@ -369,8 +369,8 @@ export abstract class BrowserContext extends SdkObject {
}
}
- async extendInjectedScript(source: string, arg?: any) {
- const installInFrame = (frame: frames.Frame) => frame.extendInjectedScript(source, arg).catch(() => {});
+ async extendInjectedScript(world: types.World, source: string, arg?: any) {
+ const installInFrame = (frame: frames.Frame) => frame.extendInjectedScript(world, source, arg).catch(() => {});
const installInPage = (page: Page) => {
page.on(Page.Events.InternalFrameNavigatedToNewDocument, installInFrame);
return Promise.all(page.frames().map(installInFrame));
diff --git a/src/server/frames.ts b/src/server/frames.ts
index f7e36462a5..e16ee9899e 100644
--- a/src/server/frames.ts
+++ b/src/server/frames.ts
@@ -1219,9 +1219,9 @@ export class Frame extends SdkObject {
this._networkIdleTimer = undefined;
}
- async extendInjectedScript(source: string, arg?: any): Promise<js.JSHandle> {
- const mainContext = await this._mainContext();
- const injectedScriptHandle = await mainContext.injectedScript();
+ async extendInjectedScript(world: types.World, source: string, arg?: any): Promise<js.JSHandle> {
+ const context = await this._context(world);
+ const injectedScriptHandle = await context.injectedScript();
return injectedScriptHandle.evaluateHandle((injectedScript, {source, arg}) => {
return injectedScript.extend(source, arg);
}, { source, arg });
diff --git a/src/server/supplements/recorderSupplement.ts b/src/server/supplements/recorderSupplement.ts
index 15e3199caa..ee19e7f1da 100644
--- a/src/server/supplements/recorderSupplement.ts
+++ b/src/server/supplements/recorderSupplement.ts
@@ -194,11 +194,11 @@ export class RecorderSupplement implements InstrumentationListener {
// Input actions that potentially lead to navigation are intercepted on the page and are
// performed by the Playwright.
await this._context.exposeBinding('_playwrightRecorderPerformAction', false,
- (source: BindingSource, action: actions.Action) => this._performAction(source.frame, action));
+ (source: BindingSource, action: actions.Action) => this._performAction(source.frame, action), 'utility');
// Other non-essential actions are simply being recorded.
await this._context.exposeBinding('_playwrightRecorderRecordAction', false,
- (source: BindingSource, action: actions.Action) => this._recordAction(source.frame, action));
+ (source: BindingSource, action: actions.Action) => this._recordAction(source.frame, action), 'utility');
await this._context.exposeBinding('_playwrightRecorderState', false, source => {
let snapshotUrl: string | undefined;
@@ -223,21 +223,21 @@ export class RecorderSupplement implements InstrumentationListener {
snapshotUrl,
};
return uiState;
- });
+ }, 'utility');
await this._context.exposeBinding('_playwrightRecorderSetSelector', false, async (_, selector: string) => {
this._setMode('none');
await this._recorderApp?.setSelector(selector, true);
await this._recorderApp?.bringToFront();
- });
+ }, 'utility');
await this._context.exposeBinding('_playwrightResume', false, () => {
this._debugger.resume(false);
- });
+ }, 'main');
const snapshotBaseUrl = await this._snapshotter.initialize() + '/snapshot/';
- await this._context.extendInjectedScript(recorderSource.source, { isUnderTest: isUnderTest(), snapshotBaseUrl });
- await this._context.extendInjectedScript(consoleApiSource.source);
+ await this._context.extendInjectedScript('utility', recorderSource.source, { isUnderTest: isUnderTest(), snapshotBaseUrl });
+ await this._context.extendInjectedScript('main', consoleApiSource.source);
if (this._debugger.isPaused())
this._pausedStateChanged();
diff --git a/src/server/trace/viewer/traceViewer.ts b/src/server/trace/viewer/traceViewer.ts
index 2493b8805c..8408dcd5aa 100644
--- a/src/server/trace/viewer/traceViewer.ts
+++ b/src/server/trace/viewer/traceViewer.ts
@@ -138,7 +138,7 @@ export class TraceViewer {
await controller.run(async progress => {
await context._browser._defaultContext!._loadDefaultContextAsIs(progress);
});
- await context.extendInjectedScript(consoleApiSource.source);
+ await context.extendInjectedScript('main', consoleApiSource.source);
const [page] = context.pages();
page.on('close', () => process.exit(0));
await page.mainFrame().goto(internalCallMetadata(), urlPrefix + '/traceviewer/traceViewer/index.html');