excel文件的groovy脚本在SoapUI中进行数据驱动测试

时间:2023-03-09 00:12:22
excel文件的groovy脚本在SoapUI中进行数据驱动测试

SoapUI Pro具有从外部文件读取数据的功能,例如:excel,csv等。但SoapUI不提供从excel文件读取数据的功能。因此,为了从SoapUI中的excel文件中读取数据,我们需要在groovy脚本中编写一些代码。
我这篇文章我将告诉你,如何从excel文件中读取数据。我正在使用poi jar文件从groovy中的excel文件中读取数据,下载以下jar文件并放入SoapUI lib文件夹。

  • POI-3.8-beta5-20111217.jar
  • POI-例子-3.8-beta5-20111217.jar
  • POI-excelant-3.8-beta5-20111217.jar
  • POI-OOXML-3.8-beta5-20111217.jar
  • POI-OOXML-架构 - 3.8 beta5-20111217.jar
  • POI暂存器-3.8-beta5-20111217.jar
  • dom4j的-1.6.1.jar

在这篇文章中,我为“ConversionRate”API创建了一个SoapUI项目,并创建了一个名为“ConversionRate”的测试用例和测试步骤。所以这里我需要为数据集运行此测试,其中数据在外部excel文件中。

excel文件的groovy脚本在SoapUI中进行数据驱动测试

我创建了一个“ReadXLSFile”groovy步骤,并在下面编写代码,从“Book1.xlsx”文件中读取“ConversionRate”API方法的数据。以下是excel文件数据:

I have created a “ReadXLSFile” groovy step and write below code to read data from “Book1.xlsx” file for the “ConversionRate” API method. Below is excel file data:

To
From
USD
ALL
AFA
DZD
AWG
BSD
BSD
BDT
GroovyScript Code:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.usermodel.*;
import java.io.*; class ExcelReader { def readData() {
def path = "E:\\Automation-WorkArea\\APITest\\Book1.xlsx";
InputStream inputStream = new FileInputStream(path);
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0); Iterator rowIterator = sheet.rowIterator();
rowIterator.next()
Row row;
def rowsData = []
while(rowIterator.hasNext()) {
row = rowIterator.next()
def rowIndex = row.getRowNum()
def colIndex;
def rowData = []
for (Cell cell : row) {
colIndex = cell.getColumnIndex()
rowData[colIndex] = cell.getRichStringCellValue().getString();
}
rowsData << rowData
}
rowsData
}
} def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def myTestCase = context.testCase ExcelReader excelReader = new ExcelReader();
List rows = excelReader.readData();
def d = []
Iterator i = rows.iterator();
while( i.hasNext()){
d = i.next();
myTestCase.setPropertyValue("From", d[0])
myTestCase.setPropertyValue("To", d[1])
testRunner.runTestStepByName( "ConversionRate")
}

描述:

  • 包含函数“readData”的ExcelReader类,用于从“Book1.xlsx”文件中读取数据。
  • myTestCase.setPropertyValue(“From”,d [0])和myTestCase.setPropertyValue(“To”,d [1])用于设置测试用例“from”和“To”属性值。
  • testRunner.runTestStepByName(“ConversionRate”)此步骤用于运行测试步骤“ConversionRate”
所以这样当我运行测试“ReadXLSFile”从xls文件中读取数据时,将为每个输出数据执行测试“ConversionRate”