public Response handle() throws Exception {
String script = getRequest().getPayload().getString("script");
JSONArray args = getRequest().getPayload().getJSONArray("args");
Object res = getWebDriver().executeScript(script, args);
Response resp = new Response();
resp.setSessionId(getSession().getSessionId());
resp.setStatus(0);
if (res instanceof RemoteObject) {
RemoteObject ro = (RemoteObject) res;
RemoteWebElement rwe = ro.getWebElement();
JSONObject jo = new JSONObject().put("ELEMENT", rwe.getReference());
resp.setValue(jo);
} else if (res instanceof Integer) {
resp.setValue(res);
} else if (res instanceof Boolean) {
resp.setValue(res);
} else if (res instanceof Collection) {
List<Object> rwes = new ArrayList<Object>();
Collection<Object> all = (Collection<Object>) res;
for (Object ro : all) {
if (ro instanceof RemoteObject) {
JSONObject
jo =
new JSONObject()
.put("ELEMENT", "" + ((RemoteObject) ro).getWebElement().getNodeId().getId());
rwes.add(jo);
} else {
rwes.add(ro);
}
}
resp.setValue(rwes);
} else {
resp.setValue(res);
}
return resp;
}