@@ -425,83 +425,96 @@ public function log()
425
425
}
426
426
427
427
/**
428
- * {@inheritdoc}
428
+ * Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (synchronous)
429
+ *
430
+ * @param array{script: string, args: array} $jsonScript
431
+ *
432
+ * @return mixed
429
433
*/
430
- protected function getElementPath ( $ elementId )
434
+ public function execute ( array $ jsonScript )
431
435
{
432
- return sprintf ('%s/element/%s ' , $ this ->url , $ elementId );
436
+ if (isset ($ jsonScript ['args ' ])) {
437
+ $ jsonScript ['args ' ] = $ this ->serializeArguments ($ jsonScript ['args ' ]);
438
+ }
439
+
440
+ $ result = $ this ->curl ('POST ' , '/execute ' , $ jsonScript );
441
+
442
+ return $ this ->unserializeResult ($ result );
433
443
}
434
444
435
445
/**
436
- * @param array $args
446
+ * Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (asynchronous)
437
447
*
438
- * @return array
448
+ * @param array{script: string, args: array} $jsonScript
449
+ *
450
+ * @return mixed
439
451
*/
440
- private function prepareScriptArguments (array $ args )
452
+ public function execute_async (array $ jsonScript )
441
453
{
442
- foreach ($ args as $ k => $ v ) {
443
- if ($ v instanceof Element) {
444
- $ args [$ k ] = [Container::WEBDRIVER_ELEMENT_ID => $ v ->getID ()];
445
- } elseif (is_array ($ v )) {
446
- $ args [$ k ] = $ this ->prepareScriptArguments ($ v );
447
- }
454
+ if (isset ($ jsonScript ['args ' ])) {
455
+ $ jsonScript ['args ' ] = $ this ->serializeArguments ($ jsonScript ['args ' ]);
448
456
}
449
457
450
- return $ args ;
458
+ $ result = $ this ->curl ('POST ' , '/execute_async ' , $ jsonScript );
459
+
460
+ return $ this ->unserializeResult ($ result );
451
461
}
452
462
453
463
/**
454
- * @param mixed $data
455
- *
456
- * @return mixed
464
+ * {@inheritdoc}
457
465
*/
458
- private function webDriverElementRecursive ( $ data )
466
+ protected function getElementPath ( $ elementId )
459
467
{
460
- $ element = $ this ->webDriverElement ($ data );
461
- if ($ element !== null ) {
462
- return $ element ;
463
- } elseif (is_array ($ data )) {
464
- foreach ($ data as $ k => $ v ) {
465
- $ data [$ k ] = $ this ->webDriverElementRecursive ($ v );
466
- }
467
- }
468
-
469
- return $ data ;
468
+ return sprintf ('%s/element/%s ' , $ this ->url , $ elementId );
470
469
}
471
470
472
471
/**
473
- * Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (synchronous )
472
+ * Serialize script arguments (containing web elements )
474
473
*
475
- * @param array{script: string, args: array} $jsonScript
474
+ * @see https://w3c.github.io/webdriver/#executing-script
476
475
*
477
- * @return mixed
476
+ * @param array $arguments
477
+ *
478
+ * @return array
478
479
*/
479
- public function execute (array $ jsonScript )
480
+ private function serializeArguments (array $ arguments )
480
481
{
481
- if (isset ($ jsonScript ['args ' ])) {
482
- $ jsonScript ['args ' ] = $ this ->prepareScriptArguments ($ jsonScript ['args ' ]);
483
- }
482
+ foreach ($ arguments as $ key => $ value ) {
483
+ // Potential compat-buster, i.e., W3C-specific
484
+ if ($ value instanceof Element) {
485
+ $ arguments [$ key ] = [Container::WEBDRIVER_ELEMENT_ID => $ value ->getID ()];
486
+ continue ;
487
+ }
484
488
485
- $ result = parent ::execute ($ jsonScript );
489
+ if (is_array ($ value )) {
490
+ $ arguments [$ key ] = $ this ->serializeArguments ($ value );
491
+ }
492
+ }
486
493
487
- return $ this -> webDriverElementRecursive ( $ result ) ;
494
+ return $ arguments ;
488
495
}
489
496
490
497
/**
491
- * Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (asynchronous )
498
+ * Unserialize result (containing web elements )
492
499
*
493
- * @param array{script: string, args: array} $jsonScript
500
+ * @param mixed $result
494
501
*
495
502
* @return mixed
496
503
*/
497
- public function execute_async ( array $ jsonScript )
504
+ private function unserializeResult ( $ result )
498
505
{
499
- if (isset ($ jsonScript ['args ' ])) {
500
- $ jsonScript ['args ' ] = $ this ->prepareScriptArguments ($ jsonScript ['args ' ]);
506
+ $ element = $ this ->webDriverElement ($ result );
507
+
508
+ if ($ element !== null ) {
509
+ return $ element ;
501
510
}
502
511
503
- $ result = parent ::execute_async ($ jsonScript );
512
+ if (is_array ($ result )) {
513
+ foreach ($ result as $ key => $ value ) {
514
+ $ result [$ key ] = $ this ->unserializeResult ($ value );
515
+ }
516
+ }
504
517
505
- return $ this -> webDriverElementRecursive ( $ result) ;
518
+ return $ result ;
506
519
}
507
520
}
0 commit comments