Forum Discussion

diego_marconato's avatar
New Contributor
9 years ago

Generate a random id used several times in the request

Hello, in my project SOAPUI use the Custom Properties to define a unique id which then use the SOAP request.

My problem is that is used than once in the request and it changes every time.
This is RandomId in request: ${#Project#RandomId}
How can I do so that the random id is always unique even if used repeatedly?
Thanks in advance

5 Replies

  • rupert_anderson's avatar
    Valued Contributor



    Is it that for each execution of a TestCase you want to generate 1 random id and use it across more than one TestStep?


    If so, in your TestCase:

    1) Insert a Groovy TestStep as the first TestStep

    2) in the Groovy TestStep generate a random number and store it in a property in the TestCase's context variable e.g.

    context["randomId"]=random number code

    3) You can than use the generated randomId property multiple times(in that execution of the TestCase) without its value changing. Use the property expansion syntax to reference itin requests etc e.g.



    Make sense?




  • nmrao's avatar
    Champion Level 3

    Not sure if I understand completely.


    Assuming below scenario:

    • A test case - containing a step - which uses property expansion such as <element>${#TestCase#UNIQUE_ID}</element>
    • And the case can be executed many times as needed.

    In this case, I may even not use the random number, instead I would go for a sequence like below

    1. Define two custom properties, say PREFIX, UNIQUE_ID
    2. Set Initial Value for PREFIX as say Test1, can be any of your choice. this will help you identify different test sets. you can change it to Test2, Test3 as needed.
    3. Set Initial Value for UNIQUE_ID as 0.
    4. Add the below script as Setup script of the test case. This reads existing value and increments it by one and store it back.


    def val = context.testCase.getPropertyValue('UNIQUE_ID') as Integer
    context.testCase.setPropertyValue('UNIQUE_ID', (val+1 as String))

    In the test request of test case, use as <element>${#TestCase#PREFIX}_${#TestCase#UNIQUE_ID}</element>


    The above would generate values incrementally as Test1_1, Test1_2 etc.,



    This does not require any additional test steps

    Coveat is that you need to run test case always in order to get the incremented value, not run as individual test step.


    Hope this helps.

    • diegomarconato's avatar
      Occasional Visitor

      My problem is that I use UNIQUE_ID several times in the SOAP request of the Test Case.
      So it happens that I generate two different UINIQUE_ID. I want to create the same again UNIQUE_ID

      • rupert_anderson's avatar
        Valued Contributor

        So another way of saying what you want is when your TestCase is run:

        1. You want to generate a unique id (once per TestCase run)
        2. Then use that id multiple times in the same TestCase execution without the value changing.
        3. On the next run of the TestCase, a new id will be generated

        Does this make sense and match what you want?


        If so please see either my or Rao's answer. Mine uses a Groovy TestStep at the beginning,  his uses a setup script - both achieve the same result.