Forum Discussion

Ludo15101966's avatar
Ludo15101966
Contributor
6 years ago

Groovy and File datasource

Hello,

I'm using Ready Api 2.6 to test REST web services

I'm trying to access to a File Datasource (CSV) with groovy script

I have created a File datasource in my testcase, the list of items seems ok, good columns name and data

Im creating a groovy script to generate a POST json to include in a REST web service step

 

I'm looking for sample groovy script to access to the datasource infos

- number of data lines

- loop through rows

- get columns values for a specific row

 

I've found some expand example but i didnt succeed to user them correctly

Do you have some example that i can use and adapt

 

Thank you

Best regards

 

Ludovic

 

 

 

 

 

5 Replies

  • richie's avatar
    richie
    Community Hero
    Hey Ludovic,

    Whats the reason you need groovy? Where you say [quote: Im creating a groovy script to generate a POST json to include in a REST web service step] i dont understand why youre not using the OTB functionality to do this.

    Later on in your post you seem to state you want the groovy to process and reports certain datasource stats.

    Also you mentioned ypuve found some groovy you thought might work if updated...f you have some groovy, even if it doesnt work its alwaysbest to add it to your post so the groovy scripters on the forum (not me!) will have a better understanding of where youre at in regards to progress.

    Example csv and request payload can only help too (although i always forget this bit too!)

    Cheers,

    rich
    • Ludo15101966's avatar
      Ludo15101966
      Contributor

      Hello rich

      Thanks for your answer

       

      Sorry I don't know what OTB means... you have a link or tutorial ?

       

      I have to generate a small json object to use as POST in my REST web service

      I'm use to do it in groovy and store a variable in context

       

      for the first time I'm using datasource to read a set of external parameters,for example

      TITLE;MINDATE;MAXDATE

      TEST;2001/01/01;2010/01/01

      TEST1;2010/01/01;2019/01/01

       

      I have created a datasource FILE step named "DataSource" and the csv seems correctly loaded

      I'm looping in datasource with datasource loop, seems ok

       

      In my groovy script I need to get the 3 fields for each line, manipulate the data and build a json structure

       

      def jsonAsText = '''{
      "field1" : "string",
      "field2" : "string",
      "field3" : "string",
      }'''
      def object = slurper.parseText(jsonAsText)

       

      I found some groovy examples to get datasource fields like

       

      def row = testRunner.testCase.testSteps["DataSource"].currentRow;   

      def thetitle = context.expand( '${DataSource#TITLE}' )

       

      I looking for a full example of FILE datasource usage in groovy

       

      Thanks for your help

       

      Ludovic