44 * @copyright 2004 Meta Platforms, Inc.
55 * @license Apache-2.0
66 *
7- * @package WebDriver
8- *
97 * @author Justin Bishop <jubishop@gmail.com>
108 */
119
1513
1614/**
1715 * Abstract WebDriver\AbstractWebDriver class
18- *
19- * @package WebDriver
2016 */
2117abstract class AbstractWebDriver
2218{
@@ -42,11 +38,19 @@ abstract class AbstractWebDriver
4238 private $ transientOptions ;
4339
4440 /**
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
4647 *
4748 * @return array
4849 */
49- abstract protected function methods ();
50+ protected function methods ()
51+ {
52+ return [];
53+ }
5054
5155 /**
5256 * Return array of obsolete method names and corresponding HTTP request methods
@@ -55,23 +59,24 @@ abstract protected function methods();
5559 */
5660 protected function obsoleteMethods ()
5761 {
58- return array () ;
62+ return [] ;
5963 }
6064
6165 /**
6266 * Constructor
6367 *
64- * @param string $url URL to Selenium server
68+ * @param string $url
6569 */
6670 public function __construct ($ url = 'http://localhost:4444/wd/hub ' )
6771 {
6872 $ this ->url = $ url ;
69- $ this ->transientOptions = array ();
73+ $ this ->transientOptions = [];
74+ $ this ->extensions = [];
7075 $ this ->curlService = ServiceFactory::getInstance ()->getService ('service.curl ' );
7176 }
7277
7378 /**
74- * Magic method which returns URL to Selenium server
79+ * Magic method which returns URL to server
7580 *
7681 * @return string
7782 */
@@ -81,7 +86,7 @@ public function __toString()
8186 }
8287
8388 /**
84- * Returns URL to Selenium server
89+ * Returns URL to server
8590 *
8691 * @return string
8792 */
@@ -117,7 +122,7 @@ public function getCurlService()
117122 */
118123 public function setTransientOptions ($ transientOptions )
119124 {
120- $ this ->transientOptions = is_array ($ transientOptions ) ? $ transientOptions : array () ;
125+ $ this ->transientOptions = is_array ($ transientOptions ) ? $ transientOptions : [] ;
121126 }
122127
123128 /**
@@ -128,6 +133,77 @@ public function getTransientOptions()
128133 return $ this ->transientOptions ;
129134 }
130135
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+
131207 /**
132208 * Serialize script arguments (containing web elements and/or shadow roots)
133209 *
@@ -167,7 +243,7 @@ protected function serializeArguments(array $arguments)
167243 *
168244 * @throws \WebDriver\Exception if error
169245 */
170- protected function curl ($ requestMethod , $ command , $ parameters = null , $ extraOptions = array () )
246+ protected function curl ($ requestMethod , $ command , $ parameters = null , $ extraOptions = [] )
171247 {
172248 if ($ parameters && is_array ($ parameters ) && $ requestMethod !== 'POST ' ) {
173249 throw WebDriverException::factory (
@@ -181,7 +257,7 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
181257 );
182258 }
183259
184- $ url = sprintf ( ' %s%s ' , $ this ->url , $ command) ;
260+ $ url = $ this ->url . $ command ;
185261
186262 if ($ parameters && (is_int ($ parameters ) || is_string ($ parameters ))) {
187263 $ url .= '/ ' . $ parameters ;
@@ -196,7 +272,7 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
196272 array_replace ($ extraOptions , $ this ->transientOptions )
197273 );
198274
199- $ this ->transientOptions = array () ;
275+ $ this ->transientOptions = [] ;
200276
201277 $ httpCode = $ info ['http_code ' ];
202278
@@ -260,63 +336,12 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
260336 ?: $ this ->offsetGet ('sessionId ' , $ value )
261337 ?: $ this ->offsetGet ('webdriver.remote.sessionid ' , $ value );
262338
263- return array (
339+ return [
264340 'value ' => $ value ,
265341 'info ' => $ info ,
266342 'sessionId ' => $ sessionId ,
267343 '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+ ];
320345 }
321346
322347 /**
0 commit comments