Skip to content

Commit

Permalink
dispatch before/after events for web driver commands
Browse files Browse the repository at this point in the history
  • Loading branch information
robocoder committed Dec 9, 2023
1 parent b453d4c commit 6238e49
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 69 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
],
"require": {
"php": ">=7.2",
"psr/event-dispatcher": "^1.0.0",
"ext-curl": "*"
},
"require-dev": {
"php": ">=7.2",
"phpunit/phpunit": "^8.5 || ^9.5"
},
"minimum-stability": "dev",
Expand Down
70 changes: 66 additions & 4 deletions lib/WebDriver/AbstractWebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace WebDriver;

use Psr\EventDispatcher\EventDispatcherInterface;
use WebDriver\Exception as WebDriverException;
use WebDriver\Service\CurlServiceInterface;

/**
* Abstract WebDriver\AbstractWebDriver class
Expand All @@ -34,6 +36,13 @@ abstract class AbstractWebDriver
*/
private $curlService;

/**
* Event Dispatcher
*
* @var \Psr\EventDispatcher\EventDispatcherInterface
*/
private $eventDispatcher;

/**
* Transient options
*
Expand Down Expand Up @@ -77,6 +86,7 @@ public function __construct($url)
$this->transientOptions = [];
$this->extensions = [];
$this->curlService = ServiceFactory::getInstance()->getService('service.curl');
$this->eventDispatcher = ServiceFactory::getInstance()->getService('event_dispatcher');
}

/**
Expand Down Expand Up @@ -104,7 +114,7 @@ public function getURL()
*
* @param \WebDriver\Service\CurlServiceInterface $curlService
*/
public function setCurlService($curlService)
public function setCurlService(CurlServiceInterface $curlService)
{
$this->curlService = $curlService;
}
Expand All @@ -119,6 +129,26 @@ public function getCurlService()
return $this->curlService;
}

/**
* Set event dispatcher
*
* @param \Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

/**
* Get curl service
*
* @return \Psr\EventDispatcher\EventDispatcherInterface
*/
public function getEventDispatcher()
{
return $this->eventDispatcher;
}

/**
* Set transient options
*
Expand Down Expand Up @@ -204,7 +234,7 @@ public function __call($name, $arguments)
}

$parameters = array_shift($arguments);
$result = $this->curl($requestMethod, '/' . $webdriverCommand, $parameters);
$result = $this->curl($requestMethod, $webdriverCommand, $parameters);

return $result['value'];
}
Expand All @@ -225,6 +255,32 @@ public function __get($name)
trigger_error('Undefined property: ' . __CLASS__ . '::$' . $name, E_USER_WARNING);
}

/**
* Event firing command wrapper
*/
protected function curl($requestMethod, $command, $parameters = null, $extraOptions = [])
{
$name = strtoupper($requestMethod) . ':'
. substr($className = get_class($this), strrpos($className, '\\') + 1) . ':'
. ($command ?: substr($this->url, strrpos($this->url, '/') + 1));

$this->eventDispatcher->dispatch((object) ['event' => 'before', 'command' => $name, 'parameters' => $parameters]);

try {
$result = $this->curlExec($requestMethod, $command, $parameters, $extraOptions);
} catch (\Exception $e) {
$source = substr($className = get_class($e), strrpos($className, '\\') + 1);

$this->eventDispatcher->dispatch((object) ['event' => 'error', 'command' => $name, 'error' => $source]);

throw $e;
}

$this->eventDispatcher->dispatch((object) ['event' => 'after', 'command' => $name, 'result' => $result['value']]);

return $result;
}

