How to create 'Social Insurance Number' (SIN) with JavaScript custom keyword

//First create the new JavaSinGenerator custom keyword

  • Start ‘Katalon Studio’
  • Open any test project
  • Click ‘File’ > ‘New’ > ‘Keyword’
  • Click Browse & select any existing package or create new package
  • Input a “Class Name”, for example: JavaSinGenerator
  • Click OK
  • Result: ‘JavaSinGenerator.groovy’ is created

Copy and paste the following code to the new custom keyword:

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)
 	}
 }
//Next add the following to the import section of your test case or use "CTRL+SHIFT+O"
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import tools.JavaSinGenerator as JavaSinGenerator

//Add the following to the body of your test case
//Open the "Console" view after running case to see results
WebUI.openBrowser('')
JavaSinGenerator JavaSIN = new JavaSinGenerator()
println("JavaSin: " + (JavaSIN.getSinAsString()));
//WebUI.setText(findTestObject('testObject'), JavaSIN.getSinAsString()) 
WebUI.closeBrowser()

2 Likes

hello,

why not use as this way
WebUI.openBrowser('')
def sin = CustomKeywords.'sin.generation.JavaSinGenerator.getSinAsString'()
println("JavaSin: " + sin);
WebUI.closeBrowser()

which country sin?
JavaSin: 983830456

Sure that works too :smile:

Thanks for adding to the tip!

//This is a 9 digit Canadian SIN which can be changed to what the end user needs.
WebUI.openBrowser('')
def sin = CustomKeywords.'tools.JavaSinGenerator.getSinAsString'()
println('JavaSin: ' + sin)
WebUI.closeBrowser()

For me the above code gives me an error.

My script:

My custom keyword

package nexus2
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import org.openqa.selenium.JavascriptExecutor as JavascriptExecutor

class JavaSinGenerator {

      @Keyword
      def static 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)
      }
    }

The error:

The error is pointing me to the below LOC:
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)

I thought it will be an import issue, hence manually added the import statement in my custom keyword as:
import org.openqa.selenium.JavascriptExecutor as JavascriptExecutor

CTRL+SHIFT+O remove this manually added import though, I don’t know why.

Note: I also tried adding the import org.openqa.selenium.JavascriptExecutor as JavascriptExecutor to my TC as well, but didn’t help.

Can anybody help?

Thanks! :slight_smile:

See how the custom keyword works by using the attached Katalon Project:

CustomKeywordExamples.zip (19.8 KB)

1 Like

Hi Dave,

Thanks for the .zip file. I know how custom keywords work. Is there something i did wrong from the above post?

Thanks

Did u try comparing my example to yours?
That’s where I would start.

Cheers,
Dave

Thanks a lot for the .zip file.

On comparison, i found that i was calling the custom keyword before launching the browser. Once the browser launched successfully, it then printed out the sin for me. Its weird the function call do not work until browser is launched .

Maybe Katalon requires the webdriver to initiate 1st and them execute javascript. Do you think this is how Katalon works?

Javascript has to run in a web browser
The SINGenerator keyword does not

1 Like