Skip to content

Frame::waitForFunction() and Locator::waitForFunction() return at once: the function reaches Playwright as an expression #213

Closed

Description

@loevgaard

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 playwright 1.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 ms

The 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

  1. smnandre commented on Nov 15, 2026

    @smnandre
    Member

    Thank you @loevgaard for the report and for tracing it to isFunction. Confirmed on main,
    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 === true in place of () => window.ready === true.

  2. added
    Has PRAn open pull request addresses this issue
    on Nov 15, 2026
  3. loevgaard commented on Nov 15, 2026

    @loevgaard
    Author

    Thanks! One thing before #215 merges: on a page whose Content-Security-Policy leaves out 'unsafe-eval', its page and frame waits throw an EvalError once 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 on 90143c5, 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 under script-src 'self' a frame's wait throws the same EvalError, as does a page's.

  4. smnandre commented on Nov 15, 2026

    @smnandre
    Member

    Should be fixed now (cf #6897979405 )

  5. added a commit that references this issue on Nov 17, 2026
    52a276b
  6. added a commit that references this issue on Nov 17, 2026
    d4f21c0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has PRAn open pull request addresses this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions