How I can use this code in my Automation ..? random number

how I can use this code in my automation to set it to a field , so every time I have new phone

var chars = [‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’];

var phone = “070”;

for (i = 0; i < 7; i++) {

var random = Math.floor(Math.random()* chars.length);

phone += chars[random];}

return phone

Have you created a test case which interacts with your web page? If not, create it first.

The test case may input, as an example, a string literal into the phone number field. Then we would be able to guide you how to modify so that it use the code snippet you presented above.

This not for Web its for App , with Appuim

Please try the following test case:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI/* originally proposed String randomPhoneNumber() {    String[] chars = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ]    String phone = "070";    for (int i = 0; i < 7; i++) {        int index = Math.floor(Math.random() * 10);        String ch = chars[index]        phone += ch;    }    return phone} *//* this is quicker */ String randomPhoneNumber() {    return "070" + String.format('%07d', (Math.random() * 10_000_000).intValue())}// generate 30 phone numbersStringBuilder sb = new StringBuilder()for (int i = 1; i <= 30; i++) {    sb.append("\n${i}: ${randomPhoneNumber()}")	}// display the result in the Katalon logWebUI.comment(sb.toString())

Here included two version of randomPhoneNumber method. The first one is similar to your original javascript snippet. The second one is my proposal. Both can produce “070” + a sequence of 7 digits at random.

The second one is quicker than the first one.

2 Likes

To be honest, I do it rather simply:

I have this in my import:
import org.apache.commons.lang.RandomStringUtils as RandomStringUtils

Then, I use this

WebUI.setText(findTestObject('{TestObject}'), RandomStringUtils.randomNumeric(10))

so you could append “070” to the front WebUI.setText(findTestObject('{TestObject}'), ("070") + RandomStringUtills.randomNumeric(7))

That’s what I use for a phone number. I also use this to randomize email addresses by appending RandomStringUtils.randomNumeric(3) to the email address.

1 Like