soapui 免费版 实现 datasource+ dataLoop

soapui 的免费版没有data source  和dataLoop 来控制外部的数据输入和循环, 比如需要把request的一个参数循环输入多个数字。

soapui 可以通过groovy写脚本实现, groovy可以使用java的JIR包。

可以在http://mvnrepository.com/上下载相关的包,然后把包放在SoapUI 的 bin\ext  安装路径下。这样就可以直接在groovy 脚本 import了。

下面的例子是循环读取.xls 类型的Excel里的数据然后传递到request  step的参数里。把response保存到test case的property, 供导出response 用。

import java.io.*;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.util.ArrayList;
File inputFile = new File("C:\\Users\\Azhang13\\Work\\1.xls");
try{
HSSFWorkbook  workbook = new HSSFWorkbook(new FileInputStream (inputFile));
HSSFSheet sheet =workbook.getSheetAt(0);
Row row;
Cell cell= null;
int i;

List responses = new ArrayList();
for ( i =0;i<3;i++)
{
row = sheet.getRow(i);
cell = row.getCell(0);
String cell1 = (String)cell;   //  setPropertyValue(String,String) 
testRunner.testCase.setPropertyValue("count",(String)i)
testRunner.testCase.setPropertyValue("investmentId",cell1);  // store the param into the test Case -> Properties 
  testRunner.runTestStepByName ("PerformanceIds_XML"); //step can get the param from the test Case ->Properties ,such as ${#TestCase#investmentId}
  def response = context.expand('${PerformanceIds_XML#response}');
responses.add(response);
}

testRunner.testCase.setPropertyValue("response",(String)responses);

      }catch(FileNotFoundException e){
system.err.println("Exception" +e.getMessage());
       }
catch(IOException e){
system.err.println("Exception"+e.getMessage());
    }


以下是把所有response导出到excel。

mport java.io.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

def List responses = new ArrayList();
def a = testRunner.testCase.getPropertyValue("response");
responses.add(a);
File outPutFile = new File("C:\\Users\\Azhang13\\Work\\2.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Result");
Row row = sheet.createRow((short)0); 
Cell cell = row.createCell((short)0);
cell.setCellValue(responses);
FileOutputStream fout = null;
try{
fout = new FileOutputStream(outPutFile);
}catch(FileNotFoundException e){
e.printStrackTrace();
}  
workbook.write(fout);
     fout.close();

你可能感兴趣的:(SoapUI)