Skip to content

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

chore: use utilityScript handle in bindings (#35824)

Dmitry Gozman42ade54975f6
diff --git a/packages/playwright-core/src/server/page.ts b/packages/playwright-core/src/server/page.ts
index 9a93f5860e..487c2a700b 100644
--- a/packages/playwright-core/src/server/page.ts
+++ b/packages/playwright-core/src/server/page.ts
@@ -48,7 +48,7 @@ import type * as types from './types';
import type { TimeoutOptions } from '../utils/isomorphic/types';
import type { ImageComparatorOptions } from './utils/comparators';
import type * as channels from '@protocol/channels';
-import type { BindingPayload } from '@injected/utilityScript';
+import type { BindingPayload, UtilityScript } from '@injected/utilityScript';
export interface PageDelegate {
readonly rawMouse: input.RawMouse;
@@ -867,14 +867,16 @@ export class PageBinding {
static async dispatch(page: Page, payload: string, context: dom.FrameExecutionContext) {
const { name, seq, serializedArgs } = JSON.parse(payload) as BindingPayload;
+ let utilityScript: js.JSHandle<UtilityScript> | undefined;
try {
+ utilityScript = await context.utilityScript();
assert(context.world);
const binding = page.getBinding(name);
if (!binding)
throw new Error(`Function "${name}" is not exposed`);
let result: any;
if (binding.needsHandle) {
- const handle = await context.evaluateExpressionHandle(`arg => ${js.accessUtilityScript()}.takeBindingHandle(arg)`, { isFunction: true }, { name, seq }).catch(e => null);
+ const handle = await utilityScript.evaluateHandle((utility, arg) => utility.takeBindingHandle(arg), { name, seq }).catch(e => null);
result = await binding.playwrightFunction({ frame: context.frame, page, context: page.browserContext }, handle);
} else {
if (!Array.isArray(serializedArgs))
@@ -882,9 +884,9 @@ export class PageBinding {
const args = serializedArgs!.map(a => parseEvaluationResultValue(a));
result = await binding.playwrightFunction({ frame: context.frame, page, context: page.browserContext }, ...args);
}
- context.evaluateExpressionHandle(`arg => ${js.accessUtilityScript()}.deliverBindingResult(arg)`, { isFunction: true }, { name, seq, result }).catch(e => debugLogger.log('error', e));
+ utilityScript.evaluate((utility, arg) => utility.deliverBindingResult(arg), { name, seq, result }).catch(e => debugLogger.log('error', e));
} catch (error) {
- context.evaluateExpressionHandle(`arg => ${js.accessUtilityScript()}.deliverBindingResult(arg)`, { isFunction: true }, { name, seq, error }).catch(e => debugLogger.log('error', e));
+ utilityScript?.evaluate((utility, arg) => utility.deliverBindingResult(arg), { name, seq, error }).catch(e => debugLogger.log('error', e));
}
}
}
diff --git a/tests/library/browsercontext-expose-function.spec.ts b/tests/library/browsercontext-expose-function.spec.ts
index 4786cabede..c242c8582e 100644
--- a/tests/library/browsercontext-expose-function.spec.ts
+++ b/tests/library/browsercontext-expose-function.spec.ts
@@ -66,11 +66,11 @@ it('should be callable from-inside addInitScript', async ({ context, server }) =
await context.addInitScript('window["woof"]("context")');
const page = await context.newPage();
await page.evaluate('undefined');
- expect(args).toEqual(['context']);
+ await expect.poll(() => args).toEqual(['context']);
args = [];
await page.addInitScript('window["woof"]("page")');
await page.reload();
- expect(args).toEqual(['context', 'page']);
+ await expect.poll(() => args).toEqual(['context', 'page']);
});
it('exposeBindingHandle should work', async ({ context }) => {
diff --git a/tests/library/popup.spec.ts b/tests/library/popup.spec.ts
index 495cf5d7cc..f4774a7567 100644
--- a/tests/library/popup.spec.ts
+++ b/tests/library/popup.spec.ts
@@ -221,31 +221,25 @@ it('should expose function from browser context', async function({ browser, serv
it('should not dispatch binding on a closed page', async function({ browser, server, browserName }) {
const context = await browser.newContext();
- const messages = [];
- await context.exposeFunction('add', (a, b) => {
- messages.push('binding');
+ let wasClosed: boolean | undefined;
+ await context.exposeBinding('add', (source, a, b) => {
+ wasClosed = source.page.isClosed();
return a + b;
});
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
await Promise.all([
- page.waitForEvent('popup').then(popup => {
- if (popup.isClosed())
- messages.push('close');
- else
- return popup.waitForEvent('close').then(() => messages.push('close'));
- }),
+ page.waitForEvent('popup'),
page.evaluate(async () => {
const win = window.open('about:blank');
win['add'](9, 4);
win.close();
}),
]);
+ // Give it a chance to dispatch the binding on the closed page.
+ await page.waitForTimeout(1000);
await context.close();
- if (browserName === 'firefox')
- expect(messages.join('|')).toBe('close');
- else
- expect(messages.join('|')).toBe('binding|close');
+ expect(wasClosed).not.toBeTruthy();
});
it('should not throttle rAF in the opener page', async ({ page, server }) => {
diff --git a/tests/page/page-expose-function.spec.ts b/tests/page/page-expose-function.spec.ts
index d6152c2b53..7af31a5785 100644
--- a/tests/page/page-expose-function.spec.ts
+++ b/tests/page/page-expose-function.spec.ts
@@ -95,7 +95,7 @@ it('should be callable from-inside addInitScript', async ({ page, server }) => {
});
await page.addInitScript(() => window['woof']());
await page.reload();
- expect(called).toBe(true);
+ await expect.poll(() => called).toBe(true);
});
it('should survive navigation', async ({ page, server }) => {