-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
251 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
/** | ||
* @license see LICENSE | ||
*/ | ||
|
||
namespace HeadlessChromium\Input; | ||
|
||
use HeadlessChromium\Communication\Message; | ||
use HeadlessChromium\Page; | ||
|
||
class Mouse | ||
{ | ||
|
||
const BUTTON_LEFT = 'left'; | ||
const BUTTON_NONE = 'none'; | ||
const BUTTON_RIGHT = 'right'; | ||
const BUTTON_MIDDLE = 'middle'; | ||
|
||
/** | ||
* @var Page | ||
*/ | ||
protected $page; | ||
|
||
protected $x = 0; | ||
protected $y = 0; | ||
|
||
protected $button = self::BUTTON_NONE; | ||
|
||
/** | ||
* @param Page $page | ||
*/ | ||
public function __construct(Page $page) | ||
{ | ||
$this->page = $page; | ||
} | ||
|
||
/** | ||
* @param int $x | ||
* @param int $y | ||
* @param array|null $options | ||
* @return $this | ||
* @throws \HeadlessChromium\Exception\CommunicationException | ||
* @throws \HeadlessChromium\Exception\NoResponseAvailable | ||
*/ | ||
public function move(int $x, int $y, array $options = null) | ||
{ | ||
$this->page->assertNotClosed(); | ||
|
||
// get origin of the move | ||
$originX = $this->x; | ||
$originY = $this->y; | ||
|
||
// set new position after move | ||
$this->x = $x; | ||
$this->y = $y; | ||
|
||
// number of steps to achieve the move | ||
$steps = $options['steps'] ?? 1; | ||
if ($steps <= 0) { | ||
throw new \InvalidArgumentException('options "steps" for mouse move must be a positive integer'); | ||
} | ||
|
||
// move | ||
for ($i = 1; $i <= $steps; $i++) { | ||
$this->page->getSession()->sendMessageSync(new Message('Input.dispatchMouseEvent', [ | ||
'x' => $originX + ($this->x - $originX) * ($i / $steps), | ||
'y' => $originY + ($this->y - $originY) * ($i / $steps), | ||
'type' => 'mouseMoved' | ||
])); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $options | ||
* @throws \HeadlessChromium\Exception\CommunicationException | ||
* @throws \HeadlessChromium\Exception\NoResponseAvailable | ||
*/ | ||
public function press(array $options = null) | ||
{ | ||
$this->page->assertNotClosed(); | ||
$this->page->getSession()->sendMessageSync(new Message('Input.dispatchMouseEvent', [ | ||
'x' => $this->x, | ||
'y' => $this->y, | ||
'type' => 'mousePressed', | ||
'button' => $options['button'] ?? self::BUTTON_LEFT, | ||
'clickCount' => 1 | ||
])); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param $options | ||
* @throws \HeadlessChromium\Exception\CommunicationException | ||
* @throws \HeadlessChromium\Exception\NoResponseAvailable | ||
*/ | ||
public function release(array $options = null) | ||
{ | ||
$this->page->assertNotClosed(); | ||
$this->page->getSession()->sendMessageSync(new Message('Input.dispatchMouseEvent', [ | ||
'x' => $this->x, | ||
'y' => $this->y, | ||
'type' => 'mouseReleased', | ||
'button' => $options['button'] ?? self::BUTTON_LEFT, | ||
'clickCount' => 1 | ||
])); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param array|null $options | ||
* @throws \HeadlessChromium\Exception\CommunicationException | ||
* @throws \HeadlessChromium\Exception\NoResponseAvailable | ||
*/ | ||
public function click(array $options = null) | ||
{ | ||
$this->press($options); | ||
$this->release($options); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<html> | ||
<head> | ||
<title>b - test</title> | ||
</head> | ||
<body> | ||
<h1>page b</h1> | ||
<div>b</div> | ||
|
||
<a id="a" href="./a.html">go to a</a> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* @license see LICENSE | ||
*/ | ||
|
||
namespace HeadlessChromium\Test; | ||
|
||
use HeadlessChromium\Browser; | ||
use HeadlessChromium\BrowserFactory; | ||
|
||
/** | ||
* @covers \HeadlessChromium\Browser | ||
* @covers \HeadlessChromium\Page | ||
*/ | ||
class MouseApiTest extends BaseTestCase | ||
{ | ||
|
||
/** | ||
* @var Browser\ProcessAwareBrowser | ||
*/ | ||
public static $browser; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
parent::setUpBeforeClass(); | ||
$factory = new BrowserFactory(); | ||
self::$browser = $factory->createBrowser(); | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
parent::tearDownAfterClass(); | ||
self::$browser->close(); | ||
} | ||
|
||
private function openSitePage($file) | ||
{ | ||
$page = self::$browser->createPage(); | ||
$page->navigate($this->sitePath($file))->waitForNavigation(); | ||
|
||
return $page; | ||
} | ||
|
||
/** | ||
* @throws \HeadlessChromium\Exception\CommunicationException | ||
* @throws \HeadlessChromium\Exception\NoResponseAvailable | ||
*/ | ||
public function testClickLink() | ||
{ | ||
// initial navigation | ||
$page = $this->openSitePage('b.html'); | ||
$rect = $page | ||
->evaluate('JSON.parse(JSON.stringify(document.querySelector("#a").getBoundingClientRect()));') | ||
->getReturnValue(); | ||
|
||
$page->mouse()->move($rect['x'], $rect['y'])->click(); | ||
$page->waitForReload(); | ||
|
||
$title = $page->evaluate('document.title')->getReturnValue(); | ||
|
||
$this->assertEquals('a - test', $title); | ||
} | ||
} |