-1
我正在使用SOAPUI来测试webservices。所有的testdata都保存在excelfiles中。如何将数据从excel中的单元格传输到请求中的数组项目?很高兴知道如何在请求中使用Excel表格中的数据填充数组中可变数量的项目。SOAPUI将数据从excel文件传输到请求数组
我正在使用SOAPUI来测试webservices。所有的testdata都保存在excelfiles中。如何将数据从excel中的单元格传输到请求中的数组项目?很高兴知道如何在请求中使用Excel表格中的数据填充数组中可变数量的项目。SOAPUI将数据从excel文件传输到请求数组
如果您有SoapUI Pro,则可以使用数据源步骤。 如果没有,你可以使用这个类的Groovy脚本就像一个图书馆
您需要下载的jar JXL读取Excel文件: http://www.java2s.com/Code/Jar/j/Downloadjxl2612jar.htm
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class readExcelToArray
{
def log
def context
def testRunner
def String inputFile;
def String[][] data = null;
def readExcelToArray(logIn,contextIn,testRunnerIn){
this.log = logIn
this.context = contextIn
this.testRunner = testRunnerIn
}
public String[][] getData()
{
return this.data;
}
public void setInputFile(String inputFile)
{
this.inputFile = inputFile;
}
//function to get une check if a value if present in the column A (column 0)
public int getIndex(String itemName)
{
def items = itemName.toLowerCase().tokenize("-");
for (int i = 0; i < this.data[0].size(); i++)
{
for (int j = 0; j < items.size(); j++){
def item = items[j].trim();
if (item.equals(this.data[0][i].toLowerCase())){
return i;
}
}
}
return -1;
}
public String[][] read() throws IOException
{
File inputWorkbook = new File(inputFile);
Workbook w;
try
{
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
data = new String[sheet.getColumns()][sheet.getRows()];
// Loop over first 10 column and lines
// log.info(sheet.getColumns() + " " +sheet.getRows());
for (int j = 0; j <sheet.getColumns(); j++)
{
for (int i = 0; i < sheet.getRows(); i++)
{
Cell cell = sheet.getCell(j, i);
data[j][i] = cell.getContents();
// System.out.println(cell.getContents());
}
}
/*
for (int j = 0; j < data.length; j++)
{
for (int i = 0; i <data[j].length; i++)
{
log.info(data[j][i]);
}
}
*/
}
catch (BiffException e)
{
e.printStackTrace();
}
return data;
}
}
context.setProperty("readExcelToArray", new readExcelToArray(log, context, testRunner))
而且你可以使用你的类像这样你脚本:
//initialize you var project here before
def project = testRunner.getTestCase().getTestSuite().getProject()
def library = project.getTestSuiteByName("Scripts");
def module = library.testCases["excelReader"].testSteps["readExcelToArray"]
module.run(testRunner, context)
this.readExcelToArray = context.readExcelToArray
this.readExcelToArray.setInputFile("pathToExcelFile.xls");
def r = this.readExcelToArray.read();
//a functionnal example
if(this.readExcelToArray.getIndex(this.context.getTestCase().name) == -1 || r[0][1] != "valueToTest"){
log.error("Error")
}
请在使用它之前检查getIndex函数,我修改了我的情况,但它就像一个例子。