Extension scripts, command not storing returned value into var

Hi All :slight_smile:

So I’ve been experimenting with Katalon recorder as a means of a quick dirty task, ones I don’t want in my automation framework.

For the most part, I’ve not ran into any issues but I’m struggling to pinpoint to why my extension script when executed, that is not storing the returned value into the variable of “x”

The log message shows as below:

[info] Executing: | storeRandomString | | x |

[info] Store ‘RandomValue’ into ‘’

My js file is as below:

 Selenium.prototype.getRandomString = function () {
   var length = 5;
   var chars = 'aA';
   var mask = '';
   if (chars.indexOf('a') > -1)
      mask += 'abcdefghijklmnopqrstuvwxyz';
   if (chars.indexOf('A') > -1)
      mask += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
   if (chars.indexOf('#') > -1)
      mask += '0123456789';
   if (chars.indexOf('!') > -1)
      mask += '~`!@#$%^&*()_+-={}[]:";\'<>?,./|\\';
   var result = '';
   for (var i = length; i > 0; --i)
      result += mask[Math.round(Math.random() * (mask.length - 1))];
   return result;
}

And I am calling the below command:

Any help in understanding why would be brilliant!!

Additional
withing my script you can see I’ve hardcoded the length and the chars, I was originally looking at passing in those two values, for example

`Selenium.prototype.getRandomString = function (length, chars) {

But looking at the docs for extension scripts examples, and searching the web, I wasn’t able to see any examples of how you pass the two parameters, i’ve seen examples with just one parameter. if any could show me via the target from within the recorder, would be brilliant!

all the best, and thanks in advance.

Hi, I personally use just “runScript” or “addScript” commands to run js. In such case You just put your code in target field and that’s it.

How You add this script? It’s the only command in your test?

Yeah, I tried that, which it just craps out the plugin :sleepy:, in which I had to restart the plugin…
Thank you.

But if you have any suggestions on how I can stop it crapping out when adding the script to the target that would be awesome? that’s if you get it working that way.

Does your code work in browser console? Did you try diffrent/simpler code. try to return some simple text to check if this will crush plugin.

Yeah i tired the examples provided within the documentation, which work via extension scripts, i havent tired extracting this to target, but i suspect its teh amount of code entered to why it craps out.

Yeah this works in the console and works via extension script as it returns the value, just that katalon isn’t saving it to a variable that I set, just says: [info] Store ‘RandomValue’ into ‘’

where “RandomValue” is the value returned in this case. just weird as I’ve followed the examples and documentation, which suggest i must be missing something for which katalon is expecting in the recorder, or infact it just isnt possible?

see below log:
image

And then the steps for which it relates too:

Thanks.

Try to restart Katalon or even browser. If it’s not help i’m afraid i can’t help you.

Done all that :slight_smile: its katalon itself

Thanks for your time, figured it out :slight_smile:

So for some bizzare reason, the extension script needs a target for the command for it to actually save it to the variable… so its as it needs a hook into it, to store the return value.

Strange, but working now… :man_shrugging:

Solution was just to remove the hardcode length, and pass that via the target instead, so now it shows as below:

image

Would be curious still how to pass two parameters via a target, and if any one could expand on why it has to be done this way with the issue i had above, just havent seen anything in the documentation on the matter, will take another look though.

1 Like

to pass 2 parameters or more try to just separate them with “,” it’s should be passing as a string or try to use “” to be sure it’s string then in your script use split

1 Like

That doesn’t seem to be the case, though you’d assume it would be, I will take a look around and see if anyone else has asked a similar question. Thanks.