Skip to content

Commit 8bb204d

Browse files
committed
Coding style + fix to PR #11
1 parent 459a42a commit 8bb204d

File tree

1 file changed

+56
-43
lines changed

1 file changed

+56
-43
lines changed

Diff for: lib/WebDriver/Session.php

+56-43
Original file line numberDiff line numberDiff line change
@@ -425,83 +425,96 @@ public function log()
425425
}
426426

427427
/**
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
429433
*/
430-
protected function getElementPath($elementId)
434+
public function execute(array $jsonScript)
431435
{
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);
433443
}
434444

435445
/**
436-
* @param array $args
446+
* Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (asynchronous)
437447
*
438-
* @return array
448+
* @param array{script: string, args: array} $jsonScript
449+
*
450+
* @return mixed
439451
*/
440-
private function prepareScriptArguments(array $args)
452+
public function execute_async(array $jsonScript)
441453
{
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']);
448456
}
449457

450-
return $args;
458+
$result = $this->curl('POST', '/execute_async', $jsonScript);
459+
460+
return $this->unserializeResult($result);
451461
}
452462

453463
/**
454-
* @param mixed $data
455-
*
456-
* @return mixed
464+
* {@inheritdoc}
457465
*/
458-
private function webDriverElementRecursive($data)
466+
protected function getElementPath($elementId)
459467
{
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);
470469
}
471470

472471
/**
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)
474473
*
475-
* @param array{script: string, args: array} $jsonScript
474+
* @see https://w3c.github.io/webdriver/#executing-script
476475
*
477-
* @return mixed
476+
* @param array $arguments
477+
*
478+
* @return array
478479
*/
479-
public function execute(array $jsonScript)
480+
private function serializeArguments(array $arguments)
480481
{
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+
}
484488

485-
$result = parent::execute($jsonScript);
489+
if (is_array($value)) {
490+
$arguments[$key] = $this->serializeArguments($value);
491+
}
492+
}
486493

487-
return $this->webDriverElementRecursive($result);
494+
return $arguments;
488495
}
489496

490497
/**
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)
492499
*
493-
* @param array{script: string, args: array} $jsonScript
500+
* @param mixed $result
494501
*
495502
* @return mixed
496503
*/
497-
public function execute_async(array $jsonScript)
504+
private function unserializeResult($result)
498505
{
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;
501510
}
502511

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+
}
504517

505-
return $this->webDriverElementRecursive($result);
518+
return $result;
506519
}
507520
}

0 commit comments

Comments
 (0)