只有第一个响应正在写在saopui的excel文件中

我是saopUI和groovy的新手。我想从excel文件中读取数据并运行请求并将数据写回excel文件。 我的阅读和执行部分工作正常。 但是当我写回应excel只有第一个响应正在写入。 但是,当我看到日志所有三个请求都成功运行。 请帮我解决这个问题。

这是我的groovy脚本:

import jxl.* import jxl.write.* def dataFileLocation="D:/SOAP/input.xls" //Datasheet read start def workbook = Workbook.getWorkbook(new File(dataFileLocation)) def sheet = workbook.getSheet(0) int count=workbook.getNumberOfSheets() def rowCount = sheet.getRows() def colCount = sheet.getColumns() def myTestCase = context.testCase propTestStep = myTestCase.getTestStepByName("Properties"); def arr=[] //Datasheet read end //Content Write start WritableWorkbook workbook1 = Workbook.createWorkbook(new File("D:/SOAP/output1.xls")) WritableSheet sheet1 = workbook1.createSheet("Worksheet 1", 0) //Content Write end for(int i = 0;i < rowCount; i++){ for(int j = 1;j < colCount+1; j++){ arr[i]= sheet.getCell(j-1,i).getContents() def val=arr[i] propTestStep.setPropertyValue("zip",val) def project = testRunner.testCase.testSuite.project def aa=testRunner.runTestStep( project.testSuites['USZipSoap TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByZIP'] ) def response = testRunner.testCase.testSteps["GetInfoByZIP"].testRequest.response.contentAsString Label label = new Label(j,i,response); log.info label sheet1.addCell(label); workbook1.write() log.info j+" " +i+" "+" "+response } } workbook1.close() 

对于如何写入Excel工作簿,JXL很奇怪。 基本上, write()并不实际写入文件,而是写入内存中的结构。 你可以在这里阅读更多。

所以把write()移出循环,直到close()应该修正它。

替代

我创build了一个名为Frosted Sheets的Groovy库。 它装饰Apache POI,使其与Excel电子表格一起工作变得非常容易。 磨砂表格可以简单的代码如下所示:

 import org.apache.poi.hssf.usermodel.HSSFWorkbook import com.emmanuelrosa.frostedsheets.* def dataFileLocation='D:/SOAP/input.xls' def workbook = new FrostedWorkbook(new HSSFWorkbook(new FileInputStream(dataFileLocation))) def sheet = workbook[0] /* Index-based access to sheets */ def myTestCase = context.testCase propTestStep = myTestCase.getTestStepByName("Properties") def workbook1 = new FrostedWorkbook(new HSSFWorkbook()) def sheet1 = workbook1['Worksheet 1'] /* Map-like access to sheets */ sheet.eachWithIndex { row, rindex -> /* Iterate through each row in a sheet. */ def output = [] row.eachWithIndex { cell, cindex -> /* Iterate through the columns of a row. */ propTestStep.setPropertyValue("zip", cell.cellValue) def project = testRunner.testCase.testSuite.project def aa=testRunner.runTestStep( project.testSuites['USZipSoap TestSuite'].testCases['GetInfoByZIP TestCase'].testSteps['GetInfoByZIP'] ) def response = testRunner.testCase.testSteps["GetInfoByZIP"].testRequest.response.contentAsString output << response log.info "$cindex $rindex $response" } sheet1 << output /* Appends a new row to the sheet. */ } workbook1.write(new FileOutputStream('D:/SOAP/output1.xls'))