1、环境准备:
LoadRunner11----->对应JDK1.6版本(32位)
LoadRunner12----->对应JDK1.7版本(32位)
(一)、JDK下载安装完成后,配置环境变量:
1)、系统变量→新建 JAVA_HOME 变量 。 变量值填写jdk的安装目录(例如:C:\Program Files (x86)\Java\jdk1.7.0_25)
2)、系统变量→寻找 Path 变量→编辑,在变量值最后输入 %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
3)、系统变量→新建 CLASSPATH 变量,变量值填写 .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar(注意最前面有一点)
系统变量配置完毕,检验是否配置成功 运行cmd 输入 java -version (java 和 -version 之间有空格)
若如图所示 显示版本信息 则说明安装和配置成功。(红色框中说明是32位版本)
(二)、下载java编写工具idea
下载地址:https://www.jetbrains.com/idea/download/#section=windows,Community免费版
2、编写java代码,实例代码以md5加密算法为例,实现登录和获取订单列表的功能。
1)、使用idea工具,新建一个项目,命名为:myjavatest
2)、在该项目下,新建一个包,命名为:mytestMd5
3)、将java代码拷贝到包下,最终目录为:
4)、将lib文件夹引入,解决报错问题:
1、选择File文件,如下:
2、
3、添加之后的显示:
4、将导入的Lib勾选:
5、至此,报错信息已解决,可以直接调试、运行代码。最终目录结构:
3、将调试通过的文件,导出为jar。
1)、选择File:
2)、
3)、
4)、
5)、进行构建:
6)、完成后,目录文件夹下生成jar包文件:
4、在loadrunner中创建JavaVuser协议脚本:
1)、将调试好的java代码拷贝到loadrunner中:
import lrapi.lr; //将包文件引入 import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; //引入MD5文件 import mytestMd5.MD5; //引入HttpClientRequest文件 import mytestMd5.HttpClientRequest; public class Actions { public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { // 定义登录URL地址 String login_url = "http://******/mobile/api/user/login"; // 定义请求的参数 String login_data = "{\"mobile\":\"130********\",\"password\":\"123456\"}"; // 调用HttpClientRequest类里面的sendpost方法 //添加事物函数 lr.start_transaction("登录"); String PostResult = HttpClientRequest.sendPost(login_url, login_data); System.out.println("登录返回:"+ PostResult); JSONObject jsonobj = new JSONObject(PostResult); int status = (int) jsonobj.get("code"); //根据status返回值,判断登录事物是否成功 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); String modifypaypwd_url = "http://*********/mobile/api/user/resetpaypwd"; //调用md5类里面的getmd5code这个方法 String newPasswod = MD5.GetMD5Code("123456"); System.out.println("newPasswod:" +newPasswod); String modifypaypwd_data = "{\"token\":\""+token+"\",\"password\":\""+newPasswod+"\"}"; String PostMd5Result = HttpClientRequest.sendPost(modifypaypwd_url, modifypaypwd_data); System.out.println("PostMd5Result:" + PostMd5Result); return 0; }//end of action public int end() throws Throwable { return 0; }//end of end }
2)、设置RunTime Settings:
1、将lib文件下的包导入
2、将jdk目录lib下的dt.jar、tools.jar导入
3、将构建完成的jar包导入
最终导入的文件如下:
3)、运行效果如下:
Starting iteration 1. Maximum number of concurrent connections per server: 4 [MsgId: MMSG-26989] Starting action Actions. Notify: Transaction "登录" started. System.out: 登录返回:{"code":0,"msg":"成功调用","data":{"id":443,"username":"test11","sex":1,"age":20,"mobile":"15989576517","email":"[email protected]","gqid":"4000326","money":0.0,"pmoney":100.0,"createtime":1448868217000,"lasttime":1555392298486,"token":"TGBCHgxZMHv83Fu8HtizNy+5m1fVDGlnh/EO4TBvh7K97TNe1L9tEaJNlV/bCCrBxRU/jxaEwo/fQbjJJq9BrA==","identity":"7ca9dfc5987093f8"}} Notify: Notify: Transaction "登录" ended with a "Pass" status (Duration: 0.5548). System.out: code:0 Notify: System.out: token:TGBCHgxZMHv83Fu8HtizNy+5m1fVDGlnh/EO4TBvh7K97TNe1L9tEaJNlV/bCCrBxRU/jxaEwo/fQbjJJq9BrA== Notify: System.out: newPasswod:e10adc3949ba59abbe56e057f20f883e Notify: System.out: PostMd5Result:{"code":0,"msg":"成功调用","data":{}} Notify: Ending action Actions. Ending iteration 1.
同样的操作,获取订单列表代码如下:
/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */ import lrapi.lr; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.codehaus.jettison.json.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; //引入testmd5.HttpClientRequest文件 import mytestMd5.HttpClientRequest; public class Actions { public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { // 定义登录URL地址 String login_url = "http://********/mobile/api/user/login"; // 定义请求的参数 String login_data = "{\"mobile\":\"130********\",\"password\":\"123456\"}"; // 调用HttpClientRequest类里面的sendpost方法 //添加登录事务函数 lr.start_transaction("登录"); String PostResult = HttpClientRequest.sendPost(login_url, login_data); System.out.println("登录返回:"+ PostResult); JSONObject jsonobj = new JSONObject(PostResult); int status = (int) jsonobj.get("code"); //根据status返回值,判断登录事务是否成功 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); String tokenUTF8 = ""; // 进行URLcode try { tokenUTF8 = URLEncoder.encode(token, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String get_url = "http://**********/mobile/api/order/getorders?"; String param = "offset=0"+"&"+"token="+token; // 把token替换成tokenUTF8 String Param = param.replace(token, tokenUTF8); //添加获取订单列表事务 lr.start_transaction("获取订单列表"); String GetResult = HttpClientRequest.sendGet(get_url, Param); System.out.println("GetResult:" + GetResult); JSONObject jsonbb = new JSONObject(PostResult); int statusbb = (int) jsonbb.get("code"); //根据statusbb返回值,判断获取订单列表事务是否成功 if(statusbb == 0) { lr.end_transaction("获取订单列表", lr.PASS); } else { lr.end_transaction("获取订单列表", lr.FAIL); } System.out.println("code:" +statusbb); return 0; }//end of action public int end() throws Throwable { return 0; }//end of end }
运行结果如下:
Starting iteration 1. Maximum number of concurrent connections per server: 4 [MsgId: MMSG-26989] Starting action Actions. Notify: Transaction "登录" started. System.out: 登录返回:{"code":0,"msg":"成功调用","data":{"id":443,"username":"test11","sex":1,"age":20,"mobile":"15989576517","email":"[email protected]","gqid":"4000326","money":0.0,"pmoney":100.0,"createtime":1448868217000,"lasttime":1555393646521,"token":"TGBCHgxZMHv83Fu8HtizNy+5m1fVDGlnh/EO4TBvh7K97TNe1L9tEaJNlV/bCCrBxRU/jxaEwo/fQbjJJq9BrA==","identity":"7ca9dfc5987093f8"}} Notify: Notify: Transaction "登录" ended with a "Pass" status (Duration: 0.4899). System.out: code:0 Notify: System.out: token:TGBCHgxZMHv83Fu8HtizNy+5m1fVDGlnh/EO4TBvh7K97TNe1L9tEaJNlV/bCCrBxRU/jxaEwo/fQbjJJq9BrA== Notify: Notify: Transaction "获取订单列表" started. System.out: null--->[HTTP/1.1 200 OK] Notify: System.out: Date--->[Tue, 16 Apr 2019 05:47:26 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: Notify: Transaction "获取订单列表" ended with a "Pass" status (Duration: 0.1213).
原文地址:https://www.cnblogs.com/benben-wu/p/10716578.html