Execute JS always return null

The below script is to get one key’s value in local storage. However it always return null. When I execute same JS on same browser’s session, it does return value.

    def a = WebUI.executeJavaScript('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"]',null)

On same browser session

1 Like

I am unable to read the code you showed.

Please use Code Formatting syntax to represent your code snippet better readable.


Thanks for sharing. I updated my post. Do you have any idea on my issue?

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”:

and “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));
let obj = results[Object.keys(results)[0]];
obj = obj[Object.keys(obj)[0]];

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

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

1 Like

Your code seems dependent on localStorage. The state of the localStorage will be heavily dependent on the environment. Your test script would require careful setup processing to make the localStorage appropriate for you test case. I do not think I can reproduce the state on my side. It would be too difficult for me to look at your code deep.

You should not ask others for help in this case. Please help yourself. Possibly you should insert some good old “debug print” statements and see how your js is working. For example,

console.log("obj : " + JSON.stringify(obj));

just trying to find return in your script to see if you allow to even return anything but … no luck so far …
in other words, i’m missing:

return (obj["accessToken"]);

on the end of your script

1 Like

Sorry for that. I converted the script to one line to see if no luck after the multiple lines failed. I also try to put them in a function and call that function different executeJavaScript command but not work. Let me put them in a variable and try again

I did not put them in a function so not sure if it needs return. However, let me try. Thank you

i think only groovy returns evaluation of last row as an value of method/function

1 Like

You DID put your code in a function. All JavaScript code inserted into the browser page is placed inside an anonymous function – actually, an IIFE:

So you MUST provide a return statement.


1 Like

Perfect!!! Many thanks to kazurayam, Andrej_Podhajsky, Russ_Thomas!