passing result or variable value from Groovy to test step
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ?
Solved! Go to Solution.
- Labels:
-
Scripting
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
https://www.soapui.org/docs/scripting-and-properties/property-expansion/
https://www.soapui.org/docs/scripting-and-properties/tips-tricks/
Regards,
Rao.
