cancel
Showing results for 
Search instead for 
Did you mean: 

Newb Question: REST Insert Random Number

SOLVED
Highlighted
Contributor

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

 

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.



Regards,
Rao.

View solution in original post

2 REPLIES 2
Highlighted
Community Hero

 

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.



Regards,
Rao.

View solution in original post

Highlighted
Contributor

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

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