一、loadrunner脚本创建
1.Insert - New step -选择Custom Request - web_custom_request
2.填入相应参数
3.生成脚本,并修改如下(参数中的引号"前需要加斜杠\转译)
Action()
{
lr_start_transaction("Sendorder");
web_reg_find("Search=Body",
"SaveCount=Sendorder_count",
"Text=ord_sn",
LAST);
web_custom_request("sendOrder.html",
"URL=http://192.168.1.205:9000/default/com.bybon.O2O.services.apiservice.getApiServiceMgr.getApiService.api",
"Method=POST",
"Resource=0",
"RecContentType=text/html",
"Referer=http://192.168.1.205:9000/default/com.bybon.O2O.services.apiservice.getApiServiceMgr.getApiService.api",
"Snapshot=t229.inf",
"Mode=HTTP",
"EncType=application/json",
"Body={\"sign\":\"963a8d91fab997a490918ebbfe71cba5\",\"param\":\"%7B%22ord_sn%22%3A%22%22%2C%22serve_type%22%3A%221%22%2C%22ord_platform%22%3A%22pcf%22%2C%22ord_src%22%3A%221%22%2C%22mem_id%22%3A%221905%22%2C%22wx_field%22%3A%22%22%2C%22mem_name%22%3A%2218551625706%22%2C%22zipcode%22%3A%22%22%2C%22postscript%22%3A%22%5Cu7559%5Cu8a00%22%2C%22tel%22%3A%2218551625706%22%2C%22email%22%3A%22%22%2C%22storecode%22%3A%22A042%22%2C%22sn%22%3A%22%22%2C%22imei%22%3A%22%22%2C%22phone_modelcode%22%3A%22080111%22%2C%22phone_color%22%3A%221%22%2C%22phone_volume%22%3A%222%22%2C%22suggest_scheme_code%22%3A%22080111.1.2.14.1%22%2C%22addressList%22%3A%5B%7B%22addr_type%22%3A%224%22%2C%22province%22%3A%222%22%2C%22city%22%3A%2252%22%2C%22district%22%3A%22506%22%2C%22address%22%3A%22test%22%2C%22zipcode%22%3A%22%22%2C%22tel%22%3A%2218551625706%22%2C%22user_name%22%3A%22%5Cu6d4b%5Cu8bd5%22%7D%5D%2C%22remark%22%3A%22%22%2C%22offerList%22%3A%22%22%2C%22appoint_stime%22%3A%222015-06-01+17%3A00%3A00%22%2C%22appoint_etime%22%3A%222015-06-01+19%3A00%3A00%22%2C%22bill_receiver_name%22%3A%22%22%2C%22bill_tel%22%3A%22%22%2C%22bill_province_name%22%3A%22%22%2C%22bill_city_name%22%3A%22%22%2C%22bill_area_name%22%3A%22%22%2C%22bill_address%22%3A%22%22%2C%22bill_title%22%3A%22%22%2C%22bill_company_name%22%3A%22%22%2C%22cnfdet_flag%22%3A2%7D\",\"svcCode\":\"EC013\",\"msgSrc\":\"SDFPHP\"}",
LAST);
if (strcmp(lr_eval_string("{Sendorder_count}"),"0") == 0)
{
lr_end_transaction("Sendorder", LR_FAIL);
return 0;
}
else
{
lr_end_transaction("Sendorder", LR_PASS);
}
return 0;
}
4.捕获页面返回值
返回格式{"code":"0","msg":"%E6%88%90%E5%8A%9F","data":{"ord_sn":"9000003486"}}
将光标移动到脚本顶部,插入关联点insert-new step-service-web_reg_save_param
在脚本顶部,插入并修改脚本如下
web_reg_save_param("user_tags",
"LB=ord_sn\":\"",
"RB=\"]}",
LAST);
在脚本尾部,加入输出日志验证是否获取成功
lr_message ("user_tags:%s", lr_eval_string("{user_tags}"));
5.对我们的接口做自动化功能测试,写验证脚本如下:
if (atoi(lr_eval_string("{user_tags}"))==0)
lr_output_message("user_tags:%s,succeed!",(lr_eval_string("{user_tags}")));
else if(atoi(lr_eval_string("{user_tags}"))==1)
lr_output_message("user_tags:%s,failed!",(lr_eval_string("{user_tags}")));
else
lr_output_message("user_tags:%s,unknow!",(lr_eval_string("{user_tags}")));
6.参数化脚本
运行脚本
---------------------------------------END---------------------------------------
二、web_custom_request和web_submit_data区别
web_custom_request方法可以发送POST和GET类型的请求;
web_submit_data只能发送POST类型的请求;
所有web_submit_data方法发送的请求都可以使用web_custom_request来实现
web_custom_request可以实现web_submit_data无法实现的请求,比如“查询所有邮件并删除”这个案例中,查询时我们使用关联把所有邮件对应的标识抓取成一个数组,如果使用web_submit_data来完成这个删除的请求,需要很多个web_submit_data请求才能完成,但使用web_custom_request就可以通过一个请求完成,方法是自己写代码拼一个web_custom_request 方法POST请求的Body值。
1. web_submit_data
请求中提交的数据格式:“Name=属性名称,”,“Value=属性值”
例如:
"Name=username″,"Value=12044″, ENDITEM,
"Name=password″,"Value=123456″, ENDITEM,
"Name=typeId″,"Value=1″, ENDITEM,
如果想提交的某个属性包含包含多个值(比如说批量删除),单个web_submit_data就无法处理了,只能通过多个web_submit_data来处理。
2. web_custom_request
提交的数据(body)格式:“Body=属性名称=属性值&属性名称=属性值&……”
下面是一个典型的web_submit_data和web_custom_request请求,可以看到web_custom_request中提交的数据(body)是以这样的方式存在的,如下:
web_submit_data("searchRecvOrgsname",
"Action=http://{url}/searchRecvOrgsname",
"Method=POST",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http://{url}/login_wj;jsessionid={jsessionid}",
"Snapshot=t18.inf",
"Mode=HTML",
ITEMDATA,
"Name=orgsId", "Value={orgsId}", ENDITEM,
"Name=code", "Value={order_end_station_code}", ENDITEM,
LAST);
web_custom_request("searchVehiclePopUp",
"URL=http://{url}/searchVehiclePopUp",
"Method=POST",
"TargetFrame=",
"Resource=0",
"RecContentType=text/html",
"Referer=http://{url}/login_wanjia;jsessionid={jsessionid}",
"Snapshot=t19.inf",
"Mode=HTML",
"EncType=application/x-www-form-urlencoded; charset=UTF-8",
"Body=&orgsId={orgsId}&order_start_station_id={order_start_station_id}&targetcode=order_truck_no&targetname=order_truck_name&targetid=order_truck_id",
LAST);
两种情况下的POST请求会被LoadRunner录制为web_custom_request:
1. 上文提到的批量提交多条同属性名称的数据的请求
2. header属性x-requested-by值为XMLHttpRequest的POST请求
这两种实现请求的方法还有一个需要注意的地方就是web_custom_request中body中的属性值如果包含一些特殊字符,必须通过URL编码,否则Web服务器会返回500错误,一个典型的例子是如果Body中包含ViewState,ViewState中常常有“=”之类的特殊字符,此时必须通过URL编码,LoadRuner中提供了一个这样的编码转换函数:
web_convert_param(“vs1″, “SourceEncoding=HTML”, “TargetEncoding=URL”, LAST);
3. web_custom_request函数详解
A.语法:
int web_custom_request( const char *RequestName, ,
[EXTRARES, ,] LAST );
B.返回值:返回LR_PASS(0)代表成功,LR_FAIL(1)代表失败。
C.参数:
(1)RequestName:步骤的名称,VuGen中树形视图中显示的名称。
(2)List of Attribute:属性列表,支持的属性有以下几种:
a.URL:页面地址。
b.Method:页面的提交方式,POST或GET。
c.TargetFrame:当前链接或资源所在Frame的名称。
除了Frame的名字,还可以指定下面的参数:
_BLANK:打开一个空窗口。
_PARENT:把最新更改过的的Frame替换为它的上级。
_SELF:替换最新更改过的的Frame。
_TOP:替换整个页面。
d.EncType:编码类型。
参考:http://blog.sina.com.cn/s/blog_6db2c206010133kn.html
来源:https://www.cnblogs.com/Viguss/p/4538338.html