Forum Discussion

Azeddin_Margani's avatar
Azeddin_Margani
Contributor
8 years ago
Solved

Iterate/loop through soapui response data/items then write to excel using Groovy.

Hi, 

 

Can you please assist me - I have a soapui response and i'm trying to read the nodes <e> then write them into excel output. Currently, I'm only getting the first item written into the excel output sheet. I need to able to read and write all of the <e> items into excel in one column but separate rows. 

 

Thanks in Advanced. 

 

 

Soapui Response data:

 

<Response>
   <e>1760cd26-1e00641a-b632e328-7fdc13be-9b6f8934</e>
   <e>28821b78-e7cda408-356c1e37-91ee8a7b-a34464df</e>
   <e>6f123c53-93dc4fd8-25e0cd93-1a6deabd-7e94ba2f</e>
   <e>69d116f5-b1ac13a0-ce2da202-7ae2cc2f-5d9575db</e>
   <e>e6dc3e9b-e8c82ae0-ee620328-c5a7e9a7-6f6aa6fd</e>
   <e>5a2e4216-65973e77-62e6ac02-24c6c714-4542be49</e>
   <e>c70de875-60a29986-b399e446-4fc29400-77e9f016</e>
</Response>

 

 

My intended Groovy code is as below: 

 

import jxl.*;
....
...
//Declare variables
log.info("Service testing satrted")
def reqOperationName="MyGetRequest";
def inputDataFileName="C:/Users/Desktop/responseData.xls"
def inputDataSheetName="Sheet1";

Workbook workbook = Workbook.getWorkbook(new File(inputDataFileName));
     WritableWorkbook copy = Workbook.createWorkbook(new File(inputDataFileName),workbook);
        WritableSheet sheet1 = copy.getSheet(inputDataSheetName);
try
{
rowcount=sheet1.getRows();
       colcount=sheet1.getColumns();
 for(Row in 1..rowcount-1){
    for (Col in 1..colcount-1){
    }
//Read response Xml
def groovyUtils=new com.eviware.soapui.support.GroovyUtils(context)
def resholder = groovyUtils.getXmlHolder(reqOperationName+"#ResponseAsXml")

// Iterate through item nodes in the response message
for( item in resholder.getNodeValues("//*:e"))
  {
    //log.info "Item : [$item]" 

     resTagValue1=resholder.getNodeValue("//*:e")

    //Write response value in xls
     Label resValue1=new Label (0,Row,resTagValue1);
           sheet1.addCell(resValue1);
   }
    }//Row loop end here
}
catch(Exception e){
 log.info(e)
}
finally{
copy.write();
copy.close();
workbook.close();
}
log.info("  service testing is finshed")