@@ -280,7 +280,7 @@ public function getWindowNames(): array
280280
281281 public function getWindowName (): string
282282 {
283- $ name = ( string ) $ this ->evaluateScript ('window.name ' );
283+ $ name = $ this -> getAsString ( $ this ->evaluateScript ('window.name ' ), ' Window name ' );
284284
285285 if ($ name === '' ) {
286286 $ name = self ::W3C_WINDOW_HANDLE_PREFIX . $ this ->getWebDriver ()->getWindowHandle ();
@@ -317,22 +317,22 @@ public function getText(
317317 return trim (str_replace (
318318 ["\r\n" , "\r" , "\n" , "\xc2\xa0" ],
319319 ' ' ,
320- $ this ->getElementDomProperty ($ this ->findElement ($ xpath ), 'innerText ' )
320+ $ this ->getAsString ( $ this -> getElementDomProperty ($ this ->findElement ($ xpath ), 'innerText ' ), ' The element \' s innerText ' )
321321 ));
322322 }
323323
324324 public function getHtml (
325325 #[Language('XPath ' )]
326326 string $ xpath
327327 ): string {
328- return $ this ->getElementDomProperty ($ this ->findElement ($ xpath ), 'innerHTML ' );
328+ return $ this ->getAsString ( $ this -> getElementDomProperty ($ this ->findElement ($ xpath ), 'innerHTML ' ), ' The element \' s innerHTML ' );
329329 }
330330
331331 public function getOuterHtml (
332332 #[Language('XPath ' )]
333333 string $ xpath
334334 ): string {
335- return $ this ->getElementDomProperty ($ this ->findElement ($ xpath ), 'outerHTML ' );
335+ return $ this ->getAsString ( $ this -> getElementDomProperty ($ this ->findElement ($ xpath ), 'outerHTML ' ), ' The element \' s outerHTML ' );
336336 }
337337
338338 public function getAttribute (
@@ -344,7 +344,8 @@ public function getAttribute(
344344 // so we cannot use webdriver api for this. See also: https://w3c.github.io/webdriver/#dfn-get-element-attribute
345345 $ escapedName = $ this ->jsonEncode ($ name , 'get attribute ' , 'attribute name ' );
346346 $ script = "return arguments[0].getAttribute( $ escapedName) " ;
347- return $ this ->executeJsOnXpath ($ xpath , $ script );
347+ $ result = $ this ->executeJsOnXpath ($ xpath , $ script );
348+ return $ result === null ? null : $ this ->getAsString ($ result , "The element's $ name attribute " );
348349 }
349350
350351 /**
@@ -412,7 +413,7 @@ public function setValue(
412413 if (is_array ($ value )) {
413414 $ this ->deselectAllOptions ($ element );
414415 foreach ($ value as $ option ) {
415- $ this ->selectOptionOnElement ($ element , $ option , true );
416+ $ this ->selectOptionOnElement ($ element , $ this -> getAsString ( $ option, ' Option value ' ) , true );
416417 }
417418 return ;
418419 }
@@ -941,12 +942,13 @@ private function charToSynOptions($char, ?string $modifier = null): string
941942 * Executes JS on a given element - pass in a js script string and argument[0] will
942943 * be replaced with a reference to the result of the $xpath query
943944 *
944- * @param string $xpath the xpath to search with
945- * @param string $script the script to execute
945+ * Example:
946+ * ```
947+ * $this->executeJsOnXpath($xpath, 'return argument[0].childNodes.length');
948+ * ```
946949 *
947950 * @return mixed
948951 * @throws DriverException
949- * @example $this->executeJsOnXpath($xpath, 'return argument[0].childNodes.length');
950952 */
951953 private function executeJsOnXpath (
952954 #[Language('XPath ' )]
@@ -960,11 +962,13 @@ private function executeJsOnXpath(
960962 /**
961963 * Executes JS on a given element - pass in a js script string and argument[0] will contain a reference to the element
962964 *
963- * @param RemoteWebElement $element the webdriver element
964- * @param string $script the script to execute
965+ * Example:
966+ * ```
967+ * $this->executeJsOnElement($element, 'return argument[0].childNodes.length');
968+ * ```
969+ *
965970 * @return mixed
966971 * @throws DriverException
967- * @example $this->executeJsOnXpath($xpath, 'return argument[0].childNodes.length');
968972 */
969973 private function executeJsOnElement (
970974 RemoteWebElement $ element ,
@@ -1257,5 +1261,21 @@ private function getElementDomProperty(RemoteWebElement $element, string $proper
12571261 }
12581262 }
12591263
1264+ /**
1265+ * @param mixed $value
1266+ * @throws DriverException
1267+ */
1268+ private function getAsString ($ value , string $ name ): string
1269+ {
1270+ if (!is_scalar ($ value )) {
1271+ $ actualType = gettype ($ value );
1272+ throw new DriverException (
1273+ "$ name should be a string or at least a scalar value, but received ` $ actualType` instead "
1274+ );
1275+ }
1276+
1277+ return (string )$ value ;
1278+ }
1279+
12601280 // </editor-fold>
12611281}
0 commit comments