Skip to content

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

fix(chromium): install binding function during initialization (#1320)

Dmitry Gozman3dd49459bba3
diff --git a/src/chromium/crPage.ts b/src/chromium/crPage.ts
index 1830df6f84..b342eaf44f 100644
--- a/src/chromium/crPage.ts
+++ b/src/chromium/crPage.ts
@@ -92,11 +92,16 @@ export class CRPage implements PageDelegate {
helper.addEventListener(this._client, 'Target.attachedToTarget', event => this._onAttachedToTarget(event)),
helper.addEventListener(this._client, 'Target.detachedFromTarget', event => this._onDetachedFromTarget(event)),
];
- this._page.frames().map(frame => this._client.send('Page.createIsolatedWorld', {
- frameId: frame._id,
- grantUniveralAccess: true,
- worldName: UTILITY_WORLD_NAME,
- }).catch(debugError)); // frames might be removed before we send this.
+ for (const frame of this._page.frames()) {
+ // Note: frames might be removed before we send these.
+ this._client.send('Page.createIsolatedWorld', {
+ frameId: frame._id,
+ grantUniveralAccess: true,
+ worldName: UTILITY_WORLD_NAME,
+ }).catch(debugError);
+ for (const binding of this._browserContext._pageBindings.values())
+ frame.evaluate(binding.source).catch(debugError);
+ }
}),
this._client.send('Log.enable', {}),
this._client.send('Page.setLifecycleEventsEnabled', { enabled: true }),
diff --git a/src/page.ts b/src/page.ts
index 2658f627aa..a66bb65a65 100644
--- a/src/page.ts
+++ b/src/page.ts
@@ -612,6 +612,8 @@ export class PageBinding {
function addPageBinding(bindingName: string) {
const binding = (window as any)[bindingName];
+ if (binding.__installed)
+ return;
(window as any)[bindingName] = (...args: any[]) => {
const me = (window as any)[bindingName];
let callbacks = me['callbacks'];
@@ -625,4 +627,5 @@ function addPageBinding(bindingName: string) {
binding(JSON.stringify({name: bindingName, seq, args}));
return promise;
};
+ (window as any)[bindingName].__installed = true;
}
diff --git a/test/browsercontext.spec.js b/test/browsercontext.spec.js
index f7c4c80138..019f965b45 100644
--- a/test/browsercontext.spec.js
+++ b/test/browsercontext.spec.js
@@ -326,7 +326,7 @@ module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, FF
});
describe('BrowserContext.exposeFunction', () => {
- it.fail(CHROMIUM || FFOX)('should work', async({browser, server}) => {
+ it.fail(FFOX)('should work', async({browser, server}) => {
const context = await browser.newContext();
await context.exposeFunction('add', (a, b) => a + b);
const page = await context.newPage();