cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Community Leader

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

Hi,

 

I have a slightly different approach.  There is nothing wrong with @HimanshuTayal's approach, but in my tests, I tend not to use 'get property' or 'set property'.  If I needed to create the value in one test and read in another test, then I probably would.  However, my tests tend to be self-contained, so I use groovy scripts that 'return' the value of interest and then 'read' these in a later step in the same test.

 

For example, I need to supply a random number as an order number in some API request.

 

Firstly, I create a Groovy script to generate a random number.  E.g.

 

Date date = new Date();
def now = date.getTime();

Random rnd = new Random();
rnd.setSeed(now);

return(rnd.nextInt(100));

note the use of return in the script.  If I wanted to use Custom Properties, I'd set it instead of retuning.  

 

In a later test step, I can then 'call' the random number groovy step to obtain the random value by using the below example...

 

   <soapenv:Body>
      <urn:submitOrder>
         <urn:order>
            <urn1:orderRef>${Get Random Number - Groovy Script#result}</urn1:orderRef>
            <!-- Other details omitted -->
         </urn:order>
      </urn:submitOrder>
   </soapenv:Body>

 

New Member

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

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.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors