我是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()
答案 0 :(得分:1)
JXL对于如何写入Excel工作簿感到很奇怪。基本上,write()
实际上并不写入文件,而是写入内存中的结构。您可以阅读更多相关信息here。
将write()
移出循环,到close()
之前应该修复它。
我创建了一个名为Frosted Sheets的Groovy库。它装饰了Apache POI,使其易于使用Excel电子表格。 Frosted Sheets 可以简单地将您的代码改为:
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'))