您的位置:首页 > 产品设计 > UI/UE

SoapUI Groovy读取Excel + 数据给REST API + Assert Json

2016-11-17 20:11 721 查看
目录:

前言

SoapUI 设置REST Rquest请求

Groovy 读取Excel传给TestCase设置的Properties

Script Assertion解析Json

总结

一. 前言

学习groovy语言:

精通 Groovy

学习SoapUI使用:

SoapUI 测试REST API(接口测试)

手把手教你接口自动化测试 – SoapUI & Groovy

二. SoapUI 设置REST Rquest请求

请求接口:

https://sh-market.ruifusoft.com/app/v1/quote/user/query/stockdetail?marketcode=hk&stockcode=00001&graph_tab_index=0&k_not_refresh=0&stock_type=010104&request_id=1478768235224

TestCase设置Properties:



项目结构:



设置REST Rquest请求:



三. Groovy 读取Excel传给TestCase设置的Properties

Excel文档:百度网盘



Groovy脚本:

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

ArrayList result = new ArrayList();

File file = new File("C:/Users/timen.xu/Desktop/SoapUI/List of CAS securities_C.xls");
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(poifsFileSystem);
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);

for(int i=6;i<=hssfSheet.getLastRowNum();i++)
{
HSSFRow row = hssfSheet.getRow(i);
if(null == row) continue;
HSSFCell cell = row.getCell(1);
if(null==cell) continue;
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC:
result.add(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
result.add(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result.add(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
result.add(" ");
break;
case HSSFCell.CELL_TYPE_BLANK:
result.add(" ");
break;
case HSSFCell.CELL_TYPE_ERROR:
result.add(" ");
break;
default:
result.add(" ");
break;
}
}

def deal(Result_excel){
if (Integer.valueOf(Result_excel) < 10) {
Result_excel = "0000" + Result_excel;
}else if (Integer.valueOf(Result_excel) < 100) {
Result_excel = "000" + Result_excel;
}else if (Integer.valueOf(Result_excel) < 1000) {
Result_excel = "00" + Result_excel;
}else if (Integer.valueOf(Result_excel) < 10000) {
Result_excel = "0" + Result_excel;
}
return Result_excel;
}

for (int i = 0; i < result.size(); i++) {
testRunner.testCase.setPropertyValue("stock_name", deal(result.get(i).toString().substring(0, result.get(i).toString().length()-2)));
testRunner.runTestStepByName("CASRequest");
}


四. Script Assertion解析Json

Script Assertion:

import net.sf.json.JSONSerializer

Object  responseObj = JSONSerializer.toJSON(messageExchange.responseContent)

def data = responseObj.get("data").get("208")

assert data == 1 : "错误信息如下 : "


如下图:



五. 总结

SoapUI执行http请求达到200以上就导致客户端很卡,查下CPU使用情况,居高不下80%,只能命令行执行命令,不会导致客户端卡顿,SoapUI命令行方式运行

相对比Python而言,SoapUI实在不行,建议大家还是自己用Python写,百度网盘中有Python版本,可以参考一下 百度网盘

欢迎加QQ群 -> 阳台测试 -> 239547991(群号)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息