Skip to content

Commit

Permalink
Add support for sending Chrome DevTools commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aboks authored and OndraM committed Feb 6, 2020
1 parent 7489011 commit ecf11c1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
35 changes: 33 additions & 2 deletions lib/Chrome/ChromeDriver.php
Expand Up @@ -6,7 +6,6 @@
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\DriverCommand;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\Service\DriverCommandExecutor;
use Facebook\WebDriver\Remote\WebDriverCommand;

class ChromeDriver extends RemoteWebDriver
Expand All @@ -22,7 +21,7 @@ public static function start(DesiredCapabilities $desired_capabilities = null, C
if ($service === null) {
$service = ChromeDriverService::createDefaultService();
}
$executor = new DriverCommandExecutor($service);
$executor = new ChromeDriverCommandExecutor($service);
$driver = new static($executor, null, $desired_capabilities);
$driver->startSession($desired_capabilities);

Expand Down Expand Up @@ -85,4 +84,36 @@ public static function createBySessionID(
) {
throw new WebDriverException('Please use ChromeDriver::start() instead.');
}

/**
* Executes a Chrome DevTools command
*
* @param string $command The DevTools command to execute
* @param array $parameters Optional parameters to the command
*/
public function sendCommand($command, array $parameters = [])
{
$params = ['cmd' => $command, 'params' => (object) $parameters];
$this->execute(
ChromeDriverCommand::SEND_COMMAND,
$params
);
}

/**
* Executes a Chrome DevTools command and returns the result
*
* @param string $command The DevTools command to execute
* @param array $parameters Optional parameters to the command
* @return array The result of the command
*/
public function sendCommandAndGetResult($command, array $parameters = [])
{
$params = ['cmd' => $command, 'params' => (object) $parameters];

return $this->execute(
ChromeDriverCommand::SEND_COMMAND_AND_GET_RESULT,
$params
);
}
}
10 changes: 10 additions & 0 deletions lib/Chrome/ChromeDriverCommand.php
@@ -0,0 +1,10 @@
<?php

namespace Facebook\WebDriver\Chrome;

class ChromeDriverCommand
{
// Chrome DevTools API
const SEND_COMMAND = 'sendCommand';
const SEND_COMMAND_AND_GET_RESULT = 'sendCommandAndGetResult';
}
25 changes: 25 additions & 0 deletions lib/Chrome/ChromeDriverCommandExecutor.php
@@ -0,0 +1,25 @@
<?php

namespace Facebook\WebDriver\Chrome;

use Facebook\WebDriver\Remote\Service\DriverCommandExecutor;
use Facebook\WebDriver\Remote\Service\DriverService;

class ChromeDriverCommandExecutor extends DriverCommandExecutor
{
public function __construct(DriverService $service)
{
parent::__construct($service);

self::$commands = array_merge(self::$commands, [
ChromeDriverCommand::SEND_COMMAND => [
'method' => 'POST',
'url' => '/session/:sessionId/chromium/send_command',
],
ChromeDriverCommand::SEND_COMMAND_AND_GET_RESULT => [
'method' => 'POST',
'url' => '/session/:sessionId/chromium/send_command_and_get_result',
],
]);
}
}
37 changes: 28 additions & 9 deletions tests/functional/Chrome/ChromeDriverTest.php
Expand Up @@ -31,6 +31,34 @@ protected function tearDown()
}

public function testShouldStartChromeDriver()
{
$this->startChromedriver();

$this->assertInstanceOf(ChromeDriver::class, $this->driver);
$this->assertInstanceOf(DriverCommandExecutor::class, $this->driver->getCommandExecutor());

$this->driver->get('http://localhost:8000/');

$this->assertSame('http://localhost:8000/', $this->driver->getCurrentURL());

$this->driver->quit();
}

public function testShouldExecuteDevToolsCommands()
{
$this->startChromedriver();

$result = $this->driver->sendCommandAndGetResult('Runtime.evaluate', [
'returnByValue' => true,
'expression' => '42 + 1',
]);
$this->assertSame('number', $result['result']['type']);
$this->assertSame(43, $result['result']['value']);

$this->driver->quit();
}

private function startChromedriver()
{
// The createDefaultService() method expect path to the executable to be present in the environment variable
putenv(ChromeDriverService::CHROME_DRIVER_EXECUTABLE . '=' . getenv('CHROMEDRIVER_PATH'));
Expand All @@ -42,14 +70,5 @@ public function testShouldStartChromeDriver()
$desiredCapabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);

$this->driver = ChromeDriver::start($desiredCapabilities);

$this->assertInstanceOf(ChromeDriver::class, $this->driver);
$this->assertInstanceOf(DriverCommandExecutor::class, $this->driver->getCommandExecutor());

$this->driver->get('http://localhost:8000/');

$this->assertSame('http://localhost:8000/', $this->driver->getCurrentURL());

$this->driver->quit();
}
}

0 comments on commit ecf11c1

Please sign in to comment.