Forum Discussion

jly's avatar
jly
Occasional Contributor
3 years ago

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 ...
  • ChrisAdams's avatar
    3 years ago

    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?

     

  • ChrisAdams's avatar
    ChrisAdams
    3 years ago

    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.