Skip to content

Commit

Permalink
Merge branch '1.12' into fix/dom-node-id-refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell authored Nov 6, 2024
2 parents 976968e + 5849ef8 commit c166296
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 61 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jobs:
- name: Checkout Code
uses: actions/checkout@v4

- uses: browser-actions/setup-chrome@v1
with:
chrome-version: 122

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand Down Expand Up @@ -81,4 +85,4 @@ jobs:
- name: Execute PHPUnit
run: vendor/bin/phpunit
env:
CHROME_PATH: google-chrome-stable
CHROME_PATH: /opt/hostedtoolcache/setup-chrome/chromium/122.0.6261.128/x64/chrome
2 changes: 1 addition & 1 deletion src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function getConnection(): Connection
*
* @param string|null $script
*/
public function setPagePreScript(string $script = null): void
public function setPagePreScript(?string $script = null): void
{
$this->pagePreScript = $script;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Browser/BrowserProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class BrowserProcess implements LoggerAwareInterface
*
* @param LoggerInterface|null $logger
*/
public function __construct(LoggerInterface $logger = null)
public function __construct(?LoggerInterface $logger = null)
{
// set or create logger
$this->setLogger($logger ?? new NullLogger());
Expand Down
2 changes: 1 addition & 1 deletion src/BrowserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BrowserFactory
*/
protected $options = [];

public function __construct(string $chromeBinary = null)
public function __construct(?string $chromeBinary = null)
{
$this->chromeBinary = $chromeBinary ?? (new AutoDiscover())->guessChromeBinaryPath();
}
Expand Down
37 changes: 20 additions & 17 deletions src/Clip.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@

class Clip
{
/** @var int|float */
protected $x;
/** @var int|float */
protected $y;
/** @var int|float */
protected $height;
/** @var int|float */
protected $width;
/** @var float */
protected $scale;

/**
* Clip constructor.
*
* @param int $x
* @param int $y
* @param int $height
* @param int $width
* @param float $scale
* @param int|float $x
* @param int|float $y
* @param int|float $height
* @param int|float $width
* @param float $scale
*/
public function __construct($x, $y, $width, $height, $scale = 1.0)
{
Expand All @@ -38,79 +41,79 @@ public function __construct($x, $y, $width, $height, $scale = 1.0)
}

/**
* @return mixed
* @return int|float
*/
public function getX()
{
return $this->x;
}

/**
* @return mixed
* @return int|float
*/
public function getY()
{
return $this->y;
}

/**
* @return mixed
* @return int|float
*/
public function getHeight()
{
return $this->height;
}

/**
* @return mixed
* @return int|float
*/
public function getWidth()
{
return $this->width;
}

/**
* @return mixed
* @return float
*/
public function getScale()
{
return $this->scale;
}

/**
* @param mixed $x
* @param int|float $x
*/
public function setX($x): void
{
$this->x = $x;
}

/**
* @param mixed $y
* @param int|float $y
*/
public function setY($y): void
{
$this->y = $y;
}

/**
* @param mixed $height
* @param int $height
*/
public function setHeight($height): void
{
$this->height = $height;
}

/**
* @param mixed $width
* @param int $width
*/
public function setWidth($width): void
{
$this->width = $width;
}

/**
* @param mixed $scale
* @param float $scale
*/
public function setScale($scale): void
{
Expand Down
6 changes: 3 additions & 3 deletions src/Communication/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Connection extends EventEmitter implements LoggerAwareInterface
* @param SocketInterface|string $socketClient
* @param int|null $sendSyncDefaultTimeout
*/
public function __construct($socketClient, LoggerInterface $logger = null, int $sendSyncDefaultTimeout = null)
public function __construct($socketClient, ?LoggerInterface $logger = null, ?int $sendSyncDefaultTimeout = null)
{
// set or create logger
$this->setLogger($logger ?? new NullLogger());
Expand Down Expand Up @@ -266,7 +266,7 @@ public function sendMessage(Message $message): ResponseReader
*
* @return Response
*/
public function sendMessageSync(Message $message, int $timeout = null): Response
public function sendMessageSync(Message $message, ?int $timeout = null): Response
{
$responseReader = $this->sendMessage($message);
$response = $responseReader->waitForResponse($timeout);
Expand Down Expand Up @@ -370,7 +370,7 @@ public function processAllEvents(): void
*
* @internal
*/
private function dispatchMessage(string $message, Session $session = null)
private function dispatchMessage(string $message, ?Session $session = null)
{
// responses come as json string
$response = \json_decode($message, true);
Expand Down
2 changes: 1 addition & 1 deletion src/Communication/ResponseReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getResponse(): Response
*
* @return Response
*/
public function waitForResponse(int $timeout = null): Response
public function waitForResponse(?int $timeout = null): Response
{
if ($this->hasResponse()) {
return $this->getResponse();
Expand Down
2 changes: 1 addition & 1 deletion src/Communication/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function sendMessage(Message $message): ResponseReader
*
* @return Response
*/
public function sendMessageSync(Message $message, int $timeout = null): Response
public function sendMessageSync(Message $message, ?int $timeout = null): Response
{
$responseReader = $this->sendMessage($message);

Expand Down
2 changes: 1 addition & 1 deletion src/Communication/Socket/Wrench.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Wrench implements SocketInterface, LoggerAwareInterface, WaitForDataInterf
/**
* @param WrenchClient $client
*/
public function __construct(WrenchClient $client, LoggerInterface $logger = null)
public function __construct(WrenchClient $client, ?LoggerInterface $logger = null)
{
$this->client = $client;

Expand Down
2 changes: 1 addition & 1 deletion src/Cookies/CookiesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CookiesCollection implements \IteratorAggregate, \Countable
/**
* CookiesCollection constructor.
*/
public function __construct(array $cookies = null)
public function __construct(?array $cookies = null)
{
if ($cookies) {
foreach ($cookies as $cookie) {
Expand Down
19 changes: 18 additions & 1 deletion src/Dom/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace HeadlessChromium\Dom;

use HeadlessChromium\Clip;
use HeadlessChromium\Communication\Message;
use HeadlessChromium\Communication\Response;
use HeadlessChromium\Exception\DomException;
Expand Down Expand Up @@ -199,7 +200,7 @@ public function click(): void
$this->scrollIntoView();
$position = $this->getPosition();
$this->page->mouse()
->move($position->getCenterX(), $position->getCenterY())
->move((int) $position->getCenterX(), (int) $position->getCenterY())
->click();
}

Expand Down Expand Up @@ -237,6 +238,22 @@ public function assertNotError(Response $response): void
}
}

public function getClip(): ?Clip
{
$position = $this->getPosition();

if (!$position) {
return null;
}

return new Clip(
$position->getX(),
$position->getY(),
$position->getWidth(),
$position->getHeight(),
);
}

protected function prepareForRequest(): void
{
$this->page->assertNotClosed();
Expand Down
32 changes: 16 additions & 16 deletions src/Dom/NodePosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,40 @@ public function __construct(array $points)
$leftBottomX = $points[6];
$leftBottomY = $points[7];

$this->x = $leftTopX;
$this->y = $leftTopY;
$this->x = (float) $leftTopX;
$this->y = (float) $leftTopY;

$this->height = $leftBottomY - $leftTopY;
$this->width = $rightBottomX - $leftBottomX;
$this->height = (float) ($leftBottomY - $leftTopY);
$this->width = (float) ($rightBottomX - $leftBottomX);
}

public function getX(): int
public function getX(): float
{
return (int) $this->x;
return $this->x;
}

public function getY(): int
public function getY(): float
{
return (int) $this->y;
return $this->y;
}

public function getWidth(): int
public function getWidth(): float
{
return (int) $this->width;
return $this->width;
}

public function getHeight(): int
public function getHeight(): float
{
return (int) $this->height;
return $this->height;
}

public function getCenterX(): int
public function getCenterX(): float
{
return (int) ($this->x + ($this->width / 2));
return $this->x + ($this->width / 2);
}

public function getCenterY(): int
public function getCenterY(): float
{
return (int) ($this->y + ($this->height / 2));
return $this->y + ($this->height / 2);
}
}
2 changes: 1 addition & 1 deletion src/Input/Keyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function press(string $key): self
*
* @return $this
*/
public function release(string $key = null): self
public function release(?string $key = null): self
{
$this->page->assertNotClosed();

Expand Down
8 changes: 4 additions & 4 deletions src/Input/Mouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(Page $page)
*
* @return $this
*/
public function move(int $x, int $y, array $options = null)
public function move(int $x, int $y, ?array $options = null)
{
$this->page->assertNotClosed();

Expand Down Expand Up @@ -88,7 +88,7 @@ public function move(int $x, int $y, array $options = null)
* @throws \HeadlessChromium\Exception\CommunicationException
* @throws \HeadlessChromium\Exception\NoResponseAvailable
*/
public function press(array $options = null)
public function press(?array $options = null)
{
$this->page->assertNotClosed();
$this->page->getSession()->sendMessageSync(new Message('Input.dispatchMouseEvent', [
Expand All @@ -106,7 +106,7 @@ public function press(array $options = null)
* @throws \HeadlessChromium\Exception\CommunicationException
* @throws \HeadlessChromium\Exception\NoResponseAvailable
*/
public function release(array $options = null)
public function release(?array $options = null)
{
$this->page->assertNotClosed();
$this->page->getSession()->sendMessageSync(new Message('Input.dispatchMouseEvent', [
Expand All @@ -126,7 +126,7 @@ public function release(array $options = null)
* @throws \HeadlessChromium\Exception\CommunicationException
* @throws \HeadlessChromium\Exception\NoResponseAvailable
*/
public function click(array $options = null)
public function click(?array $options = null)
{
$this->press($options);
$this->release($options);
Expand Down
Loading

0 comments on commit c166296

Please sign in to comment.