mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
fix(bidi): initialize utility script before adding bindings (#35816)
This commit is contained in:
parent
c75b592198
commit
cb99e260fa
@ -21,6 +21,7 @@ import * as network from '../network';
|
|||||||
import { BidiConnection } from './bidiConnection';
|
import { BidiConnection } from './bidiConnection';
|
||||||
import { bidiBytesValueToString } from './bidiNetworkManager';
|
import { bidiBytesValueToString } from './bidiNetworkManager';
|
||||||
import { addMainBindingSource, BidiPage, kPlaywrightBindingChannel } from './bidiPage';
|
import { addMainBindingSource, BidiPage, kPlaywrightBindingChannel } from './bidiPage';
|
||||||
|
import { kUtilityInitScript } from '../page';
|
||||||
import * as bidi from './third_party/bidiProtocol';
|
import * as bidi from './third_party/bidiProtocol';
|
||||||
|
|
||||||
import type { RegisteredListener } from '../utils/eventsHelper';
|
import type { RegisteredListener } from '../utils/eventsHelper';
|
||||||
@ -220,6 +221,7 @@ export class BidiBrowserContext extends BrowserContext {
|
|||||||
const promises: Promise<any>[] = [
|
const promises: Promise<any>[] = [
|
||||||
super._initialize(),
|
super._initialize(),
|
||||||
this._installMainBinding(),
|
this._installMainBinding(),
|
||||||
|
this._installUtilityScript(),
|
||||||
];
|
];
|
||||||
if (this._options.viewport) {
|
if (this._options.viewport) {
|
||||||
promises.push(this._browser._browserSession.send('browsingContext.setViewport', {
|
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[] {
|
override possiblyUninitializedPages(): Page[] {
|
||||||
return this._bidiPages().map(bidiPage => bidiPage._page);
|
return this._bidiPages().map(bidiPage => bidiPage._page);
|
||||||
}
|
}
|
||||||
|
@ -93,15 +93,9 @@ export class BidiPage implements PageDelegate {
|
|||||||
await Promise.all([
|
await Promise.all([
|
||||||
this.updateHttpCredentials(),
|
this.updateHttpCredentials(),
|
||||||
this.updateRequestInterception(),
|
this.updateRequestInterception(),
|
||||||
this._installMainBinding(),
|
|
||||||
this._addAllInitScripts(),
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _addAllInitScripts() {
|
|
||||||
return Promise.all(this._page.allInitScripts().map(initScript => this.addInitScript(initScript)));
|
|
||||||
}
|
|
||||||
|
|
||||||
didClose() {
|
didClose() {
|
||||||
this._session.dispose();
|
this._session.dispose();
|
||||||
eventsHelper.removeEventListeners(this._sessionListeners);
|
eventsHelper.removeEventListeners(this._sessionListeners);
|
||||||
@ -323,35 +317,6 @@ export class BidiPage implements PageDelegate {
|
|||||||
throw new Error('Method not implemented.');
|
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) {
|
private async _onScriptMessage(event: bidi.Script.MessageParameters) {
|
||||||
if (event.channel !== kPlaywrightBindingChannel)
|
if (event.channel !== kPlaywrightBindingChannel)
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user