From 11923e395095e8ee6f9f872b001d999155013946 Mon Sep 17 00:00:00 2001 From: Albert Prat Date: Thu, 18 Jun 2026 12:06:51 +0200 Subject: [PATCH 1/2] Add a parameter to `Node::getPostion()` to allow to chose the box model --- src/Dom/Node.php | 7 ++- tests/DomTest.php | 56 ++++++++++++++++++++---- tests/resources/static-web/boxModel.html | 27 ++++++++++++ 3 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 tests/resources/static-web/boxModel.html diff --git a/src/Dom/Node.php b/src/Dom/Node.php index efce271f..535cfa28 100644 --- a/src/Dom/Node.php +++ b/src/Dom/Node.php @@ -128,7 +128,10 @@ public function getAttribute(string $name): ?string return $this->getAttributes()->get($name); } - public function getPosition(): ?NodePosition + /** + * @param 'content'|'padding'|'border'|'margin' $boxModel + */ + public function getPosition(string $boxModel = 'content'): ?NodePosition { $message = new Message('DOM.getBoxModel', [ 'nodeId' => $this->getNodeIdForRequest(), @@ -137,7 +140,7 @@ public function getPosition(): ?NodePosition $this->assertNotError($response); - $points = $response->getResultData('model')['content']; + $points = $response->getResultData('model')[$boxModel] ?? null; if (null !== $points) { return new NodePosition($points); diff --git a/tests/DomTest.php b/tests/DomTest.php index 3a5a5fdd..6010dd84 100644 --- a/tests/DomTest.php +++ b/tests/DomTest.php @@ -5,6 +5,7 @@ use HeadlessChromium\Browser; use HeadlessChromium\BrowserFactory; use HeadlessChromium\Exception\StaleElementException; +use PHPUnit\Framework\Attributes\TestWith; /** * @covers \HeadlessChromium\Dom\Dom @@ -26,14 +27,6 @@ public static function tearDownAfterClass(): void self::$browser->close(); } - private function openSitePage($file) - { - $page = self::$browser->createPage(); - $page->navigate(self::sitePath($file))->waitForNavigation(); - - return $page; - } - public function testSearchByCssSelector(): void { $page = $this->openSitePage('domForm.html'); @@ -140,6 +133,45 @@ public function testSetAttribute(): void self::assertSame('hello', $value); } + #[TestWith(['margin', 0, 0, 310, 360])] + /** @phpstan-ignore attribute.nonRepeatable */ + #[TestWith(['border', 20, 10, 270, 340])] + /** @phpstan-ignore attribute.nonRepeatable */ + #[TestWith(['padding', 25, 15, 260, 330])] + /** @phpstan-ignore attribute.nonRepeatable */ + #[TestWith(['content', 55, 30, 200, 300])] + /** @phpstan-ignore attribute.nonRepeatable */ + #[TestWith([null, 55, 30, 200, 300])] + /** @phpstan-ignore attribute.nonRepeatable */ + #[TestWith(['-invalid-', 0, 0, 0, 0])] + public function testGetPosition( + ?string $boxModel, + float $expectedX, + float $expectedY, + float $expectedWidth, + float $expectedHeight, + ): void { + $page = $this->openSitePage('boxModel.html'); + + $element = $page->dom()->querySelector('#elem'); + + if (null !== $boxModel) { + $position = $element->getPosition($boxModel); + } else { + $position = $element->getPosition(); + } + + if ('-invalid-' !== $boxModel) { + self::assertNotNull($position); + self::assertSame($expectedX, $position->getX()); + self::assertSame($expectedY, $position->getY()); + self::assertSame($expectedWidth, $position->getWidth()); + self::assertSame($expectedHeight, $position->getHeight()); + } else { + self::assertNull($position); + } + } + public function testUploadFile(): void { $page = $this->openSitePage('domForm.html'); @@ -240,4 +272,12 @@ public function testRegularNodeIsMarkedAsStaleAfterReload(): void $inputNode->sendKeys('test'); } + + private function openSitePage($file) + { + $page = self::$browser->createPage(); + $page->navigate(self::sitePath($file))->waitForNavigation(); + + return $page; + } } diff --git a/tests/resources/static-web/boxModel.html b/tests/resources/static-web/boxModel.html new file mode 100644 index 00000000..5f64ede9 --- /dev/null +++ b/tests/resources/static-web/boxModel.html @@ -0,0 +1,27 @@ + + + box model - test + + + + +
+ some content +
+ + From 822457c7733e4372e4cb061888a5dd28e3e3f862 Mon Sep 17 00:00:00 2001 From: Albert Prat Date: Thu, 18 Jun 2026 12:35:04 +0200 Subject: [PATCH 2/2] Bump vendor-bin composer.json PHP version to match phpstan's --- vendor-bin/phpstan/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor-bin/phpstan/composer.json b/vendor-bin/phpstan/composer.json index c32e6521..ef170655 100644 --- a/vendor-bin/phpstan/composer.json +++ b/vendor-bin/phpstan/composer.json @@ -1,6 +1,6 @@ { "require": { - "php": "^7.4", + "php": "^7.4|^8.0", "phpstan/phpstan": "2.1.3" }, "config": {