Ask a Question

passing result or variable value from Groovy to test step

SOLVED
ravienu
Occasional Contributor

passing result or variable value from Groovy to test step

Dear Team,

I have query below to achieve one of the requirements. I am using ReadyAPI with proper license.

 

I would like to pass Goovy script result as one request body(XML/JSON) parameter for API. I have written a Groovy script and test step under it. But dont know how to specify groovy result as body request parameter.

 

For example:

def today = new Date()
today.format("dd/MM/yyyy")

//Add some random number to Date field and store it in some variable like cutom_date

 

Now  I would like to pass custom_date as request body(XML) parameter for API step under the groovy script  as below

........
........
<app_date>?</app_date>
.......
.......

cutom_date from groovy script should come and sit at ? place above.

 

Can anyone help on this please ?

11 REPLIES 11
nmrao
Champion Level 2

Groovy Script test step is not needed. It can done in the request step itself as shown in the below thread.

Please refer an existing solution.
https://community.smartbear.com/t5/API-Functional-Security-Testing/Ready-API-3-3-1-messed-date-up/m-...


Regards,
Rao.
richie
Community Hero

Hey @ravienu,

Am i understanding correctly you have a payload with an <app_date> element and you want it to be populated with the current date no matter when you execute your test (so the value is automatically updated to todays date)?

If the answer is "yes", you dont need a separate groovy script step, you can use dynamic scripting to do this.

For example, if you had an element <attribute>?</attribute> and you wanted it to be dynamically populated with a UUID/GUID value, i would replace the ? in my attribute element with the following string

${ =java.util.uuid.randomuuid()}

you can do the same with date handling, but the syntax to do this was changed a while back and im typing this out on my phone so cant remember the syntax.

When im in front of my laptop i will update this post with the correct dynamic scripting command you need.

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
ravienu
Occasional Contributor

Hi @richie ,

Thank you for your reply.

 

Sorry If I am not clear in my question.

I dont want to send just current date to <app_date> element in payload. I want to get current date and then modify the date field to +/- few days with custom code and then send that changed date to payload.

 

In other words, If I have one variable value in my groovy script, like employeeNumber = 2314, How I can pass this to payload element <employee_id>. ?

 

Thank you very much for your help.

 

Regards,

Ravi.

ravienu
Occasional Contributor

Hi @nmrao ,

 

Thank you for your reply.

 

I have got the code snippet to get current date from payload directly by searching in the form and it worked. But my query is different as I have replied to Richi, keeping here as well.

 

Sorry If I am not clear in my question.

I dont want to send just current date to <app_date> element in payload. I want to get current date and then modify the date field to +/- few days with custom code and then send that changed date to payload.

 

In other words, If I have one variable value in my groovy script, like employeeNumber = 2314, How I can pass this to payload element <employee_id>. ?

 

Thank you very much for your help.

 

Regards,

Ravi.

nmrao
Champion Level 2

The link provided in the previous reply has the java 8 date which can included the request itself as per the question.


Regards,
Rao.

Hey @ravienu,

You can also use the dynamic scripting option to set current date +/- number of hours/days/months etc. with the code snippet i was talking about but anyway, youre saying you have an employeeNumber variable set as 1234. You could use the setProperty method to write the value to a holding property (e.g. testcase level Property using something like "testRunner.testCase.setPropertyValue( "EmplyeeNumberProp", employeeNumber )" and then use the GUI GetData function from the employeeNumber element within your payload to source the employeeNumber value saved as a testcase level Property. This would satisfy what you need to do.

I should highlight that there are multiple ways of doing what you need and the genuine coders on the forum (i'm not one of them) like @nmrao or @ZDGN will likely have far more efficient ways of doing this.

Ta

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
ravienu
Occasional Contributor

Hi @nmrao ,

 

Thank you. Yeah I understood that date can be included in the request it self. 

As I have highlighted in my last reply, please forget about date for a while.

 

Groovy step:

If I have one variable value in my groovy script, like employeeNumber = 2314

 

TestStep (REST API with XML payload)

........
........
<employee_id>?</employee_id>
.......
.......

 

Now, employeeNumber from groovy script should be passed to <employee_id> (i.e 2314 should sit at ? place)

It wold be great if you can help with how can I do this ?

Thank you very much.

 

Regards,

Ravi.

nmrao
Champion Level 2

Save the value into custom property, say EMPLOYEE_ID by adding below line to the script

context.testCase.setPropertyValue('EMPLOYEE_ID', employeeNumber.toString())

Now, in the request, use as ${#TestCase#EMPLOYEE_ID}

Of course, one can save the property at test suite, project level as well. For that respective changes have to done in both groovy and request.


Regards,
Rao.
nmrao
Champion Level 2

cancel
Showing results for 
Search instead for 
Did you mean: