cancel
Showing results for 
Search instead for 
Did you mean: 

groovy : context expand variable parametrize user defined variable

subhaD
Contributor

groovy : context expand variable parametrize user defined variable

I am trying to pass  variable in to context.expand to get data from response, having trouble with this, any quick response is appreciated

 

def aaa = context.expand( '${1st Call#Response#$[\'data\'][\'res\'][\'b\'][0][\'aaa\']}' )
def response = context.expand( '${2nd call#Response#$[\'data\'][\'res\'][\'reportDownloadUrls\'][\'547851482\']}' )

 

I want to pass aaa variable instead of 547851482

I am writing this in groovy script test step, help debug this

9 REPLIES 9
Lucian
Community Hero

Re: groovy : context expand variable parametrize user defined variable

I don't know how to help you with this.

 

However, if you would tell me what you are trying to achieve, I might be able to provide you with a different solution.



If my answer helped please click on the 'Accept as Solution' button.
subhaD
Contributor

Re: groovy : context expand variable parametrize user defined variable

The part of URL in the second call response is based on the value sent in first call

So am trying to parametrize the value for URL using the value from first call

Lucian
Community Hero

Re: groovy : context expand variable parametrize user defined variable

I think it would be cleaner to use an Property Transfer test step to extract a property and then just use it in the second call.



If my answer helped please click on the 'Accept as Solution' button.
TanyaYatskovska
Community Manager

Re: groovy : context expand variable parametrize user defined variable

Guru experts, we need your suggestions!

@avidCoder@nmrao@msiadak

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
msiadak
Community Hero

Re: groovy : context expand variable parametrize user defined variable

You can try something like what I've posted below. If it doesn't work, can you provide your response for the two calls so I can navigate the response with groovy and parse it out better? Even if it's a sample mocked up response. 

 

 

def aaa = context.expand( '${1st Call#Response#$[\'data\'][\'res\'][\'b\'][0][\'aaa\']}' )

def response = context.expand( '${2nd call#Response#$[\'data\'][\'res\'][\'reportDownloadUrls\'][\'$aaa\']}' )

 

Typically when working with strings, you can use $ and a variable name to make the string's contents respect it as a variable. So the two are the same below:

 

log.info ("Hello world, my name is Matthew.");
def name = "Matthew";
log.info("Hello world, my name is $name.");



---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
msiadak
Community Hero

Re: groovy : context expand variable parametrize user defined variable

Looking at this, it's not as simple as I thought it was. Still, to be a better help, can you provide sample or mocked up response that I can parse with groovy? That would help me a log, as I am unfamiliar with the syntax you are using in the content.expand statement. 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
subhaD
Contributor

Re: groovy : context expand variable parametrize user defined variable

the reportdownloadUrls[99999999] is the value am trying to extract.

but 99999999 is from 1st request response am trying to take it in variable and pass it to this value, but not sure how to pass it ??

 

def URL = context.expand( '${2nd call#Response#$[\'data\'][\'resource\'][\'reportDownloadUrls\'][\'999999999\']}' )

 

I wanna parametrize the 999999999 part 

msiadak
Community Hero

Re: groovy : context expand variable parametrize user defined variable

I understand that part, and I am tyring to help here as best I can with the limited information I have so far. The groovy script is doing a content expand with some sort of pathing that I am not familiar with, which makes this a lot more difficult for me to try to help.

 

If you could provide a mocked up response that you're actually trying to parse to run the groovy script against to troubleshoot as I go? That would be beyond helpful.

 

That being said, and while it may not be pretty, this MIGHT work for you. I cannot test it, since I don't have the response to parse and try:

 

def aaa = context.expand( '${1st Call#Response#$[\'data\'][\'res\'][\'b\'][0][\'aaa\']}' )

// This sets up the content expansion string to be a literal string, and add in the parameterized aaa variable. 
def respString = '''${2nd call#Response#$[\'data\'][\'res\'][\'reportDownloadUrls\'][\'''' + aaa + '''\']}\''''

def response = context.expand( respString )



---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
subhaD
Contributor

Re: groovy : context expand variable parametrize user defined variable

@avidCoder@nmrao@msiadak

I have a first request that has the value 999999999 in a request tag

In my second request i have below value.

 

"reportDownloadUrls" : {
"999999999" : "/credit-report-download/1854/df0d2929"

 

To be able to parametrize 999999999 value i have below groovy.. and got stuck there, trying to pass variable instead of the number sicne the number  can change

def URL = context.expand( '${2nd call#Response#$[\'data\'][\'resource\'][\'reportDownloadUrls\'][\'999999999\']}' )

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