How can I use a JavaScript 'Social Insurance Number' Generator in Katalon Recorder?

Hi folks, I am looking to use a JavaScript ‘Social Insurance Number’ Generator in Katalon Recorder.
The following works as a Katalon Studio custom keyword but I am NOT sure as to how I can implement it in Katalon Recorder. Any tips or insights would be great - thanks in advance. I was thinking that it could be stored in a javasin.js file which could then be loaded and mapped to any test case…
I have been scouring through the forum but have not found any hints/solutions as of yet… Not sure of the file syntax to use… I am using Katalon Recorder 5.9.0.

package tools
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

//JavaScript 'Social Insurance Number' Generator
public class JavaSinGenerator {
	@Keyword
	public String getSinAsString() {
		String MySin = 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("")); return MySin;}', null)
 	}
 }

Hi @Elly_Tran or @vu.tran is there someone I can reach out to regarding this request?
My settings are:
Windows 11 Enterprise.
Katalon Studio Enterprise: Version: 9.10, Build 212
Chrome Version 120.0.6099.71 (Official Build) (32-bit)

2 Likes

I tried the following with no luck:

return {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("");}

Or better asked how can I use the following in Katalon Recorder:

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 &lt; length - 1){sin[index] = Math.floor(Math.random()*9); index++; }var sum = 0; var pos = 1; while(pos &lt; length-1){ var odd= sin[pos]*2; sum += odd &gt; 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['sin'] = sin.join("");}

Will the below commands work for you–store and storeEval.

Good idea @grylion54, Will try tomorrow :smiley_cat:

The following works for creating a 9 digit Canadian Social Insurance Number:

runScript|{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("")); return MySin;}|mySin

1 Like