JMeter之BeanShell使用實例: 斷言返回的數據類型


原文:http://www.jianshu.com/p/2341b901cd4a 

 先把用到的jar包org.json放在jmeter/lib文件下,並在測試計划中導入 

Add directo ry or ja r to Spath 
& O. 3. jar 
O.•apane-imeter-30QiblorgJson.iaI

思路 

function(standardData,respData){ 

  standardJson =getJson(); 

  respJson = getJson(respData); 

   循環N次{ 

         if(standardJson.getType == respJson.getType); //逐個判斷json的字段類型是否符合標准 

} 

Beanshell代碼 

import org.json.JSONArray; 

import org.json.JSONObject; 

import java.util.Iterator; 

import java.util.Map; 

  

public static String equalsJson(JSONObject standardJson, JSONObject responseJson) {//輸入兩個json,判斷第一個里面的所有字段在第二個中的類型是否相同 

    String err_message = "";  

    Iterator it = standardJson.keys(); // 儲存所有要驗證的Key 

    //log.error("it=:"+it.toString()); 

     while (it.hasNext()) { 

          String key = (String) it.next(); 

          //log.error("key=:"+key); 

          String thisKetType = standardJson.get(key).getClass().getName(); //獲取當前Key的標准type 

          log.error("standard Key = " + key + ", Value = " + standardJson.get(key) + ", Type = " + thisKetType); 

  

          if(responseJson.isNull(key)){ //判斷response中有無當前Key 

             log1 = "------ExistError: " + key + " Not found."; 

             log.info("!!Failed: " + log1); 

             err_message = (err_message + "\n" + log1); 

          } 

          else{  //response中找到Key了,再判斷type 

               String respKetType = responseJson.get(key).getClass().getName(); //獲取響應的字段類型 

  

               if(respKetType.equals(thisKetType)){ 

                    log.info("Passed."); 

                    if(thisKetType.equals("org.json.JSONObject")){ //object類型的字段繼續往內層判斷 

                        err_message += equalsJson(standardJson.getJSONObject(key), responseJson.getJSONObject(key)); //!!進入遞歸時,保存當前錯誤信息 

                    } 

                } else { 

                    String log1 = "------TypeError : " + key + " is " + respKetType + " (should be " + thisKetType + ")"; 

                    log.info("!!Failed: " + log1); 

                    err_message = (err_message + "\n" + log1); 

                } 

            } 

        } 

     return err_message; 

} 

public static Boolean respTypeAssertion(String standardData) { //輸入標准響應,轉為json並調用比較函數,得到斷言結果 

    String resData = prev.getResponseDataAsString(); //獲取前一個請求的響應 

    log.info("接口返回result: " + resData); 

    JSONObject standardJson = new JSONObject(standardData);  

    JSONObject jo = new JSONObject(resData); 

    // log.info("jo=:"+jo.toString()); 

    JSONObject responseJson = jo.getJSONObject("result"); 

    //log.info("responseJson=:"+responseJson.toString()); 

    log.info("------------------------Beanshell assertion-------------"); 

    String message = equalsJson(standardJson, responseJson); 

    log.info("------------------------ResultMessage--------------------" + message); 

    if(message == ""){    //如果錯誤信息是空,說明斷言結果通過 

        FailureMessage = "Pass!"; 

        return true; 

    } 

    else{   //有錯誤信息打印到斷言結果里面 

        Failure=true; 

        FailureMessage = "Type Error!" + message; 

    } 

    return false; 

}    

  

String standardData = "{'status':'true','diaryId':'10216','firstDiary':'0'}";//上傳新建日記hcUploadNewDiary接口的返回數據 

//String standardData = "{'uid':'123','phone':'13580478329','has_password':true,'location':{'province':true,'city':'\u6c55\u5934\u5e02'},'cpma':null}"; 

respTypeAssertion(standardData); 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM