From 52a276ba3824771aed988409428c9b029f22bcea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sun, 15 Nov 2026 04:08:25 +0100 Subject: [PATCH 1/3] Evaluate waitForFunction() predicates in the page Playwright calls a function on each poll but only evaluates a string. The frame and locator handlers forwarded the source as a string, so a function such as '() => false' was a truthy value and the wait ended at once. The page handler turned the source into a function in the server process, so an expression Node could evaluate never reached the page. The three handlers now hand Playwright a predicate that evaluates the source in the page and calls the result when it is a function. Fixes #213 --- CHANGELOG.md | 2 ++ bin/lib/handlers.js | 34 +++++++++++-------- .../Frame/FrameIntegrationTest.php | 34 +++++++++++++++++++ tests/Integration/Locator/LocatorTest.php | 22 ++++++++++++ tests/Integration/Page/PageTest.php | 10 ++++++ 5 files changed, 88 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a63c350..e33f003 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ - Screenshots taken without a path use the extension of their `type` instead of always `.png` (#207) - The `mask` option of `Page::screenshot()` and `Locator::screenshot()` covers the given locators instead of failing in the driver; other values throw `InvalidArgumentException` (#209) - A locator assertion on a locator that resolves to several elements fails at once with Playwright's strict mode message instead of waiting for its timeout (#226) +- `Frame::waitForFunction()` and `Locator::waitForFunction()` call a function given as a string and wait for its result instead of returning at once +- `Page::waitForFunction()` evaluates an expression in the page, never in the server process ## [1.6.0] - 2026-11-07 diff --git a/bin/lib/handlers.js b/bin/lib/handlers.js index 7f05b18..b15a62d 100644 --- a/bin/lib/handlers.js +++ b/bin/lib/handlers.js @@ -70,6 +70,23 @@ const evaluateHandleOnTarget = async (target, { expression, arg }) => { return typeof value === 'function' ? await value(target, arg) : await value; }; +// Predicates handed to waitForFunction() in place of the caller's source. +// Playwright calls a function on each poll but only evaluates a string, so a +// function sent as a string would never be called. These run in the page: +// they evaluate the source there and call the result when it is a function. +// Playwright serializes them, so they cannot use anything from this file. +const waitPredicateInPage = ({ source, arg }) => { + const text = source.trim(); + const value = globalThis.eval(/^(async)?\s*function(\s|\()/.test(text) ? `(${text})` : text); + return typeof value === 'function' ? value(arg) : value; +}; + +const waitPredicateOnElement = (element, { source, arg }) => { + const text = source.trim(); + const value = globalThis.eval(/^(async)?\s*function(\s|\()/.test(text) ? `(${text})` : text); + return typeof value === 'function' ? value(element, arg) : value; +}; + // A HAR url filter accepts a glob or a regex. A glob may legitimately start with // a slash, so PHP sends the regex form under its own key as a "/source/flags" // literal rather than leaving the two shapes to be told apart here. @@ -520,14 +537,7 @@ class PageHandler extends BaseHandler { async waitForFunction(page, command) { try { - let func; - try { - func = eval(`(${command.pageFunction})`); - } catch (e) { - func = command.pageFunction; - } - - await page.waitForFunction(func, command.arg, command.options); + await page.waitForFunction(waitPredicateInPage, { source: command.pageFunction, arg: command.arg }, command.options); } catch (error) { logger.error('PAGE WAITFORFUNCTION ERROR', { message: error.message }); throw error; @@ -975,9 +985,7 @@ class LocatorHandler extends BaseHandler { } async waitForFunction(locator, command) { - // Forwarded as a string: Playwright evaluates it in the page. Turning it - // into a function here would run it in this Node process instead. - await locator.waitForFunction(command.pageFunction, command.arg, command.options); + await locator.waitForFunction(waitPredicateOnElement, { source: command.pageFunction, arg: command.arg }, command.options); return { success: true }; } @@ -1076,9 +1084,7 @@ class FrameHandler extends BaseHandler { } async waitForFunction(nativeFrame, command) { - // Forwarded as a string: Playwright evaluates it in the page. Turning it - // into a function here would run it in this Node process instead. - await (await nativeFrame()).waitForFunction(command.pageFunction, command.arg, command.options); + await (await nativeFrame()).waitForFunction(waitPredicateInPage, { source: command.pageFunction, arg: command.arg }, command.options); return { success: true }; } diff --git a/tests/Integration/Frame/FrameIntegrationTest.php b/tests/Integration/Frame/FrameIntegrationTest.php index 83b8438..853642a 100644 --- a/tests/Integration/Frame/FrameIntegrationTest.php +++ b/tests/Integration/Frame/FrameIntegrationTest.php @@ -17,6 +17,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use Playwright\Exception\PlaywrightException; use Playwright\Frame\Frame; use Playwright\Testing\PlaywrightTestCaseTrait; use Playwright\Tests\Support\RouteServerTestTrait; @@ -154,6 +155,39 @@ public function itWaitsForANativeFrameFunction(): void )); } + #[Test] + public function itKeepsWaitingWhileAFrameFunctionReturnsFalse(): void + { + $this->expectException(PlaywrightException::class); + $this->expectExceptionMessage('Timeout 200ms exceeded'); + + $this->innerFrame()->waitForFunction('() => false', null, ['timeout' => 200]); + } + + #[Test] + public function itPassesTheArgumentToAFrameFunction(): void + { + $frame = $this->innerFrame(); + + $this->assertSame($frame, $frame->waitForFunction( + '(text) => document.querySelector("h4").textContent === text', + 'Inner', + ['timeout' => 1000], + )); + } + + #[Test] + public function itWaitsForAFrameExpression(): void + { + $frame = $this->innerFrame(); + + $this->assertSame($frame, $frame->waitForFunction( + 'document.querySelector("h4").textContent === "Inner"', + null, + ['timeout' => 1000], + )); + } + #[Test] public function itWaitsForANativeFrameUrl(): void { diff --git a/tests/Integration/Locator/LocatorTest.php b/tests/Integration/Locator/LocatorTest.php index cc554ea..26923df 100644 --- a/tests/Integration/Locator/LocatorTest.php +++ b/tests/Integration/Locator/LocatorTest.php @@ -239,6 +239,28 @@ public function itWaitsForAFunctionAgainstTheMatchedElement(): void )); } + #[Test] + public function itKeepsWaitingWhileALocatorFunctionReturnsFalse(): void + { + $this->expectException(PlaywrightException::class); + $this->expectExceptionMessage('Timeout 200ms exceeded'); + + $this->page->locator('#div-1')->waitForFunction('(element) => false', null, ['timeout' => 200]); + } + + #[Test] + public function itPassesTheElementAndTheArgumentToALocatorFunction(): void + { + $this->page->setContent('
ready
'); + $locator = $this->page->locator('#status'); + + $this->assertSame($locator, $locator->waitForFunction( + '(element, text) => element.textContent === text', + 'ready', + ['timeout' => 1000], + )); + } + #[Test] public function itExposesThePageItResolvesAgainst(): void { diff --git a/tests/Integration/Page/PageTest.php b/tests/Integration/Page/PageTest.php index 7cd7f2e..91efade 100644 --- a/tests/Integration/Page/PageTest.php +++ b/tests/Integration/Page/PageTest.php @@ -425,6 +425,16 @@ public function itWaitsForFunctionWithArgument(): void $this->assertSame('ready', $this->page->evaluate('() => document.querySelector("#status").textContent')); } + #[Test] + public function itEvaluatesAWaitForFunctionExpressionInThePage(): void + { + $this->assertSame($this->page, $this->page->waitForFunction( + 'typeof window !== "undefined" && typeof process === "undefined"', + null, + ['timeout' => 300] + )); + } + #[Test] public function itThrowsExceptionOnWaitForFunctionTimeout(): void { From a61273ed9d0bbe2038e48405a0b5d5e9c5ec34f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sun, 15 Nov 2026 15:26:45 +0100 Subject: [PATCH 2/3] Reference #215 in the changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e33f003..392cfaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,8 +17,8 @@ - Screenshots taken without a path use the extension of their `type` instead of always `.png` (#207) - The `mask` option of `Page::screenshot()` and `Locator::screenshot()` covers the given locators instead of failing in the driver; other values throw `InvalidArgumentException` (#209) - A locator assertion on a locator that resolves to several elements fails at once with Playwright's strict mode message instead of waiting for its timeout (#226) -- `Frame::waitForFunction()` and `Locator::waitForFunction()` call a function given as a string and wait for its result instead of returning at once -- `Page::waitForFunction()` evaluates an expression in the page, never in the server process +- `Frame::waitForFunction()` and `Locator::waitForFunction()` call a function given as a string and wait for its result instead of returning at once (#215) +- `Page::waitForFunction()` evaluates an expression in the page, never in the server process (#215) ## [1.6.0] - 2026-11-07 From 9071bf52e9693696a44d31237b87dedc4bd87091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sun, 15 Nov 2026 17:31:18 +0100 Subject: [PATCH 3/3] Evaluate a waitForFunction() function source once --- bin/lib/handlers.js | 36 +++++++++++++------ .../Frame/FrameIntegrationTest.php | 14 ++++++++ tests/Integration/Page/PageTest.php | 13 +++++++ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/bin/lib/handlers.js b/bin/lib/handlers.js index b15a62d..797bebe 100644 --- a/bin/lib/handlers.js +++ b/bin/lib/handlers.js @@ -70,21 +70,35 @@ const evaluateHandleOnTarget = async (target, { expression, arg }) => { return typeof value === 'function' ? await value(target, arg) : await value; }; -// Predicates handed to waitForFunction() in place of the caller's source. -// Playwright calls a function on each poll but only evaluates a string, so a -// function sent as a string would never be called. These run in the page: -// they evaluate the source there and call the result when it is a function. -// Playwright serializes them, so they cannot use anything from this file. -const waitPredicateInPage = ({ source, arg }) => { - const text = source.trim(); +// Predicates handed to waitForFunction() in place of the caller's source, which +// Playwright would only evaluate, never call. They run in the page, so they +// cannot use anything from this file. A function source is evaluated once and +// kept on the argument: a Content-Security-Policy without 'unsafe-eval' allows +// eval() on the first poll only. +const waitPredicateInPage = (state) => { + if (typeof state.predicate === 'function') { + return state.predicate(state.arg); + } + const text = state.source.trim(); const value = globalThis.eval(/^(async)?\s*function(\s|\()/.test(text) ? `(${text})` : text); - return typeof value === 'function' ? value(arg) : value; + if (typeof value !== 'function') { + return value; + } + state.predicate = value; + return value(state.arg); }; -const waitPredicateOnElement = (element, { source, arg }) => { - const text = source.trim(); +const waitPredicateOnElement = (element, state) => { + if (typeof state.predicate === 'function') { + return state.predicate(element, state.arg); + } + const text = state.source.trim(); const value = globalThis.eval(/^(async)?\s*function(\s|\()/.test(text) ? `(${text})` : text); - return typeof value === 'function' ? value(element, arg) : value; + if (typeof value !== 'function') { + return value; + } + state.predicate = value; + return value(element, state.arg); }; // A HAR url filter accepts a glob or a regex. A glob may legitimately start with diff --git a/tests/Integration/Frame/FrameIntegrationTest.php b/tests/Integration/Frame/FrameIntegrationTest.php index 853642a..3671ce3 100644 --- a/tests/Integration/Frame/FrameIntegrationTest.php +++ b/tests/Integration/Frame/FrameIntegrationTest.php @@ -164,6 +164,20 @@ public function itKeepsWaitingWhileAFrameFunctionReturnsFalse(): void $this->innerFrame()->waitForFunction('() => false', null, ['timeout' => 200]); } + #[Test] + public function itKeepsPollingAFrameFunctionUnderAContentSecurityPolicyWithoutUnsafeEval(): void + { + $frame = $this->innerFrame(); + $frame->setContent('

