Skip to content

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

fix(waitForFunction): eval func once (#36986)

Simon Knottc0d1c2d67b90
diff --git a/packages/playwright-core/src/server/frames.ts b/packages/playwright-core/src/server/frames.ts
index b81a4dff12..b7ad114deb 100644
--- a/packages/playwright-core/src/server/frames.ts
+++ b/packages/playwright-core/src/server/frames.ts
@@ -1455,19 +1455,23 @@ export class Frame extends SdkObject {
const context = world === 'main' ? await progress.race(this._mainContext()) : await progress.race(this._utilityContext());
const injectedScript = await progress.race(context.injectedScript());
const handle = await progress.race(injectedScript.evaluateHandle((injected, { expression, isFunction, polling, arg }) => {
+ let evaledExpression: any;
const predicate = (): R => {
// NOTE: make sure to use `globalThis.eval` instead of `self.eval` due to a bug with sandbox isolation
// in firefox.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1814898
- let result = globalThis.eval(expression);
+ let result = evaledExpression ?? globalThis.eval(expression);
if (isFunction === true) {
+ evaledExpression = result;
result = result(arg);
} else if (isFunction === false) {
result = result;
} else {
// auto detect.
- if (typeof result === 'function')
+ if (typeof result === 'function') {
+ evaledExpression = result;
result = result(arg);
+ }
}
return result;
};
diff --git a/tests/page/page-wait-for-function.spec.ts b/tests/page/page-wait-for-function.spec.ts
index 98f8a327ce..8b0368cbb0 100644
--- a/tests/page/page-wait-for-function.spec.ts
+++ b/tests/page/page-wait-for-function.spec.ts
@@ -105,10 +105,10 @@ it('should work with strict CSP policy', async ({ page, server }) => {
server.setCSP('/empty.html', 'script-src ' + server.PREFIX);
await page.goto(server.EMPTY_PAGE);
let error = null;
- await Promise.all([
- page.waitForFunction(() => window['__FOO'] === 'hit', {}, { polling: 'raf' }).catch(e => error = e),
- page.evaluate(() => window['__FOO'] = 'hit')
- ]);
+ const p = page.waitForFunction(() => window['__FOO'] === 'hit', {}, { polling: 'raf' }).catch(e => error = e);
+ await page.waitForTimeout(1000);
+ await page.evaluate(() => window['__FOO'] = 'hit');
+ await p;
expect(error).toBe(null);
});