How to use groovy script to dynamically store data in data source grid?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to use groovy script to dynamically store data in data source grid?
Hi,
I want to use a groovy script to store data+time stamp value into data source grid.
I think the steps are:
1) create data source grid step
stores the data
2) create groovy step
this script to get and then store the date value into data source grid
3) create api step
this is actual call to api
4) data source loop
this loops through each record in the data source as it calls the api
Is that correct?
In my groovy step, to get date I have:
def today = new Date()
def formattedDate = today.format("ddMMyyhhmm")
log.info(formattedDate)
But I'm not sure what the syntax is to store the value in data source. Can someone help me?
Jennifer
Solved! Go to Solution.
- Labels:
-
Data-Driven Testing
-
Scripting
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I think I understand what you're trying to do.
Here's a mock-up of the test flow, as I see your requirements....
The Groovy script needs a return statement....
def today = new Date()
def formattedDate = today.format("ddMMyyhhmm")
log.info(formattedDate);
return formattedDate;
I don't know what else you have in your grid, so here's an example that includes calling Get Date for each row...
Important Note : There is a checkbox labelled 'Recognize grid content as plain test. Uncheck this so ReadyAPI actually calls the Groovy script.
In your API step, you can then pull values from the current Datasource Grid row using this syntax.... ${Data Source Grid#Prop3}
Does that get you moving?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hey @ChrisAdams
I thought you said you didn't know how to use a DataGrid? Looks like you're doing ok considering you don't! 😉
Cheers,
Rich
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@richie, lol. didn't take much to work out. Used datasources a lot, but never noticed the grid type until it came up last week.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks @ChrisAdams.
Based on what you said - it looks like I'm on the right track.
This is what I have atm.
This is what my data source grid looks like:
My original question was: "how to do STORE values in the groovy step into data source grid?" Is this possible?
If so - then I will add another property in the data source grid called currentDate.
Then I hope to loop the groovy script to get and store date and the api step will 'get data' from the respective property from the datasource grid.
Alternatively if I cannot store in data source grid - then I will store in test case properties.
It's Australia Day today. 🇦🇺
#StaySafe.
Jennifer
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @jly ,
I might be misunderstanding your requirement, but I thought I'd achieved what you originally asked for. My example above uses a Datasource Grid with some static values as well as pulling in values from a Groovy script step.
In the Datasource Grid, the static values will always be there and the values from the Groovy script will be refreshed every run.
In your last reply, you mentioned....
My original question was: "how to do STORE values in the groovy step into data source grid?" Is this possible?
If so - then I will add another property in the data source grid called currentDate.
If you look back at my Datasource Grid screenshot, The column for Prop3 shows how the result of the Get Date Groovy script is called.
E.g. with updated screenshot.... Note column three contains... ${Get Date - Groovy Script#result}. When ReadyAPI iterates through the Datasource Grid, it calls Get Date for each row.
Again in the Datasource Grid, you can click the preview to show this....
In your Test flow, you have the Groovy script after the Datasource Grid step. For my approach, the Groovy must come before any step that references it.
To reference the date value from the Datasource Grid in your API step, you can use the $ syntax. E.g.
Alternatively, you can still just pull Current Date directly into you API step without getting it from the Datasource Grid by using ${Get Date - Groovy Script#result}
If I have misunderstood, then let me know and I'll delete this reply.
Hope you had a good Australia day!
Chris
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi @ChrisAdams ,
Thanks for your help Chris. I've re-arrange the steps and know the syntax on how to get data.
Also is it possible to call the groovy script each time the api is called?
At the moment - the groovy script is just called once (at the start) and then it loops the api step only.
What I"m trying to do is get a different time stamp - each time the api is called.
Let me know if I should raise another ticket question for this.
Jennifer
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I can achieve your end aim, but not by storing the timestamp value in the datasource grid. E.g. new test flow with Timestamp Groovy step inside the loop...
I've update the Datasource Grid to remove the timestamp column. E.g.
I've update the Groovy to return timestamp (milliseconds since January 1, 1970, 00:00:00 GMT). See https://www.tutorialspoint.com/groovy/groovy_gettime.htm
def today = new Date()
def timestamp = today.getTime();
log.info("In loop - Timestamp value is " + timestamp);
return timestamp;
In the API step, you can pull in values from both the Datasourcre grid and the Groovy Step using the $ syntax shown elsewhere.
Here's my Script log after running...
But, I cannot think of a way to store each call to Timestamp groovy in the Datasource grid. Sorry.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That again @ChrisAdams.
Going by you screenshot - it is possible to have more than just api step inside loop.
I’ll need to play around in ReadyAPI to find out how.
When I do - I’ll post it on this thread - hopefully I can other newbies.
Many thanks @ChrisAdams.
Jennifer
#StaySafe
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
You can have as many steps inside the loop as you wish. I have some tests with over 30 steps inside the loop.
I've also built tests with more than one loop and I've also built tests with nested loops.
It can cope with pretty much whatever you can think of.
All you need to do is make sure your Datasource loop steps points at the right datasource and the first step within the loop you want to call.
