Ask a Question

groovy data source loop


groovy data source loop

Hi there,


typically when using the data source I can set the amount of rows that shall be generated and with that how many times the loop should occur. Now I try to generate a data source using a groovy script with 3 rows but I can't figure out how to set this 3 rows as there is no field of "number of rows" as for i.e. the data Generator. 

Could you please tell me how I can set and read this parameter when using a groovy Data Source or is the groovy Data Source not ment to be used with loops?





Champion Level 0

The way I handle this is to create a primary data source that is a grid, and I fill it with how many rows of  data that I need to process. It would look something like this:


Count Data Source (set with 1 property, X rows, where the data does not matter.)

           Groovy Script Data Source

           Tests using GS data Source

Data Source Loop using Count Data Source as its control, and pointed at GS Data Source for its test step.

This way the Count Data Source will control how many iterations you have.




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

Hi @groovyguy,


thanks for the tip. I solved various issues with a similar approach but was hoping for some groovy command being able to determine the amount of rows generated. Well, this approach will certainly work as well.

Could you just tell me how you generate several rows in the grid dataSource (which I assume is the dataSource you named GroovyDataSource above?).

I know I can write a single value to a DataSourceProperty with 

result["DataSourceProperty"] = someValue


but how do I generate several lines? Can I simply use the index operator like 


result["DataSourceProperty"][0] = someValue[0]
result["DataSourceProperty"][1] = someValue[1]
result["DataSourceProperty"][2] = someValue[2]


Thanks a lot for the quick help!





@LennSar Not a problem! Always happy to help.

If I understand your next question, you want to generate 3 rows at once from the Groovy Data Source and access those all at the same time? I am not sure you can do that all at once. I haven't figured out a way, at least.

You could create a loop within your groovy datasource and triplicate your properties, but even that seems convoluted and messy in the long term.


If I might ask, what is your use case with needing three rows from the data source at once?


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

No it would be fine to generate the data one after another. The use case is that I need randomly generate dates but need to make sure that the second date is later than the last.


so if the first date would be 21.2.2022 the second must be at least 22.02.2022 and not 20.02.2022. 

My initial idea was to generate these dates from a script, write the results to the data source and than loop over the data source. 

I think based on what I know now I would insert the generation script in the loop and use the setPropertyValue Method to write in the relevant commands (instead of deriving the data from the data source) to make sure the second date is later than the first I would just store current year, month and date in a property so that I can access that on every iteration and count it upwards.

Hey @LennSar,

Sorry i'm a bit late to posting on this. I was just thinking. Rather than bother struggling with groovy to generate a date, write it to a property, add days to the value, pass value to the payload and then repeat these steps for each request, you could just do your data manipulation in a xls./xlsx (probably the only time i advocate using an xls/xlsx), saving yourself some of the pain?

I dunno....just a suggestion!


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

Hi @richie,


the reason for all the trouble is to generate the data dynamically and randomly. I cannot do that with an xlsx. 

Okay reading over your replies, I think I have a solution. If you build the Count Data Source with how many ever rows you need, just set those values to the number of the row. The count data should be 1, 2, 3, 4, etc. for how many ever loops you need. I can whip up a groovy script to help with this too.

From there, in your groovy script data source, set a date function that takes a NOW timestamp and adds the value from the count data source.

so on the first loop it would be

but on the second loop, it would be
(using today's date)

I can provide groovy samples to handle that too.


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

Hi @groovyguy,


thanks for the offer! I might come back to it. 
It seams my way of explanation is really confusing to everybody else than me :P. My main goal is that the date is random and successively increasing. If I understand correctly your suggestion would be a way to count upwards from a given date. 


For that I would have to create a list of dates at the beginning of the test step and than iterate over that list or I would have to generate the date on every cycle for which I would have to store the last date i.e. in a property, so I can access it on the next cycle and increase it by a random amount.

I understand what you are trying to do with a random date, but to be able to randomize a date repeatedly and to ensure that it's always in some sort of chronological order is very hard and tricky to do. Is there a reason it HAS to be a random day?

If so, you are going to have to randomize the first day and then increment from there. I cannot think of any way around that.


Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Showing results for 
Search instead for 
Did you mean: