I'm having trouble determining if this issue is related to PhantomJS or just plain Javascript.
My current setup involves using Ghostdriver to open a webpage and attempt to retrieve the response headers. GhostDriver runs the javascript and uses the onResourceReceived event for this purpose.
This is my code snippet:
String responsescript =
"var page = this,"+
"jsonResponse = \"\";"+
"page.onResourceReceived = function (res) {"+
"console.log(JSON.stringify(res));" /* The logging part works, but I need to somehow pass this data back to my Java program. I attempted the following alternative approach, which unfortunately did not work */
"jsonResponse = jsonResponse + JSON.stringify(res, undefined, 4);"+
"};"+
"function getJsonResponse(){"+
"return jsonResponse;"+
"}";
ghostDriver.executePhantomJS(responsescript);
ghostDriver.get("cnn.com");
ghostDriver.executePhantomJS("getJsonResponse();");
However, I keep encountering the same error message:
{message=Can't find variable: getJsonResponse, line=1, stack=ReferenceError: Can't find variable: getJsonResponse
All I want to do is store the response headers in a String variable in Java so that I can search for JSESSIONID within it.
Due to my limited knowledge of Javascript, I am struggling to resolve this seemingly simple problem.