LoadRunner Vuser测试脚本添加前置条件举例

调用接口前需要先获取登陆token,放入消息头中。

  1 /*
  2  * LoadRunner Java script. (Build: 3020)
  3  * 
  4  * Script Description: 接口性能测试脚本 07-查询用户新增金币数
  5  *                     
  6  */
  7 
  8 import lrapi.lr;
  9 import lrapi.web;
 10 
 11 public class Actions{
 12     
 13     public int init() throws Throwable {
 14 //        检查点
 15         web.reg_find("Text="retCode":"0000"",new String[]{"FAIL=NOTFOUND","SaveCount=StatusCount","LAST"});
 16         
 17 //        关联参数
 18         web.reg_save_param("token",
 19                            new String []{
 20                                "NOTFOUND=ERROR",
 21                            "LB=loginToken":"",
 22                            "RB=",",
 23                            "Search=Body",
 24                            "LAST"});
 25                            
 26         lr.think_time(0.2);
 27 //        事务开始点
 28         lr.start_transaction("获取LoginToken的事务");
 29 //        web.set_sockets_option("SSL_VERSION","TLS");
 30       
 31         
 32 //        接口调用
 33            web.custom_request("getLoginToken",
 34                            "METHOD=POST",
 35                            new String[]{"URL=http://10.15.1.19/arowanaintl/user/loginByPass",
 36 //                           "Body={"channelId"="autotest","clientId"="1"}",
 37                                "Body=channelId=autotest&clientId=1&phone=0825500100&pass=bfe7c8de45faf6042b42d371f52640ba",
 38                            "LAST"});
 39 
 40 //        lr.output_message(lr.eval_string("{token}"));
 41         
 42 //       判断接口返回是否查询成功
 43        if(lr.eval_int("{StatusCount}")==1){
 44 //               lr.output_message("接口返回成功!");
 45             lr.end_transaction("获取LoginToken的事务", lr.PASS);
 46         
 47         }
 48         else{
 49             lr.end_transaction("获取LoginToken的事务", lr.FAIL);
 50             lr.error_message("接口返回失败!");
 51             return 1;
 52         }
 53         
 54 //        lr.output_message(lr.eval_string("{token}"));
 55         return 0;
 56     }//end of init
 57 
 58 
 59     public int action() throws Throwable {
 60 
 61         
 62         //        检查点
 63         web.reg_find("Text="retCode":"0000"",new String[]{"FAIL=NOTFOUND","SaveCount=StatusCount2","LAST"});
 64         
 65         lr.think_time(0.2);
 66         //        事务开始点
 67         lr.start_transaction("接口调用事务");
 68 //        web.set_sockets_option("SSL_VERSION","TLS");
 69         
 70         //加消息头
 71         web.add_header("loginToken",lr.eval_string("{token}"));
 72         
 73 //        接口调用
 74            web.custom_request("07-queryUserIncreaseGolds",
 75                            "METHOD=POST",
 76                            new String[]{"URL=http://10.15.1.19/arowanaintl/gold/queryUserIncreaseGolds",
 77 //                           "Body={"channelId"="autotest","clientId"="1"}",
 78                                "Body=channelId=autotest&clientId=123456&uid=88192b97-9b18-4e28-9dce-f9e9bb3f3bd1",
 79                            "LAST"});
 80 //        
 81         
 82 //       判断接口返回是否查询成功
 83        if(lr.eval_int("{StatusCount2}")==1){
 84 //               lr.output_message("接口返回成功!");
 85             lr.end_transaction("接口调用事务", lr.PASS);
 86         
 87         }
 88         else{
 89             lr.end_transaction("接口调用事务", lr.FAIL);
 90             lr.error_message("接口返回失败!");
 91         }
 92 
 93         return 0;
 94     }//end of action
 95 
 96 
 97     public int end() throws Throwable {
 98         return 0;
 99     }//end of end
100 }
原文地址:https://www.cnblogs.com/gongxr/p/9447931.html