Execute JS always return null

Your current code:

This looks terrible to me. I can not understand the javascript code which lacks appropriate newline chars and indentations. You should make your code more readable.

Groovy supports “triple single quoted string”:
http://groovy-lang.org/syntax.html#_triple_single_quoted_string

and “triple double quoted string”:
http://groovy-lang.org/syntax.html#_triple_double_quoted_string

With these string representation you can write your javascript code embeded in Groovy Script with Newlines and indentations, like this:

String js = """
var query =\'{"authority":"https://login.microsoftonline.com/xxxx/","clientId":"axxx","scopes":"axxxx","homeAccountIdentifier":"xxxx"}\';
var i, results = []; 
for (i in localStorage) {
  if (localStorage.hasOwnProperty(i)) {
    if (i.match(query)||(!query && typeof i === "string")){
      value = JSON.parse(localStorage.getItem(i));
      results.push({val:value});
    }
  }
}
let obj = results[Object.keys(results)[0]];
obj = obj[Object.keys(obj)[0]];
obj["accessToken"]
"""

def a = WebUI.executeJavaScript(js, null)
println a

I think you should rewrite your code. Otherwise it’s too difficult to debug.

1 Like