Forum Discussion
Nastya_Khovrina
SmartBear Alumni (Retired)
Hi Sriraj,
Thank you for your post. You can use Groovy script to generate a random number or value. https://www.tutorialspoint.com/groovy/groovy_random.htmFor example, you can add the "TestRunListener.beforeStep" event for a project and use the following script to generate a random number and set the "ID" field:
import java.util.Random Random random = new Random() def min = 1 def max = 10 def randomValue = random.nextInt(max + 1 - min) + min; def props = testRunner.testCase.getTestStepByName("Request 1") if(props.hasProperty("ID")){ props.setPropertyValue("ID", String.valueOf(randomValue) ) log.info "ID was added: " + randomValue }
To generate a random value from a list:
import java.util.Random Random random = new Random() def min = 1 def max = 10 def list = [6,6,true,false,false,false] def randomValue = random.nextInt(list.size()) def props = testRunner.testCase.getTestStepByName("Request 1") if(props.hasProperty("ID")){ props.setPropertyValue("ID", String.valueOf(list[randomValue]) ) log.info "ID was added = " + list[randomValue] }
Anastasia
Customer Care Team
Sriraj
8 years agoContributor
Thanks for the note. I was looking for one liner code which does the job since i was intending to have that directly placed in the rest request.
I found the below to do the job
${=org.apache.commons.lang.RandomStringUtils.randomNumeric(5)}
Related Content
- 5 years ago
- 2 years ago
Recent Discussions
- 17 hours ago
- 17 days ago