Creating a random string/int during run time to use in a web request


I’ve tried searching for help on this but can’t seem to find anything. Apologies if this has already been answered somewhere!

**I am making a web request which creates a user. As part of this request, a unique id needs to be passed in a field called “key” each time a user is created. What is the best/easiest way to create a random string each time I run my test which can be passed into the request?

I know I can create local variables in the “Web Service request” object which can parameterise the request, but how can i make this parameter random? Where can I set it from?

JSON being passed:

  "key": "string I want to make random",
  "userName": "dave",
"country": "GB"

random string is easy:

import org.apache.commons.lang.RandomStringUtilsString charset = (('A'..'Z') + ('0'..'9')).join()Integer length = 9def randomString = RandomStringUtils.random(length, charset.toCharArray())println randomString

next, change definition of requestObject like this:

{  "key": "${keyValue}",  "userName": "dave",  "country": "GB"}

in call use following:

response = WS.sendRequest(findTestObject('pathInObjectRepository/requestObject',[('keyValue'): randomString]))

*** edited to correct typo thanx for pointing that out @“Justin Harper”

Hi Justin,

the safest way how to have an unique key is to add current timestamp to it. See this example:

long ts = System.currentTimeMillis() / 1000LString json = '''{"key": "randomKey''' + ts + '''","userName": "dave","country": "GB"}'''println json

// output

{"key": "randomKey1533726180","userName": "dave","country": "GB"}

1 Like

Thanks @Andrej Podhajský @Marek Melocik , both of your answers are a really great help!

@Andrej Podhajský

hmm, when I try and run the request from the test case, I get the below error. I’ve tried adding the reference “import com.kms.katalon.core.testobject.RequestObject” but still getting the same error message:

08-08-2018 12:30:41 PM - [ERROR] - Test Cases/API Tests/Brands/001_Create a brand with status set to enabled FAILED because (of) groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords.sendRequest() is applicable for argument types: (com.kms.katalon.core.testobject.RequestObject, java.util.LinkedHashMap) values: [TestObject - ‘Object Repository/API Test Objects/Brands/001_Create a brand with status set to enabled’, …]
Possible solutions: sendRequest(com.kms.katalon.core.testobject.RequestObject), sendRequest(com.kms.katalon.core.testobject.RequestObject, com.kms.katalon.core.model.FailureHandling)

my fault i was using my old crappy memory…

response = WS.sendRequest(findTestObject('pathInObjectRepository/requestObject',[('keyValue'): randomString]))

Andrej Podhajský said:

my fault i was using my old crappy memory…

response = WS.sendRequest(findTestObject('pathInObjectRepository/requestObject',[('keyValue'): randomString]))


Thanks @Andrej Podhajský, works a charm!

you are welcome

can you use this random number using post request. in the HTTP body i would need to give the user ID random name.

Of course you can. Create unique variable and pass it to WS object.

The big question how to make the Variable random?
after i add ${variable} to the body. then i go to the variable tab. and now how do i make variable random?

I use this to make a simple random string. You only pass in a number which lets you control how many characters the string is:

def randomString(int stringLength) {
String charset = (('A'..'Z') + ('0'..'9')).join()
Integer length = stringLength
def randomString = RandomStringUtils.random(length, charset.toCharArray())
return randomString

or if you want a random int, the below line would give you a int between 999999 and 1000000.

int randomInt = Math.abs(new Random().nextInt()) % 999999 + 1000000

Hi @Eytan_Nakache,

Were you able to randomise the variable??
Thanks in advance!

Hi there, where should I put this code?
Thanks in advance!