Forum Discussion

CByler's avatar
CByler
Contributor
9 years ago
Solved

Newb Question: REST Insert Random Number

I have searched both here and via google trying to find the best way to generate random numbers in test cases.  I have attempted to work with strings such as the below:

 

${=(int)(Math.random()*99999)+100001}
((int)(Math.random()*(1999999999-1900000000)+1900000000))
 
I am testing a rest web service.  I am inserting a transaction and my request is as follows:
 
{"transaction":
[
{
"accountName":"Jim Jolly","accountIdentity":"2345","accountType":1,
"bankIdentification":
{"bankABA":"123123123","bankAcctNo":"1178907"},
"amount":10,
"transactionType":"c","secCode":"PPD","importID":"123",
"effectiveEntryDate":"${#Project#effectiveEntryDate}","consumerInHouseID":"CID",
"discretionaryData":"DD","isRecurring":false,"terminalCity":"TAMP",
"terminalState":"FL","cardExpirationDate":"1220",
"documentReferenceNumber":"123","processControlField":"PCF",
"itemResearchNumber":"4321"
}
]
}
 
I need to replace the bankAcctNo value of 1178907 with a random number between 3 and 17 each time.
 
What is the best way to accmplish this?  Groovy script, custom property, etc?
 
I would ultimately like to have this function available at the project level as I will have to write 500-800 test cases and want to be able to reuse this in each test case.  I am not familiar with Groovy and am very new to Ready API, so your patience and time are appreciated.
 
Thanks,
Cherice

 

  •  

    Random number between the range

     

    Math.abs(new Random().nextInt() % max + min)

    So, in your case - Math.abs(new Random().nextInt() % 17 + 3)

     

    You may have goovy script

    context.testCase.testSuite.project.setPropertyValue("ACCOUNT_NUMBER", (Math.abs(new Random().nextInt() % 17 + 3).toString())

    And you need to use ${#Project#ACCOUNT_NUMBER} where that is needed.

     

    Just thinking of the number to tests to make the changes like you mentioned, you may alternatively look at Events - FilterRequest to modify the request with the changes need to avoid bulk changes for each test case if possible. Currently can't say much as do not know about the data variations.

2 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

     

    Random number between the range

     

    Math.abs(new Random().nextInt() % max + min)

    So, in your case - Math.abs(new Random().nextInt() % 17 + 3)

     

    You may have goovy script

    context.testCase.testSuite.project.setPropertyValue("ACCOUNT_NUMBER", (Math.abs(new Random().nextInt() % 17 + 3).toString())

    And you need to use ${#Project#ACCOUNT_NUMBER} where that is needed.

     

    Just thinking of the number to tests to make the changes like you mentioned, you may alternatively look at Events - FilterRequest to modify the request with the changes need to avoid bulk changes for each test case if possible. Currently can't say much as do not know about the data variations.

    • CByler's avatar
      CByler
      Contributor

      Many thanks Rao, you gave me just what I was looking for!