I need to generate number with the exact digits requested

I am passing the phone number number of digits on global variable . and on the custom keyword I have the following code :

	//generate phone numbers
	static String generatePhoneNumber(String countryFormatString) {
		///def countryFormatString = CustomKeywords.'custom.readDataFromExcel'(fileName, sheetIndex, rowNum, celldef)
		//int countryFormat = (int)(countryFormatString)
		int countryFormatLength = (int)(countryFormatString.length())
		int generatedDigits = GlobalVariable.generatedNewNumberDigits - (countryFormatLength-1)
		int pow =((int)Math.pow(10,generatedDigits))
		int randomPow = (int)(Math.random() *pow)
		String randomPowString = randomPow.toString()
		String randomNo = countryFormatString + randomPowString
		String toString = randomNo.toString()
		return GlobalVariable.newPhoneNumber= toString

when I pass 8 digits and run the code , sometimes it generate 7 not 8 and that will fail immediately test case . I need to always get the exact passing digit number without any less or more

I see a few things that you might want to review.
First, you use the @Keyword to make this routine a CustomKeyword, but you use the identifier, static. You either can remove the @Keyword and just call the static method, or you remove the static and call the @Keyword. To use the static routine, leave off the “CustomKeyword” from your calling statement and the single quotes. To use the @Keyword, replace the static with public and keep the CustomKeyword and single quotes.

"use static method"
TestObject sentObj = my.Tools.createTestObject("input", 'id("Mailings_1__NumberSent")')

"use Keyword method"
CustomKeywords.'my.Tools.isElementNotVisible'(sentObj, 10)

Second, you are creating a phone number like it is an actual number into the gazillions, instead of like it was a String (and have GlobalVariable.newPhoneNumber set as a String because you really are NOT going to do Math, like add and subtract, on a phone number). One of the benefits of using a String is you can have leading zeros–that could disappear if you use a Number.

Maybe like:
	public String generatePhoneNumber(String countryFormatString) {

		int countryFormatLength = countryFormatString.length()
		int upperLimit = 9
		int choice = 0
		GlobalVariable.newPhoneNumber = ""

		Random rand = new Random()

		for (int cnt = 0; cnt < countryFormatLength; cnt++) {
			choice = rand.nextInt(upperLimit)
			GlobalVariable.newPhoneNumber = GlobalVariable.newPhoneNumber.toString().concat(choice.toString())

		return GlobalVariable.newPhoneNumber

Edit: I added a "toString()" onto the Global Variable because it may be considered an Object and I want to use the String method, "concat()".

I could go on, but how about trying to use String data type instead of Number data type for your phone number.

I use something like the following:


import org.apache.commons.lang.RandomStringUtils as RandomStringUtils

Test case body:

def PhoneNumber = "501" + RandomStringUtils.randomNumeric(7)
println ("PhoneNumber: " + PhoneNumber)
PhoneNumber: 5011234567
1 Like