@@ -152,32 +152,38 @@ public function quit() {
152
152
}
153
153
154
154
/**
155
- * Inject a snippet of JavaScript into the page for execution in the context
156
- * of the currently selected frame. The executed script is assumed to be
157
- * synchronous and the result of evaluating the script will be returned.
155
+ * Prepare arguments for JavaScript injection
158
156
*
159
- * @param string $script The script to inject.
160
- * @param array $arguments The arguments of the script.
161
- * @return mixed The return value of the script.
157
+ * @param array $arguments
158
+ * @return array
162
159
*/
163
- public function executeScript ( $ script , array $ arguments = array ()) {
164
- $ script = str_replace ( ' " ' , ' \" ' , $ script );
160
+ private function prepareScriptArguments ( array $ arguments)
161
+ {
165
162
$ args = array ();
166
163
foreach ($ arguments as $ arg ) {
167
164
if ($ arg instanceof WebDriverElement) {
168
165
array_push ($ args , array ('ELEMENT ' => $ arg ->getID ()));
169
166
} else {
170
- // TODO: Handle the case where arg is a collection
171
167
if (is_array ($ arg )) {
172
- throw new Exception (
173
- "executeScript with collection paramatar is unimplemented "
174
- );
168
+ $ arg = $ this ->prepareScriptArguments ($ arg );
175
169
}
176
170
array_push ($ args , $ arg );
177
171
}
178
172
}
173
+ return $ args ;
174
+ }
179
175
180
- $ params = array ('script ' => $ script , 'args ' => $ args );
176
+ /**
177
+ * Inject a snippet of JavaScript into the page for execution in the context
178
+ * of the currently selected frame. The executed script is assumed to be
179
+ * synchronous and the result of evaluating the script will be returned.
180
+ *
181
+ * @param string $script The script to inject.
182
+ * @param array $arguments The arguments of the script.
183
+ * @return mixed The return value of the script.
184
+ */
185
+ public function executeScript ($ script , array $ arguments = array ()) {
186
+ $ params = array ('script ' => $ script , 'args ' => $ this ->prepareScriptArguments ($ arguments ));
181
187
$ response = $ this ->executor ->execute ('executeScript ' , $ params );
182
188
return $ response ;
183
189
}
0 commit comments