Forum Discussion

jly's avatar
jly
Occasional Contributor
3 years ago
Solved

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

  • 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?

     

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

     

     

     

     

10 Replies

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

     

     

    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?

     

    • jly's avatar
      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.

       

      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

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

         

        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

         

    • richie's avatar
      richie
      Community Hero

      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

      • ChrisAdams's avatar
        ChrisAdams
        Champion Level 2

        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.