fix(bidi): initialize utility script before adding bindings (#35816)

This commit is contained in:
Yury Semikhatsky 2025-04-30 13:09:10 -07:00 committed by GitHub
parent c75b592198
commit cb99e260fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 35 deletions

View File

@ -21,6 +21,7 @@ import * as network from '../network';
import { BidiConnection } from './bidiConnection';
import { bidiBytesValueToString } from './bidiNetworkManager';
import { addMainBindingSource, BidiPage, kPlaywrightBindingChannel } from './bidiPage';
import { kUtilityInitScript } from '../page';
import * as bidi from './third_party/bidiProtocol';
import type { RegisteredListener } from '../utils/eventsHelper';
@ -220,6 +221,7 @@ export class BidiBrowserContext extends BrowserContext {
const promises: Promise<any>[] = [
super._initialize(),
this._installMainBinding(),
this._installUtilityScript(),
];
if (this._options.viewport) {
promises.push(this._browser._browserSession.send('browsingContext.setViewport', {
@ -252,6 +254,13 @@ export class BidiBrowserContext extends BrowserContext {
});
}
private async _installUtilityScript() {
await this._browser._browserSession.send('script.addPreloadScript', {
functionDeclaration: `() => { return${kUtilityInitScript.source} }`,
userContexts: [this._userContextId()],
});
}
override possiblyUninitializedPages(): Page[] {
return this._bidiPages().map(bidiPage => bidiPage._page);
}

View File

@ -93,15 +93,9 @@ export class BidiPage implements PageDelegate {
await Promise.all([
this.updateHttpCredentials(),
this.updateRequestInterception(),
this._installMainBinding(),
this._addAllInitScripts(),
]);
}
private async _addAllInitScripts() {
return Promise.all(this._page.allInitScripts().map(initScript => this.addInitScript(initScript)));
}
didClose() {
this._session.dispose();
eventsHelper.removeEventListeners(this._sessionListeners);
@ -323,35 +317,6 @@ export class BidiPage implements PageDelegate {
throw new Error('Method not implemented.');
}
// TODO: consider calling this only when bindings are added.
// TODO: delete this method once we can add preload script for persistent context.
private async _installMainBinding() {
// For non-persistent context, the main binding is installed during context creation.
if (this._browserContext._browserContextId)
return;
const functionDeclaration = addMainBindingSource;
const args: bidi.Script.ChannelValue[] = [{
type: 'channel',
value: {
channel: kPlaywrightBindingChannel,
ownership: bidi.Script.ResultOwnership.Root,
}
}];
const promises = [];
promises.push(this._session.send('script.addPreloadScript', {
functionDeclaration,
arguments: args,
}));
promises.push(this._session.send('script.callFunction', {
functionDeclaration,
arguments: args,
target: toBidiExecutionContext(await this._page.mainFrame()._mainContext())._target,
awaitPromise: false,
userActivation: false,
}));
await Promise.all(promises);
}
private async _onScriptMessage(event: bidi.Script.MessageParameters) {
if (event.channel !== kPlaywrightBindingChannel)
return;