Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scrolling timeout issue #678

Open
wants to merge 3 commits into
base: 1.13
Choose a base branch
from
Open
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
77 changes: 63 additions & 14 deletions src/Input/Mouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public function click(?array $options = null)
*
* @throws \HeadlessChromium\Exception\CommunicationException
* @throws \HeadlessChromium\Exception\NoResponseAvailable
* @throws \HeadlessChromium\Exception\OperationTimedOut
*
* @return $this
*/
Expand All @@ -156,6 +157,7 @@ public function scrollUp(int $distance)
*
* @throws \HeadlessChromium\Exception\CommunicationException
* @throws \HeadlessChromium\Exception\NoResponseAvailable
* @throws \HeadlessChromium\Exception\OperationTimedOut
*
* @return $this
*/
Expand All @@ -180,6 +182,52 @@ private function scroll(int $distanceY, int $distanceX = 0): self
{
$this->page->assertNotClosed();

// make sure the mouse is on the screen
$this->move($this->x, $this->y);

[$distances, $targets] = $this->getScrollDistancesAndTargets($distanceY, $distanceX);

// scroll
$this->sendScrollMessage($distances);

try {
// wait until the scroll is done
Utils::tryWithTimeout(3_000_000, $this->waitForScroll($targets['x'], $targets['y']));
} catch (\HeadlessChromium\Exception\OperationTimedOut $exception) {
// Maybe the possible max scroll distances changed in the meantime.
$prevDistances = $distances;

[$distances, $targets] = $this->getScrollDistancesAndTargets($distanceY, $distanceX);

if ($prevDistances === $distances) {
throw $exception;
}

if (0 !== $distanceY || 0 !== $distanceX) { // Try with the new values.
$this->sendScrollMessage($distances);

// wait until the scroll is done
Utils::tryWithTimeout(3_000_000, $this->waitForScroll($targets['x'], $targets['y']));
}
}

// set new position after move
$this->x += $distances['x'];
$this->y += $distances['y'];

return $this;
}

/**
* @throws \HeadlessChromium\Exception\OperationTimedOut
* @throws \HeadlessChromium\Exception\CommunicationException
* @throws \HeadlessChromium\Exception\CommunicationException\ResponseHasError
* @throws \HeadlessChromium\Exception\NoResponseAvailable
*
* @return array{ array{ x: int, y: int }, array{ x: int, y: int } }
*/
private function getScrollDistancesAndTargets(int $distanceY, int $distanceX = 0): array
{
$scrollableArea = $this->page->getLayoutMetrics()->getCssContentSize();
$visibleArea = $this->page->getLayoutMetrics()->getCssVisualViewport();

Expand All @@ -192,26 +240,27 @@ private function scroll(int $distanceY, int $distanceX = 0): self
$targetX = $visibleArea['pageX'] + $distanceX;
$targetY = $visibleArea['pageY'] + $distanceY;

// make sure the mouse is on the screen
$this->move($this->x, $this->y);
return [
['x' => $distanceX, 'y' => $distanceY],
['x' => (int) $targetX, 'y' => (int) $targetY],
];
}

// scroll
/**
* @param array{ x: int, y: int } $distances
*
* @throws \HeadlessChromium\Exception\CommunicationException
* @throws \HeadlessChromium\Exception\NoResponseAvailable
*/
private function sendScrollMessage(array $distances): void
{
$this->page->getSession()->sendMessageSync(new Message('Input.dispatchMouseEvent', [
'type' => 'mouseWheel',
'x' => $this->x,
'y' => $this->y,
'deltaX' => $distanceX,
'deltaY' => $distanceY,
'deltaX' => $distances['x'],
'deltaY' => $distances['y'],
]));

// wait until the scroll is done
Utils::tryWithTimeout(30000 * 1000, $this->waitForScroll($targetX, $targetY));

// set new position after move
$this->x += $distanceX;
$this->y += $distanceY;

return $this;
}

/**
Expand Down