這種數據驅動的本質是:將測試的case,參數,url,預期結果,存儲於本地excel中。運行腳本時,從文件中獲取預期結果,將實際結果與預期結果比較,將實際結果,比較結果,響應狀態碼回寫excel。java
1:須要的jar包apache
Apache POI 3.17 session
Apache POI-OOXML 3.17 xss
Apache Commons IO 2.6 測試
OOXML模式1.3 url
commons-collections4-4.1 spa
xmlbeans-2.6.0 線程
2:將這些jar包放到jmeter的lib目錄下,記得要在jmeter關閉的狀況下放置!而後重啓jmeter 。3d
添加jar包後,在本地jmeter目錄下建立用例文件,以下所示excel
2:添加一個線程組
在測試計劃中建立一個名爲「pathToFile」的變量」。這個變量的值是咱們上面建立的Excel文件的路徑(注意雙反斜槓)
4. 添加一個JSR223採樣器,目的是根據excel文件建立一個Workbook對象
代碼以下:
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFRow; import java.io.*; XSSFWorkbook book =""; try{ InputStream inputStream = new FileInputStream(new File("${pathToFile}")); book = new XSSFWorkbook(inputStream); inputStream.close(); } catch(Exception e){ log.info("!!!!!!!!"+ e.getMessage()+"!!!!!!!!!!!!!!!"); } if(!book.equals("")){ vars.putObject("book",book); } else { SampleResult.setStopTestNow(true); }
While控制器用於Excel文件包含輸入數據的行數,做爲發送請求的次數
在While Controller中,添加如下代碼,用於中止While Controller
${__javaScript("${stopWhile}"!="OK")}
這一步咱們從excel中獲取字符串,並將這些值存儲在變量中
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFRow; import java.io.*; int i = Integer.parseInt(vars.get("counter")); XSSFRow row = vars.getObject("book").getSheetAt(0).getRow(i); vars.putObject("row",row); for (int j = 1; j <= vars.getObject("book").getSheetAt(0).getRow(0).getLastCellNum(); j++) { if (row.getCell(j) == null) { row.createCell(j).setCellValue(""); } } String parameter = row.getCell(1).toString(); vars.put("parameter",parameter); String request = row.getCell(2).toString(); vars.put("request",request); String Method = row.getCell(3).toString(); vars.put("Method",Method); String expectedResult = row.getCell(5).toString(); vars.put("expectedResult",expectedResult); String Case = row.getCell(0).toString(); vars.put("Case",Case);
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.IOException; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFRow; //String responseToApi = SampleResult.getUrlAsString(); String requestCode = SampleResult.getResponseCode(); String responseFromApi = ("${responseMessage}"); String status = null; if (vars.get("expectedResult").equals(responseFromApi)) { status = "Passed"; } else { status = "Failed"; } //vars.getObject("row").createCell(3).setCellValue(responseToApi); vars.getObject("row").createCell(4).setCellValue(responseFromApi); vars.getObject("row").createCell(7).setCellValue(status); vars.getObject("row").createCell(6).setCellValue(ResponseCode); //vars.getObject("row").createCell(0).setCellValue("${__samplerName()}"); String stopWhile = null; int i = Integer.parseInt(vars.get("counter")); if (i >= vars.getObject("book").getSheetAt(0).getLastRowNum()) { stopWhile = "OK"; vars.put("stopWhile",stopWhile); }
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import java.io.*;
try {
FileOutputStream out = new FileOutputStream(new File("${pathToFile}"));
vars.getObject("book").write(out);
vars.getObject("book").close();
} catch (Exception e) {
log.info("!!!!!!!!" + e.getMessage() + "!!!!!!!!!!!!!!!");
}
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFRow; import java.io.*; try { FileOutputStream out = new FileOutputStream(new File("${pathToFile}")); vars.getObject("book").write(out); vars.getObject("book").close(); } catch (Exception e) { log.info("!!!!!!!!" + e.getMessage() + "!!!!!!!!!!!!!!!"); }
1:在excel中建立新的sheet,做爲分支流程的數據
2:建立一個新的線程組,寫入分支業務流,修改sheet值
3:sampler中調用全局變量的主數據id,與主業務流進行關聯