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
}