Skip to content

Commit c632618

Browse files
mvorisekrobocoder
authored andcommitted
Add support for wd element object as execute args
1 parent fb74db5 commit c632618

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

lib/WebDriver/Session.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,19 +432,37 @@ protected function getElementPath($elementId)
432432
return sprintf('%s/element/%s', $this->url, $elementId);
433433
}
434434

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+
435453
/**
436454
* @param mixed $data
437455
*
438456
* @return mixed
439457
*/
440-
private function webDriverElementMulti($data)
458+
private function webDriverElementRecursive($data)
441459
{
442460
$element = $this->webDriverElement($data);
443461
if ($element !== null) {
444462
return $element;
445463
} elseif (is_array($data)) {
446464
foreach ($data as $k => $v) {
447-
$data[$k] = $this->webDriverElementMulti($v);
465+
$data[$k] = $this->webDriverElementRecursive($v);
448466
}
449467
}
450468

@@ -453,21 +471,37 @@ private function webDriverElementMulti($data)
453471

454472
/**
455473
* 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
456478
*/
457-
public function execute($jsonScript)
479+
public function execute(array $jsonScript)
458480
{
481+
if (isset($jsonScript['args'])) {
482+
$jsonScript['args'] = $this->prepareScriptArguments($jsonScript['args']);
483+
}
484+
459485
$result = parent::execute($jsonScript);
460486

461-
return $this->webDriverElementMulti($result);
487+
return $this->webDriverElementRecursive($result);
462488
}
463489

464490
/**
465491
* 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
466496
*/
467-
public function execute_async($jsonScript)
497+
public function execute_async(array $jsonScript)
468498
{
499+
if (isset($jsonScript['args'])) {
500+
$jsonScript['args'] = $this->prepareScriptArguments($jsonScript['args']);
501+
}
502+
469503
$result = parent::execute_async($jsonScript);
470504

471-
return $this->webDriverElementMulti($result);
505+
return $this->webDriverElementRecursive($result);
472506
}
473507
}

0 commit comments

Comments
 (0)