接口的請求參數和返回結果均是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區別