/**
* Curl request to webdriver server.
*
Expand All @@ -238,7 +294,7 @@ public function __get($name)
*
* @throws \WebDriver\Exception if error
*/
protected function curl($requestMethod, $command, $parameters = null, $extraOptions = [])
protected function curlExec($requestMethod, $command, $parameters, $extraOptions)
{
if ($parameters && is_array($parameters) && $requestMethod !== 'POST') {
throw WebDriverException::factory(
Expand All @@ -252,7 +308,13 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
);
}

$url = $this->url . $command;
$url = $this->url;

if ($command && substr($this->url, -1) !== '/') {
$url .= '/';
}

$url .= $command;

if (($requestMethod === 'GET' || $requestMethod === 'DELETE')
&& $parameters && (is_int($parameters) || is_string($parameters))
Expand Down
8 changes: 4 additions & 4 deletions lib/WebDriver/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function aliases()
*/
public function acceptAlert()
{
$result = $this->curl('POST', '/accept');
$result = $this->curl('POST', 'accept');

return $result['value'];
}
Expand All @@ -71,7 +71,7 @@ public function acceptAlert()
*/
public function dismissAlert()
{
$result = $this->curl('POST', '/dismiss');
$result = $this->curl('POST', 'dismiss');

return $result['value'];
}
Expand All @@ -83,7 +83,7 @@ public function dismissAlert()
*/
public function getAlertText()
{
$result = $this->curl('GET', '/text');
$result = $this->curl('GET', 'text');

return $result['value'];
}
Expand All @@ -101,7 +101,7 @@ public function setAlertValue($text)
? $text
: ['text' => $text];

$result = $this->curl('POST', '/text', $parameters);
$result = $this->curl('POST', 'text', $parameters);

return $result['value'];
}
Expand Down
4 changes: 2 additions & 2 deletions lib/WebDriver/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function findElement($using = null, $value = null)
$parameters = $this->parseArgs('element', func_get_args());

try {
$result = $this->curl('POST', '/element', $parameters);
$result = $this->curl('POST', 'element', $parameters);
} catch (WebDriverException\NoSuchElement $e) {
throw WebDriverException::factory(
WebDriverException::NO_SUCH_ELEMENT,
Expand Down Expand Up @@ -100,7 +100,7 @@ public function findElements($using = null, $value = null)
{
$parameters = $this->parseArgs('elements', func_get_args());

$result = $this->curl('POST', '/elements', $parameters);
$result = $this->curl('POST', 'elements', $parameters);

if (! is_array($result['value'])) {
return [];
Expand Down
14 changes: 7 additions & 7 deletions lib/WebDriver/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getID()
*/
public function getAccessibleName()
{
$result = $this->curl('GET', '/computedlabel');
$result = $this->curl('GET', 'computedlabel');

return $result['value'];
}
Expand All @@ -138,7 +138,7 @@ public function getAccessibleName()
*/
public function getAriaRole()
{
$result = $this->curl('GET', '/computedrole');
$result = $this->curl('GET', 'computedrole');

return $result['value'];
}
Expand All @@ -155,7 +155,7 @@ public function getAriaRole()
*/
public function getShadowRoot()
{
$result = $this->curl('POST', '/shadow');
$result = $this->curl('POST', 'shadow');
$value = $result['value'];

if (! is_array($value)) {
Expand All @@ -178,7 +178,7 @@ public function getShadowRoot()
*/
public function isEnabled()
{
$result = $this->curl('GET', '/enabled');
$result = $this->curl('GET', 'enabled');

return $result['value'];
}
Expand All @@ -190,7 +190,7 @@ public function isEnabled()
*/
public function isSelected()
{
$result = $this->curl('GET', '/selected');
$result = $this->curl('GET', 'selected');

return $result['value'];
}
Expand Down Expand Up @@ -218,7 +218,7 @@ public function sendKeys($text)
$parameters['value'] = [$parameters['text']];
}

$result = $this->curl('POST', '/value', $parameters);
$result = $this->curl('POST', 'value', $parameters);

return $result['value'];
}
Expand All @@ -235,7 +235,7 @@ public function submit()
// trigger_error(__METHOD__, E_USER_DEPRECATED);

try {
$result = $this->curl('POST', '/submit');
$result = $this->curl('POST', 'submit');

return $result['value'];
} catch (\Exception $e) {
Expand Down
4 changes: 2 additions & 2 deletions lib/WebDriver/Execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function async($script, $args = null)

$parameters['args'] = $this->serializeArguments($parameters['args']);

$result = $this->curl('POST', '/execute/async', $parameters);
$result = $this->curl('POST', 'execute/async', $parameters);

return $this->unserializeResult($result['value']);
}
Expand All @@ -55,7 +55,7 @@ public function sync($script, $args = null)

$parameters['args'] = $this->serializeArguments($parameters['args']);

$result = $this->curl('POST', '/execute/sync', $parameters);
$result = $this->curl('POST', 'execute/sync', $parameters);

return $this->unserializeResult($result['value']);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/WebDriver/Extension/ChromeDevTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function execute($cmd, $params = null)
? $cmd
: ['cmd' => $cmd, 'params' => $params ?? new \stdclass];

$result = $this->curl('POST', '/execute', $parameters);
$result = $this->curl('POST', 'execute', $parameters);

return $result['value'];
}
Expand Down
16 changes: 8 additions & 8 deletions lib/WebDriver/Extension/FederatedCredentialManagementAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function methods()
*/
public function cancelDialog()
{
$result = $this->curl('POST', '/canceldialog');
$result = $this->curl('POST', 'canceldialog');

return $result['value'];
}
Expand All @@ -71,7 +71,7 @@ public function selectAccount($parameters)
$parameters = ['accountIndex' => $parameters];
}

$result = $this->curl('POST', '/selectaccount', $parameters);
$result = $this->curl('POST', 'selectaccount', $parameters);

return $result['value'];
}
Expand All @@ -85,7 +85,7 @@ public function selectAccount($parameters)
*/
public function clickDialogButton($parameters)
{
$result = $this->curl('POST', '/clickdialogbutton', $parameters);
$result = $this->curl('POST', 'clickdialogbutton', $parameters);

return $result['value'];
}
Expand All @@ -97,7 +97,7 @@ public function clickDialogButton($parameters)
*/
public function getAccounts()
{
$result = $this->curl('GET', '/accountlist');
$result = $this->curl('GET', 'accountlist');

return $result['value'];
}
Expand All @@ -109,7 +109,7 @@ public function getAccounts()
*/
public function getTitle()
{
$result = $this->curl('GET', '/gettitle');
$result = $this->curl('GET', 'gettitle');

return $result['value'];
}
Expand All @@ -121,7 +121,7 @@ public function getTitle()
*/
public function getDialogType()
{
$result = $this->curl('GET', '/getdialogtype');
$result = $this->curl('GET', 'getdialogtype');

return $result['value'];
}
Expand All @@ -139,7 +139,7 @@ public function setDelayEnabled($parameters)
$parameters = ['enabled' => $parameters];
}

$result = $this->curl('POST', '/setdelayenabled', $parameters);
$result = $this->curl('POST', 'setdelayenabled', $parameters);

return $result['value'];
}
Expand All @@ -151,7 +151,7 @@ public function setDelayEnabled($parameters)
*/
public function resetCooldown()
{
$result = $this->curl('POST', '/resetCooldown');
$result = $this->curl('POST', 'resetCooldown');

return $result['value'];
}
Expand Down
Loading

0 comments on commit 6238e49

Please sign in to comment.