1<?php
2
3namespace Facebook\WebDriver\Chrome;
4
5use Facebook\WebDriver\Remote\RemoteWebDriver;
6
7/**
8 * Provide access to Chrome DevTools Protocol (CDP) commands via HTTP endpoint of Chromedriver.
9 *
10 * @see https://chromedevtools.github.io/devtools-protocol/
11 */
12class ChromeDevToolsDriver
13{
14    const SEND_COMMAND = [
15        'method' => 'POST',
16        'url' => '/session/:sessionId/goog/cdp/execute',
17    ];
18
19    /**
20     * @var RemoteWebDriver
21     */
22    private $driver;
23
24    public function __construct(RemoteWebDriver $driver)
25    {
26        $this->driver = $driver;
27    }
28
29    /**
30     * Executes a Chrome DevTools command
31     *
32     * @param string $command The DevTools command to execute
33     * @param array $parameters Optional parameters to the command
34     * @return array The result of the command
35     */
36    public function execute($command, array $parameters = [])
37    {
38        $params = ['cmd' => $command, 'params' => (object) $parameters];
39
40        return $this->driver->executeCustomCommand(
41            self::SEND_COMMAND['url'],
42            self::SEND_COMMAND['method'],
43            $params
44        );
45    }
46}
47