public JSONObject getScriptResponse(String script) throws JSONException {
return getScriptResponse(script, new JSONArray());
}
public JSONObject getScriptResponse(String script, JSONArray arguments) throws JSONException {
RemoteWebElement document = getDocument();
if (!context.isOnMainFrame()) {
arguments.put(new JSONObject().put("objectId", document.getRemoteObject().getId()));
arguments.put(new JSONObject().put("objectId", context.getWindow().getRemoteObject().getId()));
String contextObject = "{'document': arguments[" + (arguments.length() - 2) + "], 'window': arguments[" + (arguments.length() - 1) + "]}";
script = "with (" + contextObject + ") {" + script + "}";
}
JSONObject cmd = new JSONObject();
cmd.put("method", "Runtime.callFunctionOn");
cmd.put(
"params",
new JSONObject().put("objectId", document.getRemoteObject().getId())
.put("functionDeclaration", "(function() { " + script + "})")
.put("arguments", arguments)
.put("returnByValue", false));
JSONObject response = sendCommand(cmd);
checkForJSErrors(response);