这是一个HTTP接口测试中经常会碰到的问题,目前的服务器采用的都是UTF-8编码方式,而我们的客户机Windows系统一般默认采用的编码方式是GBK,这正是我们采用录制方式的时候会发现许多中文乱码的原因。
Loadrunner录制的时候可以通过在Virtual User Gen的Tools->Recoding Options -> Advanced -> Support charset -> UTF-8的设置规避(其实也只是部分规避),下面我们讨论在手写测试脚本时如何解决UTF-8转码的问题。
实践一:在脚本中直接采用中文明文进行请求
web_custom_request("web_custom_request",
"URL=http://172.16.4.191/list?id=环球影院", "Method=GET", "TargetFrame=", "Resource=0", "Referer=", "Body=", LAST);结果:服务端返回404错误,找不到相应的资源id,明显服务端不能正确响应非UTF8编码方式的请求。实践二:
为解决这个问题,最关键的是要把本地GBK编码的汉字转换成UTF-8编码格式的信息,为此我们引进loadrunner自带的编码函数lr_convert_string_encoding
lr_convert_string_encoding
Return Values | Parameterization |
Converts a string to a different encoding.
C Language
intlr_convert_string_encoding( const char *sourceString, const char *fromEncoding, const char *toEncoding, const char *paramName);
Example See Also
sourceString | The string to convert |
fromEncoding | The encoding of the sourceString |
toEncoding | The encoding to convert of the string saved in parameter paramName |
paramName | The name of the parameter in which the destination string will be saved |
lr_convert_string_encodingconverts a string encoding between the following encodings: System locale, Unicode, and UTF-8.The function saves the result string, including its terminating NULL, in the parameterparamName.
lr_convert_string_encodingis added manually to a script. when needed. It is not recorded.
Possible values for 'fromEncoding' and 'toEncoding' :
Constant | Value |
---|---|
LR_ENC_SYSTEM_LOCALE | NULL |
LR_ENC_UTF8 | "utf-8" |
LR_ENC_UNICODE | "ucs-2" |