cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Occasional Contributor

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

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

@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 🙂


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

View solution in original post

11 REPLIES 11
Community Hero

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

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

 

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Occasional Contributor

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

@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 

Community Hero

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

@garymags :

 

Refer attached screenshot how this should looks like,

 

HimanshuTayal_0-1599225074553.png

 

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

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Occasional Contributor

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

@HimanshuTayal Thanks again for your help.  Much appreciated.

 

This is what my resource path looks like now based on what you explained.  I need the uuid in that part of my resource path

 

/source-data/api/feeds/participants/context.expand('${Properties#uuid}')/complete

 

However, I am getting an error when it tries to run the added line in the Groovy script

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

Community Hero

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

Hey @garymags,

You dont need the 'context.expand' or the brackets or single quote marks in the path i reckon.

Currently you have the path setup as follows:

/source-data/api/feeds/participants/context.expand('${Properties#uuid}')/complete

So when this generates it will appear as follows:


/source-data/api/feeds/participants/context.expand('UUID VALUE')/complete

Whereas what i'm guessing you want generated is

/source-data/api/feeds/participants/UUID VALUE/complete

So what you want to setup as

/source-data/api/feeds/participants/${Properties#uuid}/complete



Actually. I just remembered that i had a soapui project from about 4 years back that included a GUID value in the path (sourced from a properties step). Ive just checked the project and this was exactly how i set it up. No 'context.expand', no brackets and no single quotes and thatll work fine.

Cheers,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Occasional Contributor

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

@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())

Community Hero

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

@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 🙂


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal

View solution in original post

Occasional Contributor

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

@HimanshuTayal  Thank you so much.  You are awesome!!

Community Hero

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

@garymags : I am so glad that it helped you in solving your problem 🙂

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
New Here?
Join us and watch the welcome video:
Announcements