From bbb0ab2df7834ee4fd0275d8ecb412a3c23f10c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Thu, 14 Apr 2022 17:02:48 +0200 Subject: [PATCH 1/2] Convert element to object from execute result --- lib/WebDriver/Session.php | 41 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/WebDriver/Session.php b/lib/WebDriver/Session.php index 1186745..4a5c814 100644 --- a/lib/WebDriver/Session.php +++ b/lib/WebDriver/Session.php @@ -34,8 +34,6 @@ * @method void forward() Navigates forward in the browser history, if possible. * @method void back() Navigates backward in the browser history, if possible. * @method void refresh() Refresh the current page. - * @method mixed execute($jsonScript) Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (synchronous) - * @method mixed execute_async($jsonScript) Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (asynchronous) * @method string screenshot() Take a screenshot of the current page. * @method array getCookie() Retrieve all cookies visible to the current page. * @method array postCookie($jsonCookie) Set a cookie. @@ -433,4 +431,43 @@ protected function getElementPath($elementId) { return sprintf('%s/element/%s', $this->url, $elementId); } + + /** + * @param mixed $data + * + * @return mixed + */ + private function webDriverElementMulti($data) + { + $element = $this->webDriverElement($data); + if ($element !== null) { + return $element; + } elseif (is_array($data)) { + foreach ($data as $k => $v) { + $data[$k] = $this->webDriverElementMulti($v); + } + } + + return $data; + } + + /** + * Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (synchronous) + */ + public function execute($jsonScript) + { + $result = parent::execute($jsonScript); + + return $this->webDriverElementMulti($result); + } + + /** + * Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (asynchronous) + */ + public function execute_async($jsonScript) + { + $result = parent::execute_async($jsonScript); + + return $this->webDriverElementMulti($result); + } } From 4154f2451d38c8d3e54b9be3a8eedd2c74d5917c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Thu, 14 Apr 2022 23:15:52 +0200 Subject: [PATCH 2/2] Add support for wd element object as execute args --- lib/WebDriver/Session.php | 46 ++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/WebDriver/Session.php b/lib/WebDriver/Session.php index 4a5c814..e11cab4 100644 --- a/lib/WebDriver/Session.php +++ b/lib/WebDriver/Session.php @@ -432,19 +432,37 @@ protected function getElementPath($elementId) return sprintf('%s/element/%s', $this->url, $elementId); } + /** + * @param array $args + * + * @return array + */ + private function prepareScriptArguments(array $args) + { + foreach ($args as $k => $v) { + if ($v instanceof Element) { + $args[$k] = [Container::WEBDRIVER_ELEMENT_ID => $v->getID()]; + } elseif (is_array($v)) { + $args[$k] = $this->prepareScriptArguments($v); + } + } + + return $args; + } + /** * @param mixed $data * * @return mixed */ - private function webDriverElementMulti($data) + private function webDriverElementRecursive($data) { $element = $this->webDriverElement($data); if ($element !== null) { return $element; } elseif (is_array($data)) { foreach ($data as $k => $v) { - $data[$k] = $this->webDriverElementMulti($v); + $data[$k] = $this->webDriverElementRecursive($v); } } @@ -453,21 +471,37 @@ private function webDriverElementMulti($data) /** * Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (synchronous) + * + * @param array{script: string, args: array} $jsonScript + * + * @return mixed */ - public function execute($jsonScript) + public function execute(array $jsonScript) { + if (isset($jsonScript['args'])) { + $jsonScript['args'] = $this->prepareScriptArguments($jsonScript['args']); + } + $result = parent::execute($jsonScript); - return $this->webDriverElementMulti($result); + return $this->webDriverElementRecursive($result); } /** * Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. (asynchronous) + * + * @param array{script: string, args: array} $jsonScript + * + * @return mixed */ - public function execute_async($jsonScript) + public function execute_async(array $jsonScript) { + if (isset($jsonScript['args'])) { + $jsonScript['args'] = $this->prepareScriptArguments($jsonScript['args']); + } + $result = parent::execute_async($jsonScript); - return $this->webDriverElementMulti($result); + return $this->webDriverElementRecursive($result); } }