我对 Java 和 Groovy 还很陌生。但是,我需要从 excel 文件中读取数据并将它们加载到 SOAPUI 测试套件中。我点击了一些来自谷歌的链接并尝试实现相同但面临错误。
我已经下载了 POI 4.1.0 jar 并将它们复制到 SOAPUI/bin/ext 文件夹。在 SOAPUI 中,我为 groovy 脚本添加了一个步骤(如下所述)。
import org.apache.poi.ss.usermodel.*
import org.apache.poi.hssf.usermodel.*
import org.apache.poi.xssf.usermodel.*
impot org.apache.poi.ss.util.*
def fs = new FileInputStream("FILE LOCATION")
Workbook wb = WorkbookFactory.create(fs);
def ws = wb.getSheet("Sheet1")
def r = ws.getPhysicalNumberOfRows()
log.info "==========="+r
它应该返回我的行数。但是我得到了一个错误。
org.apache.poi.ooxml.POIXMLException: Could not initialize calss org.apache.poi.ooxml.POIXMLTypeLoader Error at line 7
在此先感谢您的帮助
SmartBear 论坛和其他地方有很多此类错误,但跨 soapUI 和 POI 版本的一致解决方案并不多。您的代码在 soapUI 之外运行时有效,表明 soapUI 和 POI 之间存在某种 JAR 版本冲突。
所以,有几个选择:
我有一个适用于 .xls 文件的解决方法(尽管 .xlsx 文件有问题)。
首先下载 jxl.jar 以通过 Groovy 脚本访问 excel 文件。资料来源:https://sourceforge.net/projects/jexcelapi/files/jexcelapi.
下载最新版本(当前版本:2.6.12)。解压缩文件,将 JXL.jar 复制到:
Contents/java/app/bin/ext
Contents/java/app/lib
现在添加一个Test Step -> Properties 并将要读取的数据声明为变量(property1,property2)。
使用脚本(添加将响应结果写入另一个excel文件的功能):
import jxl.*
import jxl.write.*
import jxl.write.Number
// Specify the path to the input Excel File
def inpFile = new File("path/inputFile.xls")
// Specify the path to the output Excel File
def outFile = new File("path/outputFile.xls")
// Create a workbook object for the input Excel file
Workbook inpWorkbook = Workbook.getWorkbook(inpFile)
// Get the sheet containing the property values, 1st sheet here
Sheet propSheet = inpWorkbook.getSheet(0)
// Get no of rows
def numRows = propSheet.getRows()
// Create Workbook obj for output Excel File
WritableWorkbook outWorkbook = Workbook.createWorkbook(outFile)
// Create a "Res" sheet for results
Writable resSheet = outWorkbook.createSheet("Res", 0)
// Create labels for the result
def label = new Label(0, 0, "Test Step")
resSheet.addCell(label)
def label = new Label(1, 0, "Status")
resSheet.addCell(label)
// Loop through each row of the sheet containing data to be read
for (i in 1..numRows-1) {
// Read the property values from current row of excel sheet
// 1st column has nodeVal1, 2nd column has nodeVal2
def property1 = propSheet.getCell(0, i).getContents()
def property2 = propSheet.getCell(1, i).getContents()
// Get the test case and test step
def testCase = testRunner.testCase
def testStep = testCase.getTestStepByName("Properties")
def testRequestStep = testCase.getTestStepByName("test_request name")
// Set property values for the test step
testStep.setPropertyValue("property1", property1)
testStep.setPropertyValue("property2", property2)
// Execute the test request
testRequestStep.run(testRunner, context)
// Get the SOAP response of the test request
def resp = testRequestStep.testRequest.response
// Create a cell for the test step
def cell = new Label(0, i, testRequestStep.name)
resSheet.addCell(cell)
// Create columns for retrieving response Node values
cell = new Label(2, 0, "Node Val1")
resSheet.addCell(cell)
cell = new Label(3, 0, "Node Val2")
resSheet.addCell(cell)
if (resp.responseHeaders.toString().contains("HTTP/1.1 200 OK")) {
label = new Label(1, i, "Pass")
resSheet.addCell(label)
def xmlResp = new XmlSlurper().parseText(response.getContentAsString())
// NodeName1 and NodeName2 are the reqd nodes from the successful response
def nodeVal1 = xmlResp.'**'.find { it.name() == 'NodeName1' }?.text()
def nodeVal2 = xmlResp.'**'.find { it.name() == 'NodeName2' }?.text()
cell = new Label(2, i, nodeVal1)
resSheet.addCell(cell)
cell = new Label(3, i, nodeVal2)
resSheet.addCell(cell)
} else if (resp.responseHeaders.toString().contains("HTTP/1.1 500 Internal Server Error")) {
label = new Label(1, i, "Fail")
resSheet.addCell(label)
}
}
// Write and close the Workbooks
outWorkbook.write()
outWorkbook.close()
inpWorkbook.close()
您可以修改脚本以检查各种响应类型。