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("");}
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?
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)”
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?