Forum Discussion

bgoh29's avatar
bgoh29
Occasional Contributor
3 years ago
Solved

how do I access value from query in Groovy script

How do I get value from the query in the Groovy script? 

 

I can get the values from the Response by using for example:

 

def driverId = context.expand('${userInfo#Response#$id}')

 

 

However, if I want to get the value of the query parameter to-date, in this example, what do I put in ${ }

 

  • bgoh29's avatar
    bgoh29
    3 years ago

     Thank you, KarelHusa, 

     

    I tried the #testcase#todate solution, didn't quite work for me. However, I discovered a custom properties tab where I can access the values I was looking for without adding too much work for myself.   My Groovy script will use the line of code context.expand to access those values using  #testStep#propertyname. Thank you for pointing me in the direction of looking for information.  

     

3 Replies

  • ChrisAdams's avatar
    ChrisAdams
    Champion Level 2

    Hi,

     

    Your question is not completely clear.

     

    My take is that you want to 'pull' a value from the Groovy script step and put that value into the 'to-date' parameter for the service call.

     

    The syntax for getting the result from a Groovy script step is.... ${Name of Groovy Script step#result}.  The '#result' bit is important, it tells SoapUI to get the returned result from the script.  Also ensure your Groovy script has a return statement at the end.  E.g.

     

     

    def someValueToReturn = '17-JAN-2022';
    
    return someValueToReturn;

     

  • KarelHusa's avatar
    KarelHusa
    Champion Level 3

    I would recommend to place the value (2022-02-01) into a TestCase property or some other variable which you can easilly access.

    Then you can:

    • use its value inside REST Request as e.g. ${#TestCase#toDate}
    • inside Groovy script as well (via context expansion or TestCase Java methods)

    Using SoapUI Java API from Groovy to reach the query parameter would be also an option, but probably harder to use and maintain.

     

    Best regards,

    Karel

     

    • bgoh29's avatar
      bgoh29
      Occasional Contributor

       Thank you, KarelHusa, 

       

      I tried the #testcase#todate solution, didn't quite work for me. However, I discovered a custom properties tab where I can access the values I was looking for without adding too much work for myself.   My Groovy script will use the line of code context.expand to access those values using  #testStep#propertyname. Thank you for pointing me in the direction of looking for information.