接口的请求参数和返回结果均是JSON字符串,请求能够用POST或者GET方法。先说GET方法:web
1、GET方法测试json
Action() { //添加集合点 lr_rendezvous("jihedian"); lr_start_transaction("getTop10"); //插入检查点,检查返回值是否包含testName web_reg_find( "Search=Body", "Text=testName", LAST ); //发送get请求 web_url("www.xxx.com", "URL=http://192.168.3.33:9200/_search?{\"query\":{\"bool\":{\"must\":[{\"term\":{\"plateNumNond\":\"<NewParam>\"}}]}", "TargetFrame=", "Resource=0", "RecContentType=application/json", "Snapshot=t1.inf", "Mode=HTML", LAST ); lr_end_transaction("getTop10", LR_AUTO); //打印本次参数 lr_output_message( "the platenum is #%s", lr_eval_string( "{NewParam}" ) ); return 0; }
说明:查看服务器返回的结果需在Vuser-Runtime-settings的log选项下,勾选Enable-logging、Extended log、Data returned by server ;
2、POST方法测试数组
post方法有两种:web_submit_date和web_custom_request函数,web_submit_date不支持json串:服务器
脚本能够自行编码,也能够:app
Action()函数
{post
web_submit_data("login",
"Action=http://xxx/login?",
"Method=POST",
"TargetFrame=",
"RecContentType=application/json",
"Snapshot=t5.inf",
"Mode=HTTP",
ITEMDATA,
"Name=params","Value={\"param\":{\"userAccount\":\"{UserName}\",\"passWord\":\"e10adc3\",\"device\":\"IOS\"},\"version\":\"v1.0\",\"loginLog\":\"{testTimeParam}\"}",ENDITEM,
LAST );
或者:测试
lr_start_transaction("querybypost"); //插入检查点,检查返回值是否包含t_query_data web_reg_find( "Text=max_score", LAST ); web_custom_request("querybypost", //VuGen中树形视图中显示的名称 "Url=http://192.168.3.33:9200/_search", //请求url "Method=POST", "Resource=0", "Mode=HTTP", //请求方式 "Referer=", "EncType=application/json", //指定响应头的Content-Type,这里是JSON "RecContentType=application/json;charset=UTF-8", //指定请求头的Content-Type,这里是JSON "Body={\"query\":{\"bool\":{\"must\":[{\"term\":{\"plateNumNond\":\"<PlateNumNond>\"}}],\"must_not\":[],\"should\":[]}},\"from\":0}", //body的内容 LAST); lr_end_transaction("querybypost", LR_AUTO); lr_output_message( "PlateNumNond on iteration #%s", lr_eval_string( "<PlateNumNond>" ) );
//响应中文乱码转换
lr_convert_string_encoding(lr_eval_string("{
PlateNumNond
}"), “utf-8”,LR_ENC_SYSTEM_LOCALE,"BM");
lr_error_message(lr_eval_string("{BM}"));
}
说明:在LR中参数化标志是{},接口参数body里面也是{},因此在body里面参数化的时候用<>代替,
设置方法:Tool - General Options - Parameterization 中将Parameter Braces 改成<>便可
post请求参数有三种格式:1.application/x-www-form-urlencoded 键值对;2.multipart/form-data 表单;
三、application/json Json串,根据本身的须要修改就EncType值能够;
键值对和表单写法一致:
参数解释:编码
sourceString:被转换的源字符串。url
fromEncoding:转换前的字符编码。
toEncoding:要转换成为的字符编码。
paramName:转换后的目标字符串。
常量和值得关系:
Constant |
|
3、web_custom_request和web_submit_data区别