Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 34 additions & 14 deletions bin/lib/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 };
}

Expand Down Expand Up @@ -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 };
}

Expand Down
48 changes: 48 additions & 0 deletions tests/Integration/Frame/FrameIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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('<meta http-equiv="Content-Security-Policy" content="script-src \'self\'"><p>Strict</p>');
$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
{
Expand Down
22 changes: 22 additions & 0 deletions tests/Integration/Locator/LocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div id="status">ready</div>');
$locator = $this->page->locator('#status');

$this->assertSame($locator, $locator->waitForFunction(
'(element, text) => element.textContent === text',
'ready',
['timeout' => 1000],
));
}

#[Test]
public function itExposesThePageItResolvesAgainst(): void
{
Expand Down
23 changes: 23 additions & 0 deletions tests/Integration/Page/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<meta http-equiv="Content-Security-Policy" content="script-src \'self\'"><p>Strict</p>');
$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
{
Expand Down
Loading