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
29 changes: 28 additions & 1 deletion src/Locator/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ public function nth(int $index): self

public function evaluate(string $expression, mixed $arg = null): mixed
{
$response = $this->sendCommand('locator.evaluate', ['expression' => $expression, 'arg' => $arg]);
$normalized = self::normalizeForLocator($expression);
$response = $this->sendCommand('locator.evaluate', ['expression' => $normalized, 'arg' => $arg]);

return $response['value'] ?? null;
}
Expand Down Expand Up @@ -447,6 +448,32 @@ private function sendCommand(string $action, array $params = []): array
return $response;
}

private static function normalizeForLocator(string $expression): string
{
$trimmed = ltrim($expression);

if (self::isFunctionLike($trimmed)) {
return $expression;
}

if (self::startsWithReturn($trimmed)) {
return '(el, arg) => { '.$trimmed.' }';
}

return $expression;
}

private static function isFunctionLike(string $s): bool
{
// Detect common JS function patterns: function, async function, arrow functions
return (bool) preg_match('/^((async\s+)?function\b|\([^)]*\)\s*=>|[A-Za-z_$][A-Za-z0-9_$]*\s*=>|async\s*\([^)]*\)\s*=>)/', $s);
}

private static function startsWithReturn(string $s): bool
{
return (bool) preg_match('/^return\b/', $s);
}

/**
* @param array<string, mixed> $options
*/
Expand Down
29 changes: 28 additions & 1 deletion src/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ public function content(): ?string

public function evaluate(string $expression, mixed $arg = null): mixed
{
$response = $this->sendCommand('evaluate', ['expression' => $expression, 'arg' => $arg]);
$normalized = self::normalizeForPage($expression);
$response = $this->sendCommand('evaluate', ['expression' => $normalized, 'arg' => $arg]);

return $response['result'] ?? null;
}
Expand Down Expand Up @@ -751,6 +752,32 @@ private function createConsoleMessage(array $params): ConsoleMessage
return new ConsoleMessage($params);
}

private static function normalizeForPage(string $expression): string
{
$trimmed = ltrim($expression);

if (self::isFunctionLike($trimmed)) {
return $expression;
}

if (self::startsWithReturn($trimmed)) {
return '(arg) => { '.$trimmed.' }';
}

return $expression;
}

private static function isFunctionLike(string $s): bool
{
// Detect common JS function patterns: function, async function, arrow functions
return (bool) preg_match('/^((async\s+)?function\b|\([^)]*\)\s*=>|[A-Za-z_$][A-Za-z0-9_$]*\s*=>|async\s*\([^)]*\)\s*=>)/', $s);
}

private static function startsWithReturn(string $s): bool
{
return (bool) preg_match('/^return\b/', $s);
}

/**
* Helper method to validate transport data for Request creation.
*
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/Locator/LocatorEvaluateNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* This file is part of the playwright-php/playwright package.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace PlaywrightPHP\Tests\Unit\Locator;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use PlaywrightPHP\Locator\Locator;
use PlaywrightPHP\Transport\TransportInterface;

#[CoversClass(Locator::class)]
final class LocatorEvaluateNormalizerTest extends TestCase
{
public function testNormalizesReturnBodyToFunctionWithElement(): void
{
$transport = $this->createMock(TransportInterface::class);

$transport
->expects($this->once())
->method('send')
->with($this->callback(function ($payload) {
return 'locator.evaluate' === $payload['action']
&& '(el, arg) => { return el.textContent; }' === $payload['expression'];
}))
->willReturn(['value' => 'hello']);

$locator = new Locator($transport, 'page1', '.title');
$result = $locator->evaluate('return el.textContent;');
$this->assertSame('hello', $result);
}

public function testLeavesPlainExpressionUntouched(): void
{
$transport = $this->createMock(TransportInterface::class);

$transport
->expects($this->once())
->method('send')
->with($this->callback(function ($payload) {
return 'locator.evaluate' === $payload['action']
&& 'element.textContent' === $payload['expression'];
}))
->willReturn(['value' => 'ok']);

$locator = new Locator($transport, 'page1', '.title');
$result = $locator->evaluate('element.textContent');
$this->assertSame('ok', $result);
}
}
59 changes: 59 additions & 0 deletions tests/Unit/Page/PageEvaluateNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* This file is part of the playwright-php/playwright package.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace PlaywrightPHP\Tests\Unit\Page;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use PlaywrightPHP\Browser\BrowserContextInterface;
use PlaywrightPHP\Page\Page;
use PlaywrightPHP\Transport\TransportInterface;

#[CoversClass(Page::class)]
final class PageEvaluateNormalizerTest extends TestCase
{
public function testNormalizesReturnBodyToFunction(): void
{
$transport = $this->createMock(TransportInterface::class);
$context = $this->createMock(BrowserContextInterface::class);

$transport
->expects($this->once())
->method('send')
->with($this->callback(function ($payload) {
return 'page.evaluate' === $payload['action']
&& '(arg) => { return 42; }' === $payload['expression'];
}))
->willReturn(['result' => 42]);

$page = new Page($transport, $context, 'p1');
$result = $page->evaluate('return 42;');
$this->assertSame(42, $result);
}

public function testLeavesPlainExpressionUntouched(): void
{
$transport = $this->createMock(TransportInterface::class);
$context = $this->createMock(BrowserContextInterface::class);

$transport
->expects($this->once())
->method('send')
->with($this->callback(function ($payload) {
return 'page.evaluate' === $payload['action']
&& 'document.title' === $payload['expression'];
}))
->willReturn(['result' => 'Hello']);

$page = new Page($transport, $context, 'p1');
$result = $page->evaluate('document.title');
$this->assertSame('Hello', $result);
}
}