loadrunner 全流程java Vusers脚本
/*
* 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 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.util.List;
import java.util.Map;
import testmd5.MD5;
import testmd5.HttpClientRequest;
public class Actions
{
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
//登陆
//登录URL地址
//登录请求的参数
String login_Data="{\"mobile\":\"13528223194\",\"password\":\"123456\"}";
lr.start_transaction("login");
//调用HttpClientRequest类中的sendPost
String login_PostResult = HttpClientRequest.sendPost(login_URL,login_Data);
//打印得到的结果
System.out.println("登录返回值为:"+login_PostResult);
//新建一个json对象
JSONObject login_jsonbj =new JSONObject(login_PostResult);
int login_code =(int) login_jsonbj.get("code");
if (login_code==0)
{
lr.end_transaction("login", lr.PASS);
}
else
{
lr.end_transaction("login", lr.FAIL);
System.out.println("下订单返回的状态码为:"+login_code);
}
String login_msg =(String) login_jsonbj.get("msg");
System.out.println("登录返回的状态码为:"+login_msg);
//返回的token
String token =(String) login_jsonbj.getJSONObject("data").get("token");
System.out.println("登录返回的token为:"+token);
//重置支付密码
//重置支付密码URL地址
//密码加密转换
String resetpaypwd_newPasswd =MD5.GetMD5Code("123456");
//重置支付密码请求的参数
String resetpaypwd_Data="{\"token\":\""+token+"\",\"password\":\""+resetpaypwd_newPasswd+"\"}";
//调用HttpClientRequest类中的sendPost
lr.start_transaction("resetpaypwd");
String resetpaypwd_PostResult = HttpClientRequest.sendPost(resetpaypwd_URL,resetpaypwd_Data);
//打印得到的结果
System.out.println("重置支付密码返回值为:"+resetpaypwd_PostResult);
JSONObject resetpaypwd_jsonbj =new JSONObject(resetpaypwd_PostResult);
int resetpaypwd_code =(int) resetpaypwd_jsonbj.get("code");
if (resetpaypwd_code==0)
{
lr.end_transaction("resetpaypwd", lr.PASS);
}
else
{
lr.end_transaction("resetpaypwd", lr.FAIL);
System.out.println("下订单返回的状态码为:"+resetpaypwd_code);
}
String esetpaypwd_msg =(String) resetpaypwd_jsonbj.get("msg");
System.out.println("重置支付返回的状态码为:"+esetpaypwd_msg);
//下订单
//下订单URL地址
//下单请求的参数
String addorder_Data="{\"token\":\""+token+"\",\"getAddrId\":1,\"getCarId\":23,\"payType\":2,\"remark\":\"This is a remark\",\"price\":2.50,\"orders\":[{\"getTime\":1450921104000,\"goodss\":[{\"goodsId\":93,\"count\":1},{\"goodsId\":93,\"count\":1}]}],\"invoiceTitle\":\"abcd\"}";
//调用HttpClientRequest类中的sendPost
lr.start_transaction("addorder");
String addorder_PostResult = HttpClientRequest.sendPost(addorder_URL,addorder_Data);
//打印得到的结果
System.out.println("下订单返回值为:"+addorder_PostResult);
JSONObject addorder_jsonbj =new JSONObject(addorder_PostResult);
int addorder_code =(int) addorder_jsonbj.get("code");
if (addorder_code==0)
{
lr.end_transaction("addorder", lr.PASS);
}
else
{
lr.end_transaction("addorder", lr.FAIL);
System.out.println("下订单返回的状态码为:"+addorder_code);
}
String addorder_msg =(String) addorder_jsonbj.get("msg");
System.out.println("下订单返回信息为:"+addorder_msg);
//返回值里面没有payid信息,why
String payid =(String) addorder_jsonbj.get("msg");
System.out.println("下订单返回的msg为:"+payid);
//支付订单
//支付订单URL地址
//支付订单请求的参数
String pay_Data="{\"token\":\""+token+"\",\"payId\":\"2015092019551300001\",\"payPwd\":\""+resetpaypwd_newPasswd+"\",\"platform\":1}";
lr.start_transaction("pay");
//调用HttpClientRequest类中的sendPost
String pay_PostResult = HttpClientRequest.sendPost(pay_URL,pay_Data);
//打印得到的结果
System.out.println("支付订单返回值为:"+pay_PostResult);
//新建一个json对象
JSONObject pay_jsonbj =new JSONObject(pay_PostResult);
int pay_code =(int) pay_jsonbj.get("code");
System.out.println("支付订单返回的状态码为:"+pay_code);
if (pay_code==0)
{
lr.end_transaction("pay", lr.PASS);
}
else
{
lr.end_transaction("pay", lr.FAIL);
System.out.println("支付订单返回的状态码为:"+pay_code);
}
String pay_msg =(String) pay_jsonbj.get("msg");
System.out.println("支付订单返回的msg为:"+pay_msg);
//获得订单
String tokenUTF8 = "";
// 进行token转码为URLcode
try {
tokenUTF8 = URLEncoder.encode(token, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//获得订单URL地址
//拼接参数部分--直接跟转码后的token
String param = "offset=0"+"&"+"token="+tokenUTF8;
lr.start_transaction("getorders");
//调用HttpClientRequest类中的sendGet
String getorders_PostResult = HttpClientRequest.sendGet(getorders_URL,param);
//打印得到的结果
System.out.println("获得订单返回值为:"+getorders_PostResult);
//新建一个json对象
JSONObject getorders_jsonbj =new JSONObject(getorders_PostResult);
int getorders_code =(int) getorders_jsonbj.get("code");
if (getorders_code==0)
{
lr.end_transaction("getorders", lr.PASS);
}
else
{
lr.end_transaction("getorders", lr.FAIL);
System.out.println("获得订单返回的状态码为:"+getorders_code);
}
String getorders_msg =(String) getorders_jsonbj.get("msg");
System.out.println("获得订单返回的msg为:"+getorders_msg);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}