LoadRunner日常总结

HTTPS请求

LoadRunner对HTTPS接口进行测试时,最好加上web_set_sockets_option("SSL_VERSION","TLS")

LoadRunner在对HTTPS接口进行请求时,可能出现Error -27778: SSL protocol error when attempting to connect with host "XXX" [MsgId: MERR-27778]错误。

设置Vuser -> Run-time Setting找到Internet Protocol -> Preferences -> Advanced勾选winlnet replay instead of sockets(windows only)选项。

日志中文打印

通常在请求时想看到请求参数、返回结果等数据,可以在Vuser -> run-time setting -> general -> log勾选extended log且勾选其下的三个选项。但是这种方式不能解决中文乱码问题。

可以将中文数据通过web_reg_save_param单独提取出来lr_convert_string_encoding转码后通过lr_output_messagelr_log_message打印:

1
2
3
4
web_reg_save_param("result", "LB=message\":\"", "RB=\"", LAST);
// web_custom_request请求
lr_convert_string_encoding(lr_eval_string("{result}"), "utf-8", NULL, "msg");
lr_output_message("message--------%s",lr_eval_string("{msg}"));

中文参数乱码

通常在通过LoadRunner请求接口时,若请求参数中存在中文参数,虽然在Replay Log中打印的内容可能并没有乱码,但是请求到服务器可能就乱码了,从而导致接口请求失败。

为了解决中文参数导致的中文乱码,可以将中文参数提取出来通过lr_convert_string_encoding进行转码后使用。首先通过通过lr_save_string将中文参数参数化,也可以到Parameter List进行设置。然后将参数转成UTF-8,最后将参数转成URL编码

1
2
3
4
5
6
lr_save_string("奚姝","name");
lr_convert_string_encoding(lr_eval_string("{name}"), LR_ENC_SYSTEM_LOCALE, LR_ENC_UTF8, "encode_name");
lr_save_string(lr_eval_string("{encode_name}"),"name");
web_convert_param("name", "SourceEncoding=PLAIN", "TargetEncoding=URL", LAST);

lr_log_message("参数化结果name:%s", lr_eval_string("{name}"));

Web请求

LR可以通过web_custom_request函数发送POST或者GET请求。对于普通POST请求,未将请求参数放到RequestBody中的,可以将参数在Body中通过&符号进行拼接。

1
2
3
4
5
web_custom_request("web_custom_request","URL={url}","Method=POST",
"Resource=0","RecContentType=Application/json","Referer=","Mode=HTML",
"EncType=application/x-www-form-urlencoded;charset=UTF-8",
//"EncType=application/json;charset=UTF-8",
"Body=name={name}&phone={phone}",LAST);

对于请求参数放到RequestBody中的,可以直接将请求参数转成字符串放到Body中。或者放到RAW_BODY_STARTRAW_BODY_END之间,其中200指代参数长度。

1
2
3
4
5
6
7
8
9
web_custom_request("web_custom_request","URL={url}","Method=POST","Resource=0",
"RecContentType=application/json","Referer=","Mode=HTTP",
"EncType=application/json;charset=UTF-8",
//RAW_BODY_START,
//"{\"id\":\"{id}\",\"name\":\"{name}\",\"mobile\":\"{mobile}\"}",
//200,
//RAW_BODY_END,
"Body={\"id\":\"{id}\",\"name\":\"{name}\",\"mobile\":\"{mobile}\"}",
LAST);

对于响应结果的提取通过web_reg_save_param("code","LB=response_code\":\"","RB=\"",LAST);提取出来,用来进行事务成功与否判断。

1
2
3
4
5
6
7
8
9
10
11
12
13
lr_start_transaction ("接口A");

web_reg_save_param("code","LB=response_code\":\"","RB=\",\"",LAST);
web_custom_request("web_custom_request","URL={url}","Method=POST","Resource=0",
"RecContentType=application/json","Referer=","Mode=HTTP",
"EncType=application/json;charset=UTF-8",
"Body={\"id\":\"{id}\",\"name\":\"{name}\"}",LAST);

if (strcmp(lr_eval_string("{coke}"), "00") == 0){
lr_end_transaction("接口A", LR_PASS);
}else{
lr_end_transaction("接口A", LR_FAIL);
}