Strict

'); + $frame->evaluate('() => { window.ready = false; setTimeout(() => { window.ready = true; }, 200); }'); + + $this->assertSame($frame, $frame->waitForFunction( + '(expected) => window.ready === expected', + true, + ['timeout' => 2000], + )); + } + #[Test] public function itPassesTheArgumentToAFrameFunction(): void { diff --git a/tests/Integration/Page/PageTest.php b/tests/Integration/Page/PageTest.php index 91efade..1c38d21 100644 --- a/tests/Integration/Page/PageTest.php +++ b/tests/Integration/Page/PageTest.php @@ -425,6 +425,19 @@ public function itWaitsForFunctionWithArgument(): void $this->assertSame('ready', $this->page->evaluate('() => document.querySelector("#status").textContent')); } + #[Test] + public function itKeepsPollingAFunctionUnderAContentSecurityPolicyWithoutUnsafeEval(): void + { + $this->page->setContent('

Strict

'); + $this->page->evaluate('() => { window.ready = false; setTimeout(() => { window.ready = true; }, 200); }'); + + $this->assertSame($this->page, $this->page->waitForFunction( + '() => window.ready === true', + null, + ['timeout' => 2000] + )); + } + #[Test] public function itEvaluatesAWaitForFunctionExpressionInThePage(): void {