1.将原有的请求方法进行封装(Post/Get)打包成相应的jar包,编译、打包选择的jdk版本为1.6,否则会导致生成的jar包不能在LR中跑起来
备注:此处为反编译后显示的代码
2.手动编写请求脚本
GET 请求:
import java.util.HashMap;
import java.util.Map;
import lrapi.lr;
import com.hzins.pluto.HttpPost_Method;
import com.hzins.pluto.HttpGet_Method;
public class Actions
{
String Detail="http://xxx.xxx.xxx/product/detail-500.html";
HttpGet_Method get =null;
public int init() throws Throwable {
get=new HttpGet_Method();
return 0;
}//end of init
public int action() throws Throwable {
String response3=get.do_httpget(Detail,null);
lr.output_message("response3:"+response3);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
Post请求
import java.util.HashMap;
import java.util.Map;
import lrapi.lr;
import com.hzins.pluto.HttpPost_Method;
public class Actions
{
String GetProductSellCount="http://xxx.xxx.xxx/Public/GetProductSellCount";
HttpPost_Method post=null;
public int init() throws Throwable {
post=new HttpPost_Method();
return 0;
}//end of init
public int action() throws Throwable {
lr.start_transaction("schedule");
Map<String, String> nameValuePairs2 = new HashMap<String, String>();
nameValuePairs2.put("productId", "500");
String response2=post.do_post(GetProductSellCount, nameValuePairs2);
lr.output_message("response:"+response2);
if (response2.contains("Count"))
lr.end_transaction("schedule", 0);
else {
lr.end_transaction("schedule", 1);
}
lr.think_time(1);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
3.将jdk包和依赖包加载到LR的运行目录
4.运行脚本
Post执行后的结果:
Virtual User Script started at : 2016-03-31 09:23:12
Starting action vuser_init.
Ending action vuser_init.
Running Vuser...
Starting iteration 1.
Starting action Actions.
Notify: Transaction "schedule" started.
response:"[{\"Id\":500,\"Count\":2238}]"
Notify: Transaction "schedule" ended with "Pass" status (Duration: 0.3856).
Ending action Actions.
Ending iteration 1.
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated.
下载Lr.jar包,方便在Eclipse中编写代码,将代码COPY到LR中进行编译和调试,LoadRunner只支持JDK1.6 32位,否则会执行不过
http://download.csdn.net/detail/u011053107/8166389