Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
4 years ago

Groovy Script to read a CSV

My Groovy script has a loop to read an CSV file and to execute the testcases one by one.  It is working but I like to improve its efficiency.

If the CSV has blank lines, the testcase would still execute.  Ideally, I like to output a proper log.info and report file when this happens (a message like Input line is blank).and do not execute the testcase.    What is the best way to do this?

 

Below is part of the Groovy:

for(int i =0; i < rowsize; i++)

rowdata = rowsData[i]
String[] data = rowdata.split(",")

 testRunner.testCase.setPropertyValue( "TARGET-1", data[0] ) 
 testRunner.runTestStepByName( "PT-SDM-GetImsi")
 tStep = testRunner.testCase.getTestStepByName("PT-SDM-GetImsi")

...

//  Output results to proper log.info

//  Output results to proper report file (log file)

...

}

log.info " End"
log_date = new Date()

sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
report << "\n"+sdf.format(log_date)+" End"
return

  • Ok.
    Instead of data[0].charAt[0], try using
    if (rowdata.startsWith('3')) {
    // do all the processing here
    }
  • Thanks, nmrao.

    I used your code but modified it a bit.   I also added a continue statement so to ignore the rest of the code like testcase, etc.

     

    if ( !rowdata.startsWith('302') )
    {
    log.info " Seq="+i+ " " + data[0]+" Invalid line"
    report << "\n"+ sdf.format(log_date)+" Seq="+i+ " " + data[0] +" Invalid line"
    continue
    }

    • Mis3's avatar
      Mis3
      Frequent Contributor

      Problem is I am usually not the one who prepare the input file and to execute the Groovy script.

      The script in its current form is working well.  I just wanted to improve it so to handle potential human errors.  

       

      Because the 1st character of each "good" input line should be "3", I was thinking to compare this char so if it is not "3", the loop should skip this line.  

      char_1 = data[0].charAt(0)

      log.info char_1

      Unfortunately, this does not work.  It gave me a outofbound error.  

       

      1. Is there a way to detect blank line?

      2. When a blank line is detected, how do I skip the rest of the commands in the loop?  I like to output a message like "invalid data."; skip all the commands in the loop and proceed to the next line (until no more data)?

       

      Thanks.

       

      • Mis3's avatar
        Mis3
        Frequent Contributor

        I am getting somewhere.  I added a if-else statement inside the loop to check for blank line and invalid input.

        It works but I would like to exit the loop and read the next line until no more line in the input.  

        How do I ask the Groovy to skip the rest of the loop (no need to execute the testcase)?

        ...

        ...
        for(int i =0; i < rowsize; i++)
        {
        def log_date = new Date()
        def sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
        rowdata = rowsData[i]
        String[] data = rowdata.split(",")

        if (data[0] == "" )
        { log.info "\n" + "PT Seq="+i + " Empty line."
        }
        else
        {
        char_1 = data[0].charAt(0)
        if (char_1 != "3" )
        { log.info "\n" + "PT Seq="+i + " Invalid line."
        }
        }

        ...

        ...