Skip to content

Commit 505dc87

Browse files
committed
dispatch before/after events for web driver commands
1 parent 9c7d9be commit 505dc87

16 files changed

+165
-69
lines changed

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
],
2727
"require": {
2828
"php": ">=7.2",
29+
"psr/event-dispatcher": "^1.0.0",
2930
"ext-curl": "*"
3031
},
3132
"require-dev": {
32-
"php": ">=7.2",
3333
"phpunit/phpunit": "^8.5 || ^9.5"
3434
},
3535
"minimum-stability": "dev",

Diff for: lib/WebDriver/AbstractWebDriver.php

+66-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace WebDriver;
1313

14+
use Psr\EventDispatcher\EventDispatcherInterface;
1415
use WebDriver\Exception as WebDriverException;
16+
use WebDriver\Service\CurlServiceInterface;
1517

1618
/**
1719
* Abstract WebDriver\AbstractWebDriver class
@@ -34,6 +36,13 @@ abstract class AbstractWebDriver
3436
*/
3537
private $curlService;
3638

39+
/**
40+
* Event Dispatcher
41+
*
42+
* @var \Psr\EventDispatcher\EventDispatcherInterface
43+
*/
44+
private $eventDispatcher;
45+
3746
/**
3847
* Transient options
3948
*
@@ -77,6 +86,7 @@ public function __construct($url)
7786
$this->transientOptions = [];
7887
$this->extensions = [];
7988
$this->curlService = ServiceFactory::getInstance()->getService('service.curl');
89+
$this->eventDispatcher = ServiceFactory::getInstance()->getService('event_dispatcher');
8090
}
8191

8292
/**
@@ -104,7 +114,7 @@ public function getURL()
104114
*
105115
* @param \WebDriver\Service\CurlServiceInterface $curlService
106116
*/
107-
public function setCurlService($curlService)
117+
public function setCurlService(CurlServiceInterface $curlService)
108118
{
109119
$this->curlService = $curlService;
110120
}
@@ -119,6 +129,26 @@ public function getCurlService()
119129
return $this->curlService;
120130
}
121131

