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)
System.out.println(a)
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.
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.
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,
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