Forum Discussion

garymags's avatar
garymags
Occasional Contributor
5 years ago

how to use a variable from a groovy script in the resource patch

Hi,

I found a knowledgebase article that explained how I can use a groovy script step in a testcase to generate a random uuid:

 

// Generating a random variable value.
def uuid = UUID.randomUUID()

// Logging the variable.
log.info uuid

 

So I did that and when I run that particular script in my test case, I see a UUID is being generated properly.

 

What I would like is to be able to use this output which is stored in a variable called uuid in my next step which is a REST request.  I need to pass the value of the variable in my resource path.  How can that be done?

 

Thanks,

Gary

 

  • garymags :

     

    Name in bold should be same as the name of your properties test step:

     

    testRunner.testCase.getTestStepByName("Propertes").setPropertyValue("uuid",uuid.toString())

     

    correct it and you are good to go 🙂

  • garymags : You can store the value in custom Properties as below 2 ways with in same groovy step :

     

    1. by adding Properties step in your test case and storing the value in properties step by following code:

    testRunner.testCase.getTestStepByName("Propertes").setPropertyValue("uuid",uuid.toString())

    and then in your rest test step you can use : context.expand('${Properties#uuid}') 

     

    2. by storing value in test case custom properties by following code:

    testRunner.testCase.setProppertyValue("uuid",uuid.toString())

    and then in your rest test step you can use : context.expand('${#TestCase#uuid}')

     

     

    • garymags's avatar
      garymags
      Occasional Contributor

      HimanshuTayal Hi Himanshu, thank you for your reply.

       

      Sorry but I'm really new to Ready API so I was hoping you can confirm if I am doing this correctly.  I provided screenshoots of my testcase.  Can you tell me what I am doing wrong?  I am trying to follow your first recommendation.

       

      Gary 

      • HimanshuTayal's avatar
        HimanshuTayal
        Community Hero

        garymags :

         

        Refer attached screenshot how this should looks like,

         

         

        On rest request part, your attached image in not getting open, please re-attach it or simple use below syntax where you want to use uuid

         

        context.expand('${Properties#uuid}')

         

  • garymags's avatar
    garymags
    Occasional Contributor

    HimanshuTayal Hi, thank you so much for all your help.  It is greatly appreciated.

     

    It looks like the problem is the last line of the groovy script.  I can run the groovy script separately and it generates a uuid.  However, it has a problem when it gets to the last line of the script.  I followed exactly what you had recommended to put in the groovy script but I'm still getting the error.

     

    An error appears saying:

    java.lang.NullPointerException: Cannot invoke method setPropertyValue() on null object error at line: 7

     

     

    // Generating a random variable value.
    def uuid = UUID.randomUUID()

     

    // Logging the variable.
    log.info uuid

     

    testRunner.testCase.getTestStepByName("Propertes").setPropertyValue("uuid",uuid.toString())

    • HimanshuTayal's avatar
      HimanshuTayal
      Community Hero

      garymags :

       

      Name in bold should be same as the name of your properties test step:

       

      testRunner.testCase.getTestStepByName("Propertes").setPropertyValue("uuid",uuid.toString())

       

      correct it and you are good to go 🙂

  • This works really well for us. Thanks for addressing this topic. I was looking for the information regarding the same. Keep sharing the info dqfanfeedback, kudos.