132+
/**
133+
* Set event dispatcher
134+
*
135+
* @param \Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher
136+
*/
137+
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
138+
{
139+
$this->eventDispatcher = $eventDispatcher;
140+
}
141+
142+
/**
143+
* Get curl service
144+
*
145+
* @return \Psr\EventDispatcher\EventDispatcherInterface
146+
*/
147+
public function getEventDispatcher()
148+
{
149+
return $this->eventDispatcher;
150+
}
151+
122152
/**
123153
* Set transient options
124154
*
@@ -204,7 +234,7 @@ public function __call($name, $arguments)
204234
}
205235

206236
$parameters = array_shift($arguments);
207-
$result = $this->curl($requestMethod, '/' . $webdriverCommand, $parameters);
237+
$result = $this->curl($requestMethod, $webdriverCommand, $parameters);
208238

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

258+
/**
259+
* Event firing command wrapper
260+
*/
261+
protected function curl($requestMethod, $command, $parameters = null, $extraOptions = [])
262+
{
263+
$name = strtoupper($requestMethod) . ':'
264+
. substr($className = get_class($this), strrpos($className, '\\') + 1) . ':'
265+
. ($command ?: substr($this->url, strrpos($this->url, '/') + 1));
266+
267+
$this->eventDispatcher->dispatch((object) ['event' => 'before', 'command' => $name, 'parameters' => $parameters]);
268+
269+
try {
270+
$result = $this->curlExec($requestMethod, $command, $parameters, $extraOptions);
271+
} catch (\Exception $e) {
272+
$source = substr($className = get_class($e), strrpos($className, '\\') + 1);
273+
274+
$this->eventDispatcher->dispatch((object) ['event' => 'error', 'command' => $name, 'error' => $source]);
275+
276+
throw $e;
277+
}
278+
279+
$this->eventDispatcher->dispatch((object) ['event' => 'after', 'command' => $name, 'result' => $result['value']]);
280+
281+
return $result;
282+
}
283+
228284
/**
229285
* Curl request to webdriver server.
230286
*
@@ -238,7 +294,7 @@ public function __get($name)
238294
*
239295
* @throws \WebDriver\Exception if error
240296
*/
241-
protected function curl($requestMethod, $command, $parameters = null, $extraOptions = [])
297+
protected function curlExec($requestMethod, $command, $parameters, $extraOptions)
242298
{
243299
if ($parameters && is_array($parameters) && $requestMethod !== 'POST') {
244300
throw WebDriverException::factory(
@@ -252,7 +308,13 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
252308
);
253309
}
254310

255-
$url = $this->url . $command;
311+
$url = $this->url;
312+
313+
if ($command && substr($this->url, -1) !== '/') {
314+
$url .= '/';
315+
}
316+
317+
$url .= $command;
256318

257319
if (($requestMethod === 'GET' || $requestMethod === 'DELETE')
258320
&& $parameters && (is_int($parameters) || is_string($parameters))

Diff for: lib/WebDriver/Alert.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function aliases()
5959
*/
6060
public function acceptAlert()
6161
{
62-
$result = $this->curl('POST', '/accept');
62+
$result = $this->curl('POST', 'accept');
6363

6464
return $result['value'];
6565
}
@@ -71,7 +71,7 @@ public function acceptAlert()
7171
*/
7272
public function dismissAlert()
7373
{
74-
$result = $this->curl('POST', '/dismiss');
74+
$result = $this->curl('POST', 'dismiss');
7575

7676
return $result['value'];
7777
}
@@ -83,7 +83,7 @@ public function dismissAlert()
8383
*/
8484
public function getAlertText()
8585
{
86-
$result = $this->curl('GET', '/text');
86+
$result = $this->curl('GET', 'text');
8787

8888
return $result['value'];
8989
}
@@ -101,7 +101,7 @@ public function setAlertValue($text)
101101
? $text
102102
: ['text' => $text];
103103

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

106106
return $result['value'];
107107
}

Diff for: lib/WebDriver/Container.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function findElement($using = null, $value = null)
5454
$parameters = $this->parseArgs('element', func_get_args());
5555

5656
try {
57-
$result = $this->curl('POST', '/element', $parameters);
57+
$result = $this->curl('POST', 'element', $parameters);
5858
} catch (WebDriverException\NoSuchElement $e) {
5959
throw WebDriverException::factory(
6060
WebDriverException::NO_SUCH_ELEMENT,
@@ -100,7 +100,7 @@ public function findElements($using = null, $value = null)
100100
{
101101
$parameters = $this->parseArgs('elements', func_get_args());
102102

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

105105
if (! is_array($result['value'])) {
106106
return [];

Diff for: lib/WebDriver/Element.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function getID()
126126
*/
127127
public function getAccessibleName()
128128
{
129-
$result = $this->curl('GET', '/computedlabel');
129+
$result = $this->curl('GET', 'computedlabel');
130130

131131
return $result['value'];
132132
}
@@ -138,7 +138,7 @@ public function getAccessibleName()
138138
*/
139139
public function getAriaRole()
140140
{
141-
$result = $this->curl('GET', '/computedrole');
141+
$result = $this->curl('GET', 'computedrole');
142142

143143
return $result['value'];
144144
}
@@ -155,7 +155,7 @@ public function getAriaRole()
155155
*/
156156
public function getShadowRoot()
157157
{
158-
$result = $this->curl('POST', '/shadow');
158+
$result = $this->curl('POST', 'shadow');
159159
$value = $result['value'];
160160

161161
if (! is_array($value)) {
@@ -178,7 +178,7 @@ public function getShadowRoot()
178178
*/
179179
public function isEnabled()
180180
{
181-
$result = $this->curl('GET', '/enabled');
181+
$result = $this->curl('GET', 'enabled');
182182

183183
return $result['value'];
184184
}
@@ -190,7 +190,7 @@ public function isEnabled()
190190
*/
191191
public function isSelected()
192192
{
193-
$result = $this->curl('GET', '/selected');
193+
$result = $this->curl('GET', 'selected');
194194

195195
return $result['value'];
196196
}
@@ -218,7 +218,7 @@ public function sendKeys($text)
218218
$parameters['value'] = [$parameters['text']];
219219
}
220220

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

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

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

240240
return $result['value'];
241241
} catch (\Exception $e) {

Diff for: lib/WebDriver/Execute.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function async($script, $args = null)
3434

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

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

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

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

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

6060
return $this->unserializeResult($result['value']);
6161
}

Diff for: lib/WebDriver/Extension/ChromeDevTools.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function execute($cmd, $params = null)
3636
? $cmd
3737
: ['cmd' => $cmd, 'params' => $params ?? new \stdclass];
3838

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

4141
return $result['value'];
4242
}

Diff for: lib/WebDriver/Extension/FederatedCredentialManagementAPI.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function methods()
5353
*/
5454
public function cancelDialog()
5555
{
56-
$result = $this->curl('POST', '/canceldialog');
56+
$result = $this->curl('POST', 'canceldialog');
5757

5858
return $result['value'];
5959
}
@@ -71,7 +71,7 @@ public function selectAccount($parameters)
7171
$parameters = ['accountIndex' => $parameters];
7272
}
7373

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

7676
return $result['value'];
7777
}
@@ -85,7 +85,7 @@ public function selectAccount($parameters)
8585
*/
8686
public function clickDialogButton($parameters)
8787
{
88-
$result = $this->curl('POST', '/clickdialogbutton', $parameters);
88+
$result = $this->curl('POST', 'clickdialogbutton', $parameters);
8989

9090
return $result['value'];
9191
}
@@ -97,7 +97,7 @@ public function clickDialogButton($parameters)
9797
*/
9898
public function getAccounts()
9999
{
100-
$result = $this->curl('GET', '/accountlist');
100+
$result = $this->curl('GET', 'accountlist');
101101

102102
return $result['value'];
103103
}
@@ -109,7 +109,7 @@ public function getAccounts()
109109
*/
110110
public function getTitle()
111111
{
112-
$result = $this->curl('GET', '/gettitle');
112+
$result = $this->curl('GET', 'gettitle');
113113

114114
return $result['value'];
115115
}
@@ -121,7 +121,7 @@ public function getTitle()
121121
*/
122122
public function getDialogType()
123123
{
124-
$result = $this->curl('GET', '/getdialogtype');
124+
$result = $this->curl('GET', 'getdialogtype');
125125

126126
return $result['value'];
127127
}
@@ -139,7 +139,7 @@ public function setDelayEnabled($parameters)
139139
$parameters = ['enabled' => $parameters];
140140
}
141141

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

144144
return $result['value'];
145145
}
@@ -151,7 +151,7 @@ public function setDelayEnabled($parameters)
151151
*/
152152
public function resetCooldown()
153153
{
154-
$result = $this->curl('POST', '/resetCooldown');
154+
$result = $this->curl('POST', 'resetCooldown');
155155

156156
return $result['value'];
157157
}

0 commit comments

Comments
 (0)