只有第一个响应被写入 soapui 的 excel 文件中
only first response is being written in excel file in saopui
我是 saopUI 的新手,groovy.I 想从 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()
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'))
我是 saopUI 的新手,groovy.I 想从 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()
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'))