I am currently working on a plugin for crawljax that involves executing some JavaScript code, similar to the following:
String result = browser.executeJavaScript(script).toString();
The script code is as follows:
function getElementPosition(id) {
var element = document.getElementById(id);
return JSON.stringify(elementpos(findPosX(element), findPosY(element)));
}
function elementpos(x, y) {
elementpos = new Object();
elementpos.x = x;
elementpos.y = y;
return elementpos;
}
return getElementPosition("foo");
While this seems to run successfully, the result always comes back as null. Strangely enough, if I print out the same data using document.write, I receive a well-formatted JSON string like this:
{"x":8, "y":24}
Could it be that I am misunderstanding something? Is there some issue when dealing with JSON strings and Java? Since I don't have much experience in JavaScript, could it be that I am not permitted to return data in this manner?
I am carrying out these tests on Google Chrome version 25.
Note: I do not believe this problem is related to Crawljax itself since another plugin (written by someone else) utilizes a script that returns a JSON string without any issues...