4
4
* @copyright 2004 Meta Platforms, Inc.
5
5
* @license Apache-2.0
6
6
*
7
- * @package WebDriver
8
- *
9
7
* @author Justin Bishop <jubishop@gmail.com>
10
8
*/
11
9
15
13
16
14
/**
17
15
* Abstract WebDriver\AbstractWebDriver class
18
- *
19
- * @package WebDriver
20
16
*/
21
17
abstract class AbstractWebDriver
22
18
{
@@ -42,11 +38,19 @@ abstract class AbstractWebDriver
42
38
private $ transientOptions ;
43
39
44
40
/**
45
- * Return array of supported method names and corresponding HTTP request methods
41
+ * @var array
42
+ */
43
+ private $ extensions ;
44
+
45
+ /**
46
+ * Return array of protocol methods
46
47
*
47
48
* @return array
48
49
*/
49
- abstract protected function methods ();
50
+ protected function methods ()
51
+ {
52
+ return [];
53
+ }
50
54
51
55
/**
52
56
* Return array of obsolete method names and corresponding HTTP request methods
@@ -55,23 +59,24 @@ abstract protected function methods();
55
59
*/
56
60
protected function obsoleteMethods ()
57
61
{
58
- return array () ;
62
+ return [] ;
59
63
}
60
64
61
65
/**
62
66
* Constructor
63
67
*
64
- * @param string $url URL to Selenium server
68
+ * @param string $url
65
69
*/
66
70
public function __construct ($ url = 'http://localhost:4444/wd/hub ' )
67
71
{
68
72
$ this ->url = $ url ;
69
- $ this ->transientOptions = array ();
73
+ $ this ->transientOptions = [];
74
+ $ this ->extensions = [];
70
75
$ this ->curlService = ServiceFactory::getInstance ()->getService ('service.curl ' );
71
76
}
72
77
73
78
/**
74
- * Magic method which returns URL to Selenium server
79
+ * Magic method which returns URL to server
75
80
*
76
81
* @return string
77
82
*/
@@ -81,7 +86,7 @@ public function __toString()
81
86
}
82
87
83
88
/**
84
- * Returns URL to Selenium server
89
+ * Returns URL to server
85
90
*
86
91
* @return string
87
92
*/
@@ -117,7 +122,7 @@ public function getCurlService()
117
122
*/
118
123
public function setTransientOptions ($ transientOptions )
119
124
{
120
- $ this ->transientOptions = is_array ($ transientOptions ) ? $ transientOptions : array () ;
125
+ $ this ->transientOptions = is_array ($ transientOptions ) ? $ transientOptions : [] ;
121
126
}
122
127
123
128
/**
@@ -128,6 +133,77 @@ public function getTransientOptions()
128
133
return $ this ->transientOptions ;
129
134
}
130
135
136
+ /**
137
+ * Register extension
138
+ *
139
+ * @param string $extension
140
+ * @param string $className
141
+ * @param string $path
142
+ */
143
+ public function register ($ extension , $ className , $ path )
144
+ {
145
+ if (class_exists ($ className , false )) {
146
+ $ this ->extensions [$ extension ] = [$ className , $ path ];
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Magic method that maps calls to class methods to execute WebDriver commands
152
+ *
153
+ * @param string $name Method name
154
+ * @param array $arguments Arguments
155
+ *
156
+ * @return mixed
157
+ *
158
+ * @throws \WebDriver\Exception if invalid WebDriver command
159
+ */
160
+ public function __call ($ name , $ arguments )
161
+ {
162
+ if (count ($ arguments ) > 1 ) {
163
+ throw WebDriverException::factory (
164
+ WebDriverException::JSON_PARAMETERS_EXPECTED ,
165
+ 'Commands should have at most only one parameter, which should be the JSON Parameter object '
166
+ );
167
+ }
168
+
169
+ if (count ($ arguments ) === 0 && is_array ($ this ->extensions ) && array_key_exists ($ name , $ this ->extensions )) {
170
+ $ className = $ this ->extensions [$ name ][0 ];
171
+
172
+ return new $ className ($ this ->url . '/ ' . $ this ->extensions [$ name ][1 ]);
173
+ }
174
+
175
+ if (preg_match ('/^(get|post|delete)/ ' , $ name , $ matches )) {
176
+ $ requestMethod = strtoupper ($ matches [0 ]);
177
+ $ webdriverCommand = strtolower (substr ($ name , strlen ($ requestMethod )));
178
+
179
+ $ this ->getRequestMethod ($ webdriverCommand ); // validation
180
+ } else {
181
+ $ webdriverCommand = $ name ;
182
+ $ requestMethod = $ this ->getRequestMethod ($ webdriverCommand );
183
+ }
184
+
185
+ $ methods = $ this ->methods ();
186
+
187
+ if (! in_array ($ requestMethod , (array ) $ methods [$ webdriverCommand ])) {
188
+ throw WebDriverException::factory (
189
+ WebDriverException::INVALID_REQUEST ,
190
+ sprintf (
191
+ '%s is not an available http request method for the command %s. ' ,
192
+ $ requestMethod ,
193
+ $ webdriverCommand
194
+ )
195
+ );
196
+ }
197
+
198
+ $ result = $ this ->curl (
199
+ $ requestMethod ,
200
+ '/ ' . $ webdriverCommand ,
201
+ array_shift ($ arguments )
202
+ );
203
+
204
+ return $ result ['value ' ];
205
+ }
206
+
131
207
/**
132
208
* Serialize script arguments (containing web elements and/or shadow roots)
133
209
*
@@ -167,7 +243,7 @@ protected function serializeArguments(array $arguments)
167
243
*
168
244
* @throws \WebDriver\Exception if error
169
245
*/
170
- protected function curl ($ requestMethod , $ command , $ parameters = null , $ extraOptions = array () )
246
+ protected function curl ($ requestMethod , $ command , $ parameters = null , $ extraOptions = [] )
171
247
{
172
248
if ($ parameters && is_array ($ parameters ) && $ requestMethod !== 'POST ' ) {
173
249
throw WebDriverException::factory (
@@ -181,7 +257,7 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
181
257
);
182
258
}
183
259
184
- $ url = sprintf ( ' %s%s ' , $ this ->url , $ command) ;
260
+ $ url = $ this ->url . $ command ;
185
261
186
262
if ($ parameters && (is_int ($ parameters ) || is_string ($ parameters ))) {
187
263
$ url .= '/ ' . $ parameters ;
@@ -196,7 +272,7 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
196
272
array_replace ($ extraOptions , $ this ->transientOptions )
197
273
);
198
274
199
- $ this ->transientOptions = array () ;
275
+ $ this ->transientOptions = [] ;
200
276
201
277
$ httpCode = $ info ['http_code ' ];
202
278
@@ -260,63 +336,12 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
260
336
?: $ this ->offsetGet ('sessionId ' , $ value )
261
337
?: $ this ->offsetGet ('webdriver.remote.sessionid ' , $ value );
262
338
263
- return array (
339
+ return [
264
340
'value ' => $ value ,
265
341
'info ' => $ info ,
266
342
'sessionId ' => $ sessionId ,
267
343
'sessionUrl ' => $ sessionId ? $ this ->url . '/session/ ' . $ sessionId : $ info ['url ' ],
268
- );
269
- }
270
-
271
- /**
272
- * Magic method that maps calls to class methods to execute WebDriver commands
273
- *
274
- * @param string $name Method name
275
- * @param array $arguments Arguments
276
- *
277
- * @return mixed
278
- *
279
- * @throws \WebDriver\Exception if invalid WebDriver command
280
- */
281
- public function __call ($ name , $ arguments )
282
- {
283
- if (count ($ arguments ) > 1 ) {
284
- throw WebDriverException::factory (
285
- WebDriverException::JSON_PARAMETERS_EXPECTED ,
286
- 'Commands should have at most only one parameter, which should be the JSON Parameter object '
287
- );
288
- }
289
-
290
- if (preg_match ('/^(get|post|delete)/ ' , $ name , $ matches )) {
291
- $ requestMethod = strtoupper ($ matches [0 ]);
292
- $ webdriverCommand = strtolower (substr ($ name , strlen ($ requestMethod )));
293
-
294
- $ this ->getRequestMethod ($ webdriverCommand ); // validation
295
- } else {
296
- $ webdriverCommand = $ name ;
297
- $ requestMethod = $ this ->getRequestMethod ($ webdriverCommand );
298
- }
299
-
300
- $ methods = $ this ->methods ();
301
-
302
- if (! in_array ($ requestMethod , (array ) $ methods [$ webdriverCommand ])) {
303
- throw WebDriverException::factory (
304
- WebDriverException::INVALID_REQUEST ,
305
- sprintf (
306
- '%s is not an available http request method for the command %s. ' ,
307
- $ requestMethod ,
308
- $ webdriverCommand
309
- )
310
- );
311
- }
312
-
313
- $ result = $ this ->curl (
314
- $ requestMethod ,
315
- '/ ' . $ webdriverCommand ,
316
- array_shift ($ arguments )
317
- );
318
-
319
- return $ result ['value ' ];
344
+ ];
320
345
}
321
346
322
347
/**
0 commit comments