步骤:
1、根据自己使用的lr版面选择jdk
1)LR11 1.6 32位
2)LR12 1.7 32位
2、安装好jdk,并搭建java环境变量,安装好java代码编写工具
3、在src下创建一个包(我的是com.md5),将需要的jar文件放在lib目录下(没有的自己创建)
4、在包下放HttpClientRequest方法类、MD5算法类
5、写好Md5Request.java
6、将com.md5打包成jar文件md5_test.jar
file-->Project Structure-->Artifacts-->JAR
7、在lr新建java Vuser协议的脚本
8、在runtime setting 的classpath中添加jar文件:
1)lib中的jar文件
2)jdk安装目录lib下的dt.jar和tools.jar
3)自己打包的md5_test.jar
9、最后将Md5Request.java里面的代码复制到lr的action方法中
lr代码如下:
/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */ import lrapi.lr; 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; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; //引入md5文件 import com.md5.MD5; //引入HttpClientRequest文件 import com.md5.HttpClientRequest; public class Actions { public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { //调用md5加密算法 将密码加密 重置密码时传参要用 String md5pwd = MD5.GetMD5Code("123456"); lr.start_transaction("登录"); //登录 String loginRequest = HttpClientRequest.sendPost("http://192.168.145.130:8080/mobile/api/user/login","{"mobile":"18705092505","password":"123456"}"); System.out.println("登录响应数据:"+loginRequest); //创建JSONObject对象,获取登录响应数据loginRequest JSONObject loginRusult = new JSONObject(loginRequest); //获取code int loginCode = (int)loginRusult.get("code"); if (loginCode == 0){ lr.end_transaction("登录", lr.PASS); }else { lr.end_transaction("登录", lr.FAIL); } //获取token String token = (String)loginRusult.getJSONObject("data").get("token"); //重置密码 并判断是否成功 String resetpaypwd = HttpClientRequest.sendPost("http://192.168.145.130:8080/mobile/api/user/resetpaypwd","{"token":""+token+"","password":""+md5pwd+""}"); System.out.println("重置密码响应数据:"+resetpaypwd); //将token转换成URLcode String URLcode =""; try { URLcode = URLEncoder.encode(token,"UTF-8"); System.out.println("URLcode:"+URLcode); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } lr.start_transaction("获取订单列表"); //获取订单列表 String queryRequest = HttpClientRequest.sendGet("http://192.168.145.130:8080/mobile/api/order/getorders?","offset=0"+"&"+"size=15"+"&"+"token="+URLcode); JSONObject queryRusult = new JSONObject(queryRequest); int orderListCode = (int) queryRusult.get("code"); 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 }
过程中遇到的问题:在idea工具运行代码没有问题,在lr运行报错如下
解决如下:
原因:返回的响应编码问题
解决:在HttpClientRequest的sendGet请求中读取响应加上utf-8编码格式