0

I've tried this so far and it always returns "null"

    WebDriver driver = new FirefoxDriver();


    driver.get("http://localhost/index.php");


    driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);

    Object response = ((JavascriptExecutor) driver).executeScript("Debug.gameControls.debugSpinStart({\"positions\":[16,27,25,19,26],\"reelsIndex\":0});$.getJSON");

    System.out.println(response); 

Any suggestions ?

SolidState
  • 9
  • 1
  • 4
  • Use `.stringify()` function that javascript provides to convert object to json. [Here's an example](http://stackoverflow.com/a/4162803/4180674). Is this what you are looking for? – giri-sh Sep 11 '15 at 15:05

2 Answers2

2

You do not return anything from your JavaScript code so you'll always get null. You'd need something like:

Object response = ((JavascriptExecutor) driver).executeScript("return ...")
Louis
  • 146,715
  • 28
  • 274
  • 320
-2

$getJSON takes callback, assuming you using it call a resource, below code can be used to return json data in string format.

JSON.stringify() converts json object into string within executescript scope. Then executeScript returns this value and it gets assigned to response Object below.

Object response = ((JavascriptExecutor) driver).executeScript("Debug.gameControls.debugSpinStart({\"positions\":[16,27,25,19,26],\"reelsIndex\":0});" + 
"$.getJSON('/resource/data.json',function(data){ return JSON.stringify(data) });")

And parse the string to Json using jsonparser

JsonElement jelement = new JsonParser().parse((String) response);
JsonObject  jobject = jelement.getAsJsonObject();  

Both above types require import of com.google.json.

Shambu
  • 2,612
  • 1
  • 21
  • 16
  • Which package should I import ? https://gyazo.com/e0cad542383c0b0f0daa3a90235602ba – SolidState Sep 12 '15 at 06:47
  • Try com.google.gson . Link to this Gson api -> https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonParser.html and this stackoverflow answer has more information http://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java – Shambu Sep 12 '15 at 14:56
  • None of these make any sense to me . The guy from that thread is parsing sample Json which is already a string variable . In my case there's no string at this time . I need to convert Json coming from the javascript execution to a string . How can I achieve this ? Can you please elaborate ? – SolidState Sep 14 '15 at 06:37
  • if you online we can chat on https://chat.stackoverflow.com/rooms/89567/returningjson . Object response is capturing string (created from JSON) by Javascript executor. – Shambu Sep 14 '15 at 09:42
  • It says that I don't have enough reputation to use the chat . – SolidState Sep 14 '15 at 10:41
  • I have updated the answer adding more details. Hope this helps now – Shambu Sep 14 '15 at 12:18
  • Here's a sample of my code and it does not print anything . It doesn't even throw exception . What does this mean ? – SolidState Sep 15 '15 at 07:06
  • https://gyazo.com/0cdd810392a9b5eb14ea282559c46341 - It outputs nothing , not even and exception . – SolidState Sep 15 '15 at 07:17
  • And here is a sample of the json : https://gyazo.com/d06ac25db7a92afe0255a0b59640d886 – SolidState Sep 15 '15 at 07:18
  • the script within executeScript should have return statement and yours doesn't have that.How are you capturing the json within javascript? I mean where is that json created or returned within javascript? say if you have a function that generates/returns that json,then capture it into variable and then call return statement returning that variable. So it would be like var captureJson = functionthatreturnsJson(); return captureJson; – Shambu Sep 15 '15 at 11:44