1. 程式人生 > >SoapUI Groovy讀取Excel + 資料給REST API + Assert Json

SoapUI Groovy讀取Excel + 資料給REST API + Assert Json

目錄:

  1. 前言
  2. SoapUI 設定REST Rquest請求
  3. Groovy 讀取Excel傳給TestCase設定的Properties
  4. Script Assertion解析Json
  5. 總結

一. 前言
學習groovy語言:
精通 Groovy

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(群號)