How to incorporate Javascript into method or custom keyword

Window7, 64bit
Katalon Studio; Version: 6.0.6; Build: 4
Version 73.0.3683.86 (Official Build) (64-bit)

Back when we were still using Selenium IDE we were able to use the following JavaScript for creating random Social Insurance Numbers. How could this be incorporated into a method or custom keyword?

javascript{var validPrefix = new Array(1,2,3,4,5,6,7,9); var length = 9; var sin = new Array(length);sin[0] = validPrefix[Math.floor(Math.random()*validPrefix.length)]; var index = 1; while(index < length - 1){ sin[index] = Math.floor(Math.random()*9); index++; }var sum = 0; var pos = 1; while(pos < length-1){ var odd = sin[pos]*2; sum += odd > 9 ? odd - 9 : odd; sum += sin[pos-1]; pos += 2; } var checkdigit = ((Math.floor(sum/10)+1)*10 - sum)%10; sin[length-1] = checkdigit; storedVars[‘sinpo’] = sin.join("");}

runScript javascript{var validPrefix = new Array(1,2,3,4,5,6,7,9); var length = 9; var sin = new Array(length);sin[0] = validPrefix[Math.floor(Math.random()*validPrefix.length)]; var index = 1; while(index < length - 1){ sin[index] = Math.floor(Math.random()*9); index++; }var sum = 0; var pos = 1; while(pos < length-1){ var odd = sin[pos]*2; sum += odd > 9 ? odd - 9 : odd; sum += sin[pos-1]; pos += 2; } var checkdigit = ((Math.floor(sum/10)+1)*10 - sum)%10; sin[length-1] = checkdigit; storedVars[‘sinpo’] = sin.join("");} ${sinpo}
echo ${sinpo}

There’s a method in the WebUI class for this:

https://docs.katalon.com/katalon-studio/docs/webui-execute-javascript.html

Thanks for your reply!

Hi,
I am not having any luck getting "WebUI.executeJavaScript() to work with my attached Javascript.
I am not sure of the syntax to use… The script works fine in Chrome when loaded. Just not sure how to encapsulate it in "WebUI.executeJavaScript(). Can anyone give me an example?

WorksSIN.html (733 Bytes)

Do it like this:

WebUI.executeJavaScript('''
// line 1
// line 2
// etc...
''', Arrays.asList(arg1, ..., argN))

Arrays.asList can be null if you don’t have any arguments.

Thanks for the pointers Russ - got it working.

The following JavaScript prints ‘mySin’ to the opened browser; but how do I use ‘mySin’ in Katalon Studio test cases? For example I want to insert mySin into a SIN input field:
“WebUI.setText(findTestObject(SinInputField), mySin)”

I’ve used https://docs.katalon.com/katalon-studio/docs/webui-execute-javascript.html to try a number of suggestions but I am not having any luck.

WebUI.executeJavaScript(’{var validPrefix = new Array(1, 2, 3, 4, 5, 6, 7, 9); var length = 9; var sin = new Array(length); sin[0] = validPrefix[Math.floor(Math.random() * validPrefix.length)]; var index = 1; while (index < length - 1) {sin[index] = Math.floor(Math.random() * 9); index++; } var sum = 0; var pos = 1; while (pos < length - 1) { var odd = sin[pos] * 2; sum += odd > 9 ? odd - 9 : odd; sum += sin[pos - 1]; pos += 2; } var checkdigit = ((Math.floor(sum / 10) + 1) * 10 - sum) % 10; sin[length - 1] = checkdigit; var mySin = (sin.join("")); document.write(mySin);}’, null)

1 Like

Try this thread Dave… if it doesn’t help, come back.

Read from the linked response and then on down…

Thanks for your help & responses Russ.

I read your reply, but I’m still not clear as to how I would return var ‘mySin’ back to Katalon.
Sorry if I am not getting your meaning.

I would have thought that something like the following might work, but it’s not.
WebElement SIN = WebUI.executeJavaScript(“return(‘mySin’);”, null)

Thanks,
Dave

Well, one way is to extract your JavaScript out of your sin file and drop it right between the two sets of triple-single-quotes (much as I hinted at above)

'''
line 1;
line 2;
etc;
return mySin;
'''

That assumes there is a JavaScript var called mySin.

Now you wrap the triple-quoted code block in a call to executeJavaScript, something like…

String mySin = WebUI.executeJavaScript(
'''
line 1;
line 2;
etc;
return mySin;
''',
null)

Notice I’m not using WebElement - I don’t think that’s what you want… I think your SIN is a string of digits, right?

Now you can do…

WebUI.setText(findTestObject(...), mySin)

or whatever.

Man you rock!
I missed what you where saying.
It works as expected.

Have a great weekend!
Best regards,
Dave