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)â
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)
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?