diff --git a/src/Browser/BrowserContext.php b/src/Browser/BrowserContext.php index 066186d..9f20252 100644 --- a/src/Browser/BrowserContext.php +++ b/src/Browser/BrowserContext.php @@ -85,6 +85,9 @@ public function dispatchEvent(string $eventName, array $params): void if ('binding' === $eventName) { $bindingName = $params['name']; if (is_string($bindingName) && isset($this->bindings[$bindingName]) && is_callable($this->bindings[$bindingName])) { + if (!is_string($params['pageId'])) { + throw new ProtocolErrorException('Invalid pageId in binding event', 0); + } $source = [ 'context' => $this, 'page' => $this->pages[$params['pageId']] ?? null, diff --git a/src/Transport/JsonRpc/JsonRpcTransport.php b/src/Transport/JsonRpc/JsonRpcTransport.php index af3a8f8..829566f 100644 --- a/src/Transport/JsonRpc/JsonRpcTransport.php +++ b/src/Transport/JsonRpc/JsonRpcTransport.php @@ -272,6 +272,12 @@ private function handleEvent(array $event): void } $objectId = $event['objectId']; + if (!is_string($objectId)) { + $this->logger->warning('Invalid objectId in event', ['event' => $event]); + + return; + } + if (isset($this->eventDispatchers[$objectId])) { $this->logger->debug('Dispatching event to registered handler', [ 'objectId' => $objectId, diff --git a/tests/Integration/Browser/BrowserContextTest.php b/tests/Integration/Browser/BrowserContextTest.php index 907880f..86e47c6 100644 --- a/tests/Integration/Browser/BrowserContextTest.php +++ b/tests/Integration/Browser/BrowserContextTest.php @@ -133,7 +133,17 @@ public function itSetsGeolocation(): void $page->click('button'); - usleep(500000); + // Poll for either coordinates or error text since Page::waitForFunction is not available + $deadline = microtime(true) + 5.0; // 5 seconds + do { + $content = $page->content() ?? ''; + $hasCoordinates = str_contains($content, '59.95,30.31667'); + $hasError = str_contains($content, 'Error'); + if ($hasCoordinates || $hasError) { + break; + } + usleep(100 * 1000); // 100ms + } while (microtime(true) < $deadline); $content = $page->content(); $hasCoordinates = str_contains($content, '59.95,30.31667'); diff --git a/tests/Integration/Locator/LocatorTest.php b/tests/Integration/Locator/LocatorTest.php index 2dd9cb5..543382f 100644 --- a/tests/Integration/Locator/LocatorTest.php +++ b/tests/Integration/Locator/LocatorTest.php @@ -168,6 +168,16 @@ public function itTakesAScreenshotOfAnElement(): void $this->assertStringStartsWith(base64_decode('iVBORw0KGgo='), base64_decode($binary)); } + #[Test] + public function itEvaluatesElementTagNameAndCss(): void + { + $tagName = $this->page->locator('h1')->evaluate('element => element.tagName'); + $this->assertEquals('H1', $tagName); + + $width = $this->page->locator('#div-1')->evaluate('element => window.getComputedStyle(element).width'); + $this->assertEquals('50px', $width); + } + private static function findFreePort(): int { return 0;