读取 excel 文件并加载到 SOAP UI 请求中

问题描述 投票:0回答:2

我对 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

在此先感谢您的帮助

java groovy soapui
2个回答
1
投票

SmartBear 论坛和其他地方有很多此类错误,但跨 soapUI 和 POI 版本的一致解决方案并不多。您的代码在 soapUI 之外运行时有效,表明 soapUI 和 POI 之间存在某种 JAR 版本冲突。

所以,有几个选择:

  • 如果您负担得起,SoapUI Pro 的数据循环测试步骤中内置了 Excel 功能。
  • 尝试降级到早期版本的 POI 和 soapUI。代码与您的代码基本相同使用 soapUI 5.3 和 POI 3.14
  • 使用更简单的东西。由于您使用的是 Excel,因此您应该能够以 CSV 格式获取数据,并使用类似 public Object splitEachLine(String regex, Closure closure) 的方法仅使用 Groovy 处理 CSV 数据。
  • 创建您自己的 Groovy 或 Java 项目,其中包含 POI 依赖项以及一个简单的接口来调用您需要的功能。参见例如,SoapUI 中的库冲突

0
投票

我有一个适用于 .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()

您可以修改脚本以检查各种响应类型。

© www.soinside.com 2019 - 2024. All rights reserved.