Repository navigation
Description
Environment
- playwright-php/playwright: 1.5.1 (the handlers below are the same on
main, 1.6.0) - PHP: 8.5.12
- Node.js: 22.23.4 (npm
playwright1.63.0) - Browser: chromium (headless shell)
What happens
$frame->waitForFunction('() => false') returns at once, where it should throw a TimeoutException once its timeout ends. $locator->waitForFunction() does the same. $page->waitForFunction() waits, as it should.
So a test that waits in a frame for something can go on before that thing exists, and passes when it never comes.
How to reproduce
<?php
require __DIR__.'/vendor/autoload.php';
use Playwright\Exception\TimeoutException;
use Playwright\Playwright;
$context = Playwright::chromium(['headless' => true]);
$page = $context->newPage();
$page->setContent('<iframe name="child" srcdoc="<p>child</p>"></iframe>');
$frame = $page->frame(['name' => 'child']);
foreach (['page' => $page, 'frame' => $frame, 'locator' => $page->locator('iframe')] as $what => $target) {
$start = microtime(true);
try {
$target->waitForFunction('() => false', null, ['timeout' => 1000]);
printf("%s: returned after %d ms\n", $what, (microtime(true) - $start) * 1000);
} catch (TimeoutException $e) {
printf("%s: timed out after %d ms\n", $what, (microtime(true) - $start) * 1000);
}
}
$context->close();Expected behavior
page: timed out after 1000 ms
frame: timed out after 1000 ms
locator: timed out after 1000 ms
Actual behavior
page: timed out after 1053 ms
frame: returned after 52 ms
locator: returned after 16 ms
Why
The frame handler's waitForFunction() in bin/lib/handlers.js hands Playwright the function's source as a string, and so does the locator handler's:
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);
return { success: true };
}Playwright's client sends isFunction: typeof pageFunction === 'function', which is false for a string. So the server evaluates the string as an expression and never calls it: () => false evaluates to a function, which is truthy, so the wait ends at once and resolves with a handle to that function. In Playwright alone:
const frame = page.frame({ name: 'child' });
await frame.waitForFunction('() => false', undefined, { timeout: 1000 }); // returns after 13 ms, with a function
await frame.waitForFunction(eval('(() => false)'), undefined, { timeout: 1000 }); // TimeoutError after 1001 msThe page handler's waitForFunction() turns the string into a function first (eval(`(${command.pageFunction})`)), so Playwright sends it with isFunction: true, calls it in the page and waits. The comment above is not so: Playwright sends a function to the page as its source, String(pageFunction), and runs it there, never in Node.
A possible fix
Do as the page handler does in the frame's and the locator's handlers, but keep the string when it isn't a function, so that an expression such as window.ready === true still reaches the page as one:
let predicate;
try {
predicate = eval(`(${command.pageFunction})`);
} catch {
predicate = undefined;
}
await frame.waitForFunction(typeof predicate === 'function' ? predicate : command.pageFunction, command.arg, command.options);The page handler would gain from the typeof check too: an expression that Node can evaluate itself, such as typeof window === 'undefined', is evaluated there today, and its value, not the expression, reaches the page.
Activity
smnandre commentedon Nov 15, 2026
Thank you @loevgaard for the report and for tracing it to
isFunction. Confirmed onmain,and your analysis is right 👍
#215 fixes it: page, frame and locator now share one predicate that runs in the
page, so a function source and an expression behave the same on the three.
Until it is released, an expression works on frames and locators:
window.ready === truein place of() => window.ready === true.loevgaard commentedon Nov 15, 2026
Thanks! One thing before #215 merges: on a page whose Content-Security-Policy leaves out
'unsafe-eval', its page and frame waits throw anEvalErroronce a poll after the first runs. Our app's pages send such a policy, and wait with$page->waitForFunction('() => …'), which 1.5.1 handles. The numbers and a repro are on the PR: #215 (comment). I ran them again on90143c5, with the same results.The workaround has the same limit: on 1.5.1,
'window.ready === true'waits on a frame and a locator without a CSP, but underscript-src 'self'a frame's wait throws the sameEvalError, as does a page's.smnandre commentedon Nov 15, 2026
Should be fixed now (cf #6897979405 )
Evaluate waitForFunction() predicates in the page
Evaluate waitForFunction() predicates in the page (playwright-php#215)