Skip to content
Open
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
7 changes: 5 additions & 2 deletions src/Dom/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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);
Expand Down
56 changes: 48 additions & 8 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use HeadlessChromium\Browser;
use HeadlessChromium\BrowserFactory;
use HeadlessChromium\Exception\StaleElementException;
use PHPUnit\Framework\Attributes\TestWith;

/**
* @covers \HeadlessChromium\Dom\Dom
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
}
27 changes: 27 additions & 0 deletions tests/resources/static-web/boxModel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<html>
<head>
<title>box model - test</title>

<style>
html, body{
width: 500px;
height: 500px;
margin: 0;
padding: 0;
}

div {
width: 200px;
height: 300px;
margin: 10px 20px;
padding: 15px 30px;
border: 5px solid red;
}
</style>
</head>
<body>
<div id="elem">
some content
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion vendor-bin/phpstan/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"php": "^7.4",
"php": "^7.4|^8.0",
"phpstan/phpstan": "2.1.3"
},
"config": {
Expand Down