diff --git a/CHANGELOG.md b/CHANGELOG.md index a63c350..392cfaf 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 (#215) +- `Page::waitForFunction()` evaluates an expression in the page, never in the server process (#215) ## [1.6.0] - 2026-11-07 diff --git a/bin/lib/handlers.js b/bin/lib/handlers.js index 7f05b18..797bebe 100644 --- a/bin/lib/handlers.js +++ b/bin/lib/handlers.js @@ -70,6 +70,37 @@ 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, 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); + if (typeof value !== 'function') { + return value; + } + state.predicate = value; + return value(state.arg); +}; + +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); + 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 // 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 +551,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 +999,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 +1098,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..3671ce3 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,53 @@ 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 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 + { + $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..1c38d21 100644 --- a/tests/Integration/Page/PageTest.php +++ b/tests/Integration/Page/PageTest.php @@ -425,6 +425,29 @@ 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 + { + $this->assertSame($this->page, $this->page->waitForFunction( + 'typeof window !== "undefined" && typeof process === "undefined"', + null, + ['timeout' => 300] + )); + } + #[Test] public function itThrowsExceptionOnWaitForFunctionTimeout(): void {