Forum Discussion

Sriraj's avatar
Sriraj
Contributor
8 years ago

Can i generate a random number without using a data source and have it passed in the rest request?

I need to generate random numbers which needs to be sent for a field call "ID" and there can be multiple occurrences of this field and thus i want to generate a random number for these tags where ever ID is present of a specified length .

 

Some thing like below is what i want to put where ever field ID is present.

 

"id": %STR(6,6,True,false,false,false),
"brand": "e-commerce",
"channel": "online", 

 

I see that it does not work when i tried and i do not want to use Data Source . Any alternate options?

  • While i different options , i found a way to do this and is as below.

     

    ${=org.apache.commons.lang.RandomStringUtils.randomNumeric(5)}

     

    Number 5 represents the length for the number.

  • While i different options , i found a way to do this and is as below.

     

    ${=org.apache.commons.lang.RandomStringUtils.randomNumeric(5)}

     

    Number 5 represents the length for the number.

  • Nastya_Khovrina's avatar
    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's avatar
      Sriraj
      Contributor

      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)}

  • nmrao's avatar
    nmrao
    Champion Level 3
    For example, to use a number between 1, and 1000 thousand, use the following as value for rest request itself. See if the below helps.
    "${= new Random.nextInt(1, 1000)}"
    • Sriraj's avatar
      Sriraj
      Contributor

      Throws me the below error . when i have it this way "id": "${= new Random.nextInt(1, 1000)}" ,

       

      "startup failed:
      Script3.groovy: 1: unable to resolve class Random.nextInt
      @ line 1, column 2.
      new Random.nextInt(1, 1000)
      ^
      org.codehaus.groovy.syntax.SyntaxException: unable to resolve class Random.nextInt
      @ line 1, column 2.
      at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:149)
      at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(ResolveVisitor.java:264)
      at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(ResolveVisitor.java:274)
      at org.codehaus.groovy.control.ResolveVisitor.transformConstructorCallExpression(ResolveVisitor.java:1049)
      at org.codehaus.groovy.control.ResolveVisitor.transform(ResolveVisitor.java:708)
      at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitExpressionStatement(ClassCodeExpressionTransformer.java:142)
      at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:42)
      at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:37)
      at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:166)
      at org.codehaus.groovy.control.ResolveVisitor.visitBlockStatement(ResolveVisitor.java:1320)
      at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:71)
      at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:104)
      at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:115)
      at org.codehaus.groovy.ast.ClassCodeExpressionTransformer.visitConstructorOrMethod(ClassCodeExpressionTransformer.java:53)
      at org.codehaus.groovy.control.ResolveVisitor.visitConstructorOrMethod(ResolveVisitor.java:203)
      at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:126)
      at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1086)
      at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:53)
      at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1263)
      at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:178)
      at org.codehaus.groovy.control.CompilationUnit$11.call(CompilationUnit.java:651)
      at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:931)
      at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:593)
      at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:542)
      at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
      at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
      at groovy.lang.GroovyShell.parseClass(GroovyShell.java:694)
      at groovy.lang.GroovyShell.parse(GroovyShell.java:706)
      at groovy.lang.GroovyShell.parse(GroovyShell.java:742)
      at groovy.lang.GroovyShell.parse(GroovyShell.java:733)
      at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.recompileWithNewName(SoapUIGroovyScriptEngine.java:155)
      at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:146)
      at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:95)
      at com.eviware.soapui.support.scripting.groovy.SoapUIProGroovyScriptEngineFactory$SoapUIProGroovyScriptEngine.run(SoapUIProGroovyScriptEngineFactory.java:79)
      at com.eviware.soapui.model.propertyexpansion.resolvers.EvalPropertyResolver.doEval(EvalPropertyResolver.java:165)
      at com.eviware.soapui.model.propertyexpansion.resolvers.EvalPropertyResolver.resolveProperty(EvalPropertyResolver.java:145)
      at com.eviware.soapui.model.propertyexpansion.PropertyExpander.expand(PropertyExpander.java:212)
      at com.eviware.soapui.model.propertyexpansion.PropertyExpander.expand(PropertyExpander.java:155)
      at com.eviware.soapui.model.propertyexpansion.PropertyExpander.expand(PropertyExpander.java:151)
      at com.eviware.soapui.model.propertyexpansion.PropertyExpander.expandProperties(PropertyExpander.java:134)
      at com.eviware.soapui.impl.wsdl.submit.filters.PropertyExpansionRequestFilter.filterAbstractHttpRequest(PropertyExpansionRequestFilter.java:47)
      at com.eviware.soapui.impl.wsdl.submit.filters.AbstractRequestFilter.filterRequest(AbstractRequestFilter.java:34)
      at com.eviware.soapui.impl.wsdl.submit.transports.http.HttpClientRequestTransport.filterRequest(HttpClientRequestTransport.java:410)
      at com.eviware.soapui.impl.wsdl.submit.transports.http.HttpClientRequestTransport.sendRequest(HttpClientRequestTransport.java:193)
      at com.eviware.soapui.impl.wsdl.WsdlSubmit.run(WsdlSubmit.java:126)
      at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)

      • nmrao's avatar
        nmrao
        Champion Level 3
        Sorry, there was typo.
        Change from:
        ${= new Random.nextInt(1, 1000)}

        to

        ${= new Random().nextInt(1, 1000)}