Ask a Question

How to use groovy script to dynamically store data in data source grid?

SOLVED
jly
Occasional Contributor

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

10 REPLIES 10
ChrisAdams
Champion Level 2

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....

 

ChrisAdams_0-1643014495781.png

 

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...

ChrisAdams_1-1643014719921.png

 

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?

 

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

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

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

jly
Occasional Contributor

Thanks @ChrisAdams.
Based on what you said - it looks like I'm on the right track. 

This is what I have atm.

Screen Shot 2022-01-26 at 8.56.05 pm.png

This is what my data source grid looks like:

Screen Shot 2022-01-26 at 9.02.18 pm.png

 

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

ChrisAdams
Champion Level 2

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.

ChrisAdams_0-1643214567271.png

 

Again in the Datasource Grid, you can click the preview to show this....

ChrisAdams_1-1643214725693.png

 

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.

ChrisAdams_2-1643215784988.png

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

 

jly
Occasional Contributor

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

ChrisAdams
Champion Level 2

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...

ChrisAdams_0-1643540134576.png

 

I've update the Datasource Grid to remove the timestamp column.  E.g.

ChrisAdams_1-1643540339956.png

 

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...

ChrisAdams_2-1643540589814.png

But, I cannot think of a way to store each call to Timestamp groovy in the Datasource grid.  Sorry.

 

 

 

 

jly
Occasional Contributor

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

 

 

ChrisAdams
Champion Level 2

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. 

 

cancel
Showing results for 
Search instead for 
Did you mean: