LoadRunner字符串编码转换函数:lr_convert_string_encoding

来源:互联网 发布:赌博庄家抽水算法 编辑:程序博客网 时间:2024/06/04 18:24

        最近在调试脚本的过程中,多次用到LoadRunner字符串编码转换函数lr_convert_string_encoding,这里简单总结一下,方便以后参考~

1.函数实现

Loadrunner自带函数,直接使用即可

lr_convert_string_encoding

//C Languageint lr_convert_string_encoding( const char *sourceString, const char *fromEncoding,const char *toEncoding, const char *paramName);
官方参数解释:

sourceString

The string to convert

fromEncoding

The encoding of the sourceString

toEncoding

The encoding to convert of the string saved in parameterparamName

paramName

The name of the parameter in which the destination string will be saved


对应的中文参数解释:

   sourceString:被转换的源字符串。

   fromEncoding:转换前的字符编码。

   toEncoding:要转换成为的字符编码。

   paramName:转换后的目标字符串。

lr_convert_string_encoding converts a string encoding betweenthe following encodings: System locale, Unicode, and UTF-8. The function savesthe result string, including its terminating NULL, in the parameterparamName.

根据上面的说明,这个函数其实只是把字符串在三种编码格式中做转换,所以其实功能也是有限的,不过也已经可以适用到我们常见的一些场景了。

常量和值的对应关系如下:

Possible values for 'fromEncoding'and 'toEncoding' :

Constant

Value

LR_ENC_SYSTEM_LOCALE

NULL

LR_ENC_UTF8

"utf-8"

LR_ENC_UNICODE

"ucs-2"


2.Loadrunner中的使用

   在URL请求的返回消息中,我们有时候会遇到中文返回被编码的情况,不易识别,这时候就可以用编码转换函数,方便调试脚本。


Action(){int compare=1;web_reg_save_param("payRespBody","LB=","RB=","NotFound=ERROR","Search=Body",LAST);web_reg_save_param("responseMsg","LB=responseMsg\":\"","RB=\",","NotFound=ERROR","Search=All",LAST);web_custom_request("pay.htm", "URL=http://xxx/a/b/pay.htm?x={orderId}&pwd=x", "Method=POST", "TargetFrame=", "Resource=0", "RecContentType=application/json", "Referer=", "Mode=HTML", "EncType=application/x-www-form-urlencoded; charset=UTF-8", LAST);lr_convert_string_encoding(lr_eval_string("{responseMsg}"),"utf-8",NULL,"msg");//将返回消息responseMsg值做编码转换lr_output_message("转换编码前payRespBody----%s",lr_eval_string("{payRespBody}"));lr_convert_string_encoding(lr_eval_string("{payRespBody}"),"utf-8",NULL,"bodymsg"); <pre name="code" class="cpp">         //将返回消息的body做编码转换compare=strcmp(lr_eval_string("{msg}"),"支付成功");//将编码转换后的responseMsg值与可识别的中文做比较,判断事务成功或失败if(compare==0){lr_end_transaction("3--支付",LR_PASS);}else{lr_end_transaction("3--支付",LR_FAIL);lr_output_message("支付失败orderId:----%s,responseMsg----%s",lr_eval_string("{orderId}"),lr_eval_string("{msg}"));//打印转换编码后的返回lr_output_message("转换编码后payRespBody----%s",lr_eval_string("{bodymsg}"));//打印转换编码后的返回,便于定位问题}return 0;}

通过对比转换前和转换后的值可以发现,编码转换后,中文变得可识别,方便了脚本调试和脚本问题定位及解决。
Action.c(x): 转换编码前payRespBody----{"responseMsg":"鏀粯澶辫触","success":false}
Action.c(y): 转换编码后payRespBody----{"responseMsg":"支付失败","success":false}

0 0