Inject JS activity with JSON.parse throws error "JSON undefined"

Hi,

I am trying to pass multiple parameters to the Inject JS activity by wrapping these in a JSON string and passing that to the function. However, I keep getting the error “‘JSON’ not defined”.

For some reason it does not recognise “JSON” but sees this as an undefined variable.
I’ve seen multiple examples on the web and in this forum that use the JSON object in scripts.
Do I need a reference somewhere or am I missing anything else?

My JSON string:

{“userName”:“myusername”,“password”:“mypassword”}

My JS script:

function(e, jsonString){
var jsonObj = JSON.parse(jsonString);

document.forms['myForm'].userName.value = jsonObj.userName;
document.forms['myForm'].password.value = jsonObj.password;
document.forms['myForm'].submit();

}

Thanks for any insights you can provide!

Regards,
Dimitri

Hi,

In my environment, JSON.parse works well.
Can you share more details such as version of Studio and UiAutomation package, browser and screenshot of the activity (properties) etc?

Regards,

hi Yoichi,

It has been fixed. Your questions triggered me to find the fix.

The Inject JS activity runs in a “open browser” activity but I had not specified the browser type (chrome is my default so it opened in that and I did not notice the missing setting).

Once I set the browser type to chrome it works fine. Stupid oversight from my side.

But thanks for the questions, they made me think!

Regards,
Dimitri

1 Like