一.java vuser学习重点以及操作步骤
1.搭建jdk环境变量
LR11:---->>jdk 1.6版本,32位
LR12:---->>jdk 1.7版本,32位
2.用idea工具来调试的算法
(1)找开发获取算法
(2)将开发给的算法(java文件)放到main->java目录下进行调试,调试OK后,再进行调用
(3)在test->java目录下新建class,命名TestPayPasswdMd5,调试HttpClientRequest和MD5算法
(4)在test->java目录下新建class,命名TestGetPayList,调试获取订单接口
(5)将项目打成jar包,file->Project Structure->Artifacts, Add->jar->From mudules with dependencies,选择main class后保存。选择Build->Build Artifacts,对刚刚生成的jar 进行Build。
(6)在lr12中,新建脚本,选择Java vuser协议,点击Runtime settings,选择classpath,添加刚做好的jar包
(7)将在idea中调试好的脚本拷贝到action中,并加上if判断,代码如下
/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */ import lrapi.lr; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import pertest.HttpClientRequest; import pertest.MD5; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; public class Actions { public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { String base_url="http://192.168.12.135:8080/mobile/api/"; lr.start_transaction("登录"); // 定义登录URL地址 String login_url = base_url+"user/login"; // 定义请求的参数 String login_data = "{"mobile":"13578577663","password":"111111"}"; // 调用HttpClientRequest类里面的sendpost方法 String PostResult = HttpClientRequest.sendPost(login_url, login_data); //System.out.println("登录返回:"+ PostResult); JSONObject jsonobj = new JSONObject(PostResult); int status = (int) jsonobj.getInt("code"); if(status == 0){ lr.end_transaction("登录", lr.PASS); }else{ lr.end_transaction("登录", lr.FAIL); } // System.out.println("code:" +status); // 获取token值 String token = (String) jsonobj.getJSONObject("data").get("token"); //System.out.println("token:" +token); lr.start_transaction("重置密码"); String resetpaypwd_url =base_url+"user/resetpaypwd"; // 调用md5类里面的getmd5code这个方法 String newpwd = "123456"; String utf8 = new String(newpwd.getBytes("ISO-8859-1"),"UTF-8"); //将密码转换为MD5 String newPasswod = MD5.GetMD5Code("123456"); //System.out.println("newPasswod:" +newPasswod); String resetpaypwd_data = "{"token":""+token+"","password":""+newPasswod+""}"; String PostMd5Result = HttpClientRequest.sendPost(resetpaypwd_url, resetpaypwd_data); System.out.println("PostMd5Result:" + PostMd5Result); JSONObject jsonobj_resetpaypwd = new JSONObject(PostMd5Result); int resetpaypwd_status = (int) jsonobj_resetpaypwd.getInt("code"); System.out.println(resetpaypwd_status); if(resetpaypwd_status == 0){ lr.output_message("密码转换MD5成功!重置支付密码成功"); lr.end_transaction("重置密码", lr.PASS); }else{ lr.end_transaction("重置密码", lr.FAIL); } lr.start_transaction("获取订单列表"); String tokenUTF8 = ""; // 进行URLcode try { tokenUTF8 = URLEncoder.encode(token, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String get_url = base_url+"order/getorders?"; String param = "offset=0"+"&"+"token="+token; // 把token替换成tokenUTF8 String Param = param.replace(token, tokenUTF8); String GetResult = HttpClientRequest.sendGet(get_url, Param); System.out.println("GetResult:" + GetResult); JSONObject GetResultobj = new JSONObject(GetResult); int orderListCode = (int) GetResultobj.getInt("code"); System.out.println(orderListCode); if(orderListCode == 0){ lr.end_transaction("获取订单列表", lr.PASS); }else{ lr.end_transaction("获取订单列表", lr.FAIL); } return 0; }//end of action public int end() throws Throwable { return 0; }//end of end }
运行结果:
Virtual User Script started at : 2019/11/21 21:40:13 Starting action vuser_init. Ending action vuser_init. Running Vuser... Starting iteration 1. Maximum number of concurrent connections per server: 4 [MsgId: MMSG-26989] Starting action Actions. Notify: Transaction "登录" started. Notify: Transaction "登录" ended with a "Pass" status (Duration: 0.8088). Notify: Transaction "重置密码" started. System.out: PostMd5Result:{"code":0,"msg":"成功调用","data":{}} Notify: System.out: 0 Notify: 密码转换MD5成功!重置支付密码成功 Notify: Transaction "重置密码" ended with a "Pass" status (Duration: 0.0232). Notify: Transaction "获取订单列表" started. System.out: null--->[HTTP/1.1 200 OK] Notify: System.out: Date--->[Wed, 03 Apr 2019 13:37:53 GMT] Notify: System.out: Transfer-Encoding--->[chunked] Notify: System.out: Content-Type--->[application/json;charset=UTF-8] Notify: System.out: Server--->[Apache-Coyote/1.1] Notify: System.out: GetResult:{"code":0,"msg":"成功调用","data":{"lists":[]}} Notify: System.out: 0 Notify: Notify: Transaction "获取订单列表" ended with a "Pass" status (Duration: 0.0611). Ending action Actions. Ending iteration 1. Ending Vuser... Starting action vuser_end. Ending action vuser_end. Vuser Terminated.