zoukankan      html  css  js  c++  java
  • JMeter ---相关脚本笔记

    整理一下工作中使用的BeanShell断言脚本,防止遗忘。代码能力有限,所以只能引用大佬的源码了,不过最重要的还是要学会用才行。

    来一点代码注释,对于小白来讲,还是必须的。

    1 int M = ${passengerId_#}; //获取passengerId这个变量的count值
    2 
    3 job.getString("passengerId") //从response里面取数据
    4 
    5 vars.get("mobile_1") //从数据库里取数据
    6 
    7 ${gender} //引用变量

    由于断言的情况分多钟,所以这里也列举了一些情况。主要是API测试时,请求参数、返回参数、数据库查询的数据进行对比的操作。

    • Response返回的list跟数据库里面的数据进行对比

    代码如下:

     1 import org.json.*;
     2 
     3 json = prev.getResponseDataAsString();  
     4 System.out.println("响应内容: 
    " + json);
     5 JSONObject response = new JSONObject(json);
     6 JSONArray data = response.getJSONArray("data");
     7 int M = ${passengerId_#};
     8 if (data.length()==M) {
     9      for (int i = 0; i < data.length(); i++) {
    10          JSONObject job = data.getJSONObject(i);
    11                         String passengerId=job.getString("passengerId");
    12                         flag=false;
    13                         for(int j=1;j<=M;j++) {
    14                             if(vars.get("passengerId_" + j).toString().equals(passengerId)) {
    15                                 String Msg = "PassengerId : "+passengerId+"
    ";
    16                                 mobile = vars.get("mobile_" + j).toString();
    17                                 if(!job.getString("mobile").equals(mobile)) {
    18                                     Failure = true;  
    19                                     FailureMessage = Msg + " mobile 数据不匹配! 数据库内容: " +mobile+ "; " + "响应内容: " + job.getString("mobile");
    20                                 }
    21                                 gender = vars.get("gender_" + j).toString();
    22                                 if(!job.getString("gender").equals(gender)) {
    23                                     Failure = true;  
    24                                     FailureMessage = Msg + " gender 数据不匹配! 数据库内容: " +gender+ "; " + "响应内容: " + job.getString("gender");  
    25                                 }
    26                                 firstRegistrationArea = vars.get("firstRegistrationArea_" + j).toString();
    27                                 if(!job.getString("firstRegistrationArea").equals(firstRegistrationArea)) {
    28                                     Failure = true;  
    29                                     FailureMessage = Msg + " firstRegistrationArea 数据不匹配! 数据库内容: " +firstRegistrationArea+ "; " + "响应内容: " + job.getString("firstRegistrationArea");  
    30                                 }
    31                                 age = vars.get("age_" + j).toString();
    32                                 if(!job.getString("age").equals(age)) {
    33                                     Failure = true;  
    34                                     FailureMessage = Msg + " age 数据不匹配! 数据库内容: " +age+ "; " + "响应内容: " + job.getString("age");  
    35                                 }
    36                                 business = vars.get("business_" + j).toString();
    37                                 if(!job.getString("business").equals(business)) {
    38                                     Failure = true;  
    39                                     FailureMessage = Msg + " business 数据不匹配! 数据库内容: " +business+ "; " + "响应内容: " + job.getString("business");  
    40                                 }
    41                                 memberLevel = vars.get("memberLevel_" + j).toString();
    42                                 if(!job.getString("memberLevel").equals(memberLevel)) {
    43                                     Failure = true;  
    44                                     FailureMessage = Msg + " memberLevel 数据不匹配! 数据库内容: " +memberLevel+ "; " + "响应内容: " + job.getString("memberLevel");  
    45                                 }
    46                                 userTypes = vars.get("userTypes_" + j).toString();
    47                                 if(!job.getString("userTypes").equals(userTypes)) {
    48                                     Failure = true;  
    49                                     FailureMessage = Msg + " userTypes 数据不匹配! 数据库内容: " +userTypes+ "; " + "响应内容: " + job.getString("userTypes");  
    50                                 }
    51                                 flag = true;
    52                                 break;
    53                             }
    54                         }
    55                        if(!flag) {
    56                             Failure = true;  
    57                             FailureMessage = "passengerId ="+passengerId+" 返回的内容与数据库内容不匹配!
    ";
    58                         }
    59      }
    60 } else {
    61     Failure = true;  
    62     String Msg = "返回的记录数与数据库记录数不匹配!
    ";
    63     FailureMessage = Msg + "数据库记录: " + M + "; " + "返回记录数: " + data.length();
    64 }
    View Code

    由于返回的参数含有list(列表),所以为了进行逐个的对比,需要加入循环,进行一次次遍历比较


     

    • Response返回json数据跟数据库进行对比

    代码如下:

     1 import org.json.*;
     2 
     3 json = prev.getResponseDataAsString();  
     4 System.out.println("响应内容: 
    " + json);
     5 JSONObject response = new JSONObject(json);
     6 JSONObject data = response.getJSONObject("data");
     7 
     8 String Msg = "PassengerId : "+${uid_1}+"
    ";
     9 nickName = vars.get("nickName_1").toString();
    10 if(!data.getString("nickName").equals(nickName)) {
    11     Failure = true;  
    12     FailureMessage = Msg + " nickName 数据不匹配! 数据库内容: " +nickName+ "; " + "响应内容: " + data.getString("nickName");
    13 }
    14 gender = vars.get("gender_1").toString();
    15 if(!data.getString("gender").equals(gender)) {
    16     Failure = true;  
    17     FailureMessage = Msg + " gender 数据不匹配! 数据库内容: " +gender+ "; " + "响应内容: " + data.getString("gender");  
    18 }
    19 mobile = vars.get("mobile_1").toString();
    20 if(!data.getString("mobile").equals(mobile)) {
    21     Failure = true;  
    22     FailureMessage = Msg + " mobile 数据不匹配! 数据库内容: " +mobile+ "; " + "响应内容: " + data.getString("mobile");  
    23 }
    24 age = vars.get("age_1").toString();
    25 if(!data.getString("age").equals(age)) {
    26     Failure = true;  
    27     FailureMessage = Msg + " age 数据不匹配! 数据库内容: " +age+ "; " + "响应内容: " + data.getString("age");  
    28 }
    29 business = vars.get("business_1").toString();
    30 if(!data.getString("business").equals(business)) {
    31     Failure = true;  
    32     FailureMessage = Msg + " business 数据不匹配! 数据库内容: " +business+ "; " + "响应内容: " + data.getString("business");  
    33 }
    34 memberLevel = vars.get("memberLevel_1").toString();
    35 if(!data.getString("memberLevel").equals(memberLevel)) {
    36     Failure = true;  
    37     FailureMessage = Msg + " memberLevel 数据不匹配! 数据库内容: " +memberLevel+ "; " + "响应内容: " + data.getString("memberLevel");  
    38 }
    View Code


     

    • 请求参数跟数据库的值进行对比

    代码如下:

     1 String Msg = "PassengerId : "+${uid_1}+"
    ";
     2 gender = vars.get("gender_1").toString();
     3 if(!gender.equals(${gender})) {
     4     Failure = true;  
     5     FailureMessage = Msg + " gender 数据不匹配! 数据库内容: " +gender+ "; " + "传入变量: " + ${gender};  
     6 }
     7 gender = vars.get("gender_1").toString();
     8 if(!gender.equals(${gender})) {
     9     Failure = true;  
    10     FailureMessage = Msg + " gender 数据不匹配! 数据库内容: " +gender+ "; " + "传入变量: " + ${gender};  
    11 }
    View Code
  • 相关阅读:
    Eclipse 中怎样自动格式化代码?
    如何使用Jfreechart生成柱状图?
    ADT OOP
    5.2 Construction for Reuse
    Lab6实验的一些思考
    软件构造的八个多维视图
    软件构造笔记5.1 Metrics, Morphology and External Observations of Reusability
    事后诸葛亮之感谢
    (Model)针对编译错误的友好提示
    第二次结对作业
  • 原文地址:https://www.cnblogs.com/ruichow/p/9539453.html
Copyright © 2011-2022 走看看