diff --git a/src/Dom/Node.php b/src/Dom/Node.php index efce271..535cfa2 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 3a5a5fd..6010dd8 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 0000000..5f64ede --- /dev/null +++ b/tests/resources/static-web/boxModel.html @@ -0,0 +1,27 @@ + +
+