@@ -432,19 +432,37 @@ protected function getElementPath($elementId)
432
432
return sprintf ('%s/element/%s ' , $ this ->url , $ elementId );
433
433
}
434
434
435
+ /**
436
+ * @param array $args
437
+ *
438
+ * @return array
439
+ */
440
+ private function prepareScriptArguments (array $ args )
441
+ {
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
+ }
448
+ }
449
+
450
+ return $ args ;
451
+ }
452
+
435
453
/**
436
454
* @param mixed $data
437
455
*
438
456
* @return mixed
439
457
*/
440
- private function webDriverElementMulti ($ data )
458
+ private function webDriverElementRecursive ($ data )
441
459
{
442
460
$ element = $ this ->webDriverElement ($ data );
443
461
if ($ element !== null ) {
444
462
return $ element ;
445
463
} elseif (is_array ($ data )) {
446
464
foreach ($ data as $ k => $ v ) {
447
- $ data [$ k ] = $ this ->webDriverElementMulti ($ v );
465
+ $ data [$ k ] = $ this ->webDriverElementRecursive ($ v );
448
466
}
449
467
}
450
468
@@ -453,21 +471,37 @@ private function webDriverElementMulti($data)
453
471
454
472
/**
455
473
* Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (synchronous)
474
+ *
475
+ * @param array{script: string, args: array} $jsonScript
476
+ *
477
+ * @return mixed
456
478
*/
457
- public function execute ($ jsonScript )
479
+ public function execute (array $ jsonScript )
458
480
{
481
+ if (isset ($ jsonScript ['args ' ])) {
482
+ $ jsonScript ['args ' ] = $ this ->prepareScriptArguments ($ jsonScript ['args ' ]);
483
+ }
484
+
459
485
$ result = parent ::execute ($ jsonScript );
460
486
461
- return $ this ->webDriverElementMulti ($ result );
487
+ return $ this ->webDriverElementRecursive ($ result );
462
488
}
463
489
464
490
/**
465
491
* Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (asynchronous)
492
+ *
493
+ * @param array{script: string, args: array} $jsonScript
494
+ *
495
+ * @return mixed
466
496
*/
467
- public function execute_async ($ jsonScript )
497
+ public function execute_async (array $ jsonScript )
468
498
{
499
+ if (isset ($ jsonScript ['args ' ])) {
500
+ $ jsonScript ['args ' ] = $ this ->prepareScriptArguments ($ jsonScript ['args ' ]);
501
+ }
502
+
469
503
$ result = parent ::execute_async ($ jsonScript );
470
504
471
- return $ this ->webDriverElementMulti ($ result );
505
+ return $ this ->webDriverElementRecursive ($ result );
472
506
}
473
507
}
0 commit comments