cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate a random Number, String, AlphaNumeric string

Community Leader

How to generate a random Number, String, AlphaNumeric string

This below function will generate random Number, String, alphaNumeric string as what you pass in parameters. Refer below code and help yourself in generating random numbers

 

 

def num = generateRndString(10, "numeric");
log.info num
def str = generateRndString(10, "string");
log.info str
def alphaNum = generateRndString(10, "alphanumeric");
log.info alphaNum
testRunner.testCase.getTestStepByName("Properties").setPropertyValue("RndNum", num)
testRunner.testCase.getTestStepByName("Properties").setPropertyValue("RndString", str)
testRunner.testCase.getTestStepByName("Properties").setPropertyValue("RndAlpha", alphaNum)

def generateRndString(int num, String type){
	def randValue = "";
	if( type.equalsIgnoreCase("numeric") ){
		def alphaNumeric = ('0'..'9').join()
		randValue = RandomStringUtils.random(num, alphaNumeric)
		while (randValue.size()!=num)
		{
			randValue = RandomStringUtils.random(num, alphaNumeric)
		}		
	}
	else if( type.equalsIgnoreCase("string") ){
		def alphaNumeric = (('a'..'z')+('A'..'Z')).join()
		randValue = RandomStringUtils.random(num, alphaNumeric)
		while (randValue.size()!=num)
		{
			randValue = RandomStringUtils.random(num, alphaNumeric)
		}
	}
	else if( type.equalsIgnoreCase("alphanumeric") ){
		def alphaNumeric = (('0'..'9')+('a'..'z')+('A'..'Z')).join()
		randValue = RandomStringUtils.random(num, alphaNumeric)
		while (randValue.size()!=num)
		{
			randValue = RandomStringUtils.random(num, alphaNumeric)
		}
	}
	return randValue
}

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
2 REPLIES 2
Highlighted
Frequent Contributor

import org.apache.commons.lang3.RandomStringUtils
import org.codehaus.groovy.runtime.DateGroovyMethods

def getRandomAlphanumeric(int len, String format="MMddkkmm") {
  try {
	format.size() < 1 ? RandomStringUtils.randomAlphanumeric(len).toUpperCase() : \
	DateGroovyMethods.format(new Date(), format).concat(RandomStringUtils.randomAlphanumeric(len).toUpperCase())
} catch (Exception e) {
	throw new Exception(e)
 }
}
Highlighted
Community Leader

@aaronpliu : Could you please add a bit of description so that it will be helpful to understand for help seekers 🙂

Like what this code is doing and how we can use this


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Get a full Raw JMS request

Participate!

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors