7、LoadRunner回放脚本时,在浏览器显示的中文是乱码
解决办法(1):
首先设置Run-Time Settings – Browser – Browser Emulation – User-Agent
解决办法:
使用lr_convert_string_encoding函数来转换编码
下面是一个使用的例子:
lr_convert_string_encoding(lr_eval_string("{ReplyContents}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"ReplyMessage");
web_submit_data("postreply.aspx_2",
"Action=http://127.0.0.1/postreply.aspx?infloat=1&topicid=2&&inajax=1",
"Method=POST",
"EncType=multipart/form-data",
"RecContentType=text/xml",
"Referer=http://127.0.0.1/showtopic-2.aspx",
"Snapshot=t9.inf",
"Mode=HTML",
ITEMDATA,
"Name=iconid", "Value=0", ENDITEM,
"Name=title", "Value=", ENDITEM,
"Name=wysiwyg", "Value=0", ENDITEM,
"Name=checkbox", "Value=0", ENDITEM,
"Name=message", "Value=[localimg=180,112]1[/localimg]\r\n\r\n{ReplyMessage}", ENDITEM,
"Name=e_mediatyperadio", "Value=on", ENDITEM,
"Name=sl_attachdesc", "Value=", ENDITEM,
"Name=attachid", "Value=", ENDITEM,
"Name=attachdesc", "Value=", ENDITEM,
"Name=localid", "Value=", ENDITEM,
"Name=attachdesc", "Value=", ENDITEM,
"Name=localid", "Value=1", ENDITEM,
"Name=attachdesc", "Value=", ENDITEM,
"Name=localid", "Value=2", ENDITEM,
"Name=emailnotify", "Value=on", ENDITEM,
"Name=postreplynotice", "Value=on", ENDITEM,
"Name=postfile", "Value=", "File=Yes", ENDITEM,
"Name=postfile", "Value=D:\\\\图片收集\\\\It‘s about time.JPG", "File=Yes", ENDITEM,
"Name=postfile", "Value=", "File=Yes", ENDITEM,
"Name=uploadallowmax", "Value=10", ENDITEM,
"Name=uploadallowtype", "Value=jpg,gif", ENDITEM,
"Name=thumbwidth", "Value=300", ENDITEM,
"Name=thumbheight", "Value=250", ENDITEM,
"Name=noinsert", "Value=0", ENDITEM,
LAST);
实例2:
在LoadRunner中,为我们提供了一个字符串编码转换的函数 lr_convert_string_encoding ( const char *sourceString, const char *fromEncoding, const char *toEncoding, const char *paramName);
该函数有4个参数,含义如下:
sourceString:被转换的源字符串。
fromEncoding:转换前的字符编码。
toEncoding:要转换成为的字符编码。
paramName:转换后的目标字符串。
在本例中可以看到,我们需要把字符编码转换为UTF-8格式,因此用法如下: lr_convert_string_encoding("汽车",LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str"); 这样一来,就成功地完成了字符串的编码转换。此时我们就可以对"汽车"这个参数进行参数化,参数化的方法很简单,地球人都知道!于是最终的脚本编码看起来像这样: lr_convert_string_encoding("lr_eval_string("{name}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"str") 由于url不能传输汉字,所以程序要把汉字转换一下,lr怎么参数化呢 看下面的列子 web_url("0.00", "URL=http://192.168.0.15/xxx/xxx/xxx.aspx?HotelName=%e5%8c%97%e4%ba%ac%e5%8d%8e%e5%87%af%e5%ae%be%e9%a6%86", "Resource=0", "RecContentType=text/html", "Referer=http://192.168.0.15/xxx/xxx/xxx.aspx?hotelid=00101210&hotelname=%e5%8c%97%e4%ba%ac%e5%8d%8e%e5%87%af%e5%ae%be%e9%a6%86", "Snapshot=t41.inf", "Mode=HTTP", LAST); 注意黑体字,被转化了的汉字,使用如下函数 定义两个变量 char temp; char hotelname[4096]; lr_convert_string_encoding(lr_eval_string("{hotel_name}"),LR_ENC_SYSTEM_LOCALE, LR_ENC_UTF8 ,"temp"); 转化完的字符放到了temp里, strcat(hotelname,lr_eval_string ( "{temp}" )); web_url("0.00", "URL=http://192.168.0.15/xxx/xxx/xxx.aspx?HotelName={hotelname}", "Resource=0", "RecContentType=text/html", "Referer=http://192.168.0.15/xxx/xxx/xxx.aspx?hotelid=00101210&hotelname=%e5%8c%97%e4%ba%ac%e5%8d%8e%e5%87%af%e5%ae%be%e9%a6%86", "Snapshot=t41.inf", "Mode=HTTP", LAST);
在录制选项中,“HTTP属性-高级”中选择支持字符集,UTF-8 此时如果在web_submit_data中有中文,在脚本查看中能正确显示为中文,而不是乱码。 此时web_url中仍会显示为url(注意不是乱码)编码。
其实url编码就是一个字符ascii码的十六进制 如 “胡”的ascii码是-17670,十六进制是BAFA,url编码是“%BA%FA” UTF-8是一种可变长度字符编码,用在网页上可以同一页面显示中文简体繁体及其它语言(如英文,日文,韩文),如“中国”:中国 “HTTP属性-高级”中选择支持字符集选择UTF-8的意义在于将UTF-8 符转换为您所在语言环境的计算机编码,因而能正确显示在屏幕上
常用函数解释:
lr_output_message 输出消息
lr_output_message ("hello,world");
strcpy 复制一个字符串到另外一个字符串
char tmp[50];
strcpy(tmp,"hello,world");
lr_output_message ("%s",tmp);
注意,char tmp[50]变量定义要放在action的最开始,否则会报错
web_url("search.aspx", //"URL=http://192.168.20.60:8080/zk/search.aspx?key=U%3D%E4%B8%AD", "URL=http://192.168.20.60:8080/zk/search.aspx?key=U%3D{skey}", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t86.inf", "Mode=HTML", EXTRARES, "Url=../images/search-left-bg.gif", "Referer=http://192.168.20.60:8080/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD%E9%87%8E%E7%94%9F%E8%B5%84%E6%BA%90", ENDITEM, "Url=../images/bg.png", "Referer=http://192.168.20.60:8080/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD%E9%87%8E%E7%94%9F%E8%B5%84%E6%BA%90", ENDITEM, "Url=../images/cluster-pic.png", "Referer=http://192.168.20.60:8080/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD%E9%87%8E%E7%94%9F%E8%B5%84%E6%BA%90", ENDITEM, "Url=../images/search-select-current.png", "Referer=http://192.168.20.60:8080/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD%E9%87%8E%E7%94%9F%E8%B5%84%E6%BA%90", ENDITEM, "Url=../images/search-select-bg.png", "Referer=http://192.168.20.60:8080/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD%E9%87%8E%E7%94%9F%E8%B5%84%E6%BA%90", ENDITEM,
HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器籍此可以获得一些信息用于处理
因此这部分的参数服务器可能是不会处理的,比如次例中,gif和png都是静态文件,因此后免的Referer中的key不用做参数替换
本例中,需要对key=U%3D%E4%B8%AD 参数化,其中U%3D表示U=,这部分不变,需要参数话的是%E4%B8%AD,即“中”,参数名为skey
上例中,由于需要参数化%E4%B8%AD的是一个url编码,一般来说参数化文件是一个汉字的列表,所以需要如下面的处理
Action()
{
//此时skey是一个汉字列表的参数,把它转换为utf-8,存入UnicodeString参数中
lr_convert_string_encoding(lr_eval_string("{skey}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"UnicodeString");
//把UnicodeString参数的值又覆盖回skey参数,前2步相当于完成了skey参数的汉字到utf-8的转换
lr_save_string (lr_eval_string("{UnicodeString}"),"skey" );
//这一步把skey参数从utf-8又转换到url编码
web_convert_param("skey", "SourceEncoding=PLAIN", "TargetEncoding=URL", LAST );
lr_output_message("Url编码 = %s",lr_eval_string("{skey}"));
web_url("search.aspx",
"URL=http://qikan.cqvip.com/zk/search.aspx?key=U%3D{skey}",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t39.inf",
"Mode=HTML",
EXTRARES,
"Url=../images/bg.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
"Url=../images/search-left-bg.gif", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
"Url=../images/search-select-bg.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
"Url=../images/search-select-current.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
"Url=../images/cluster-name-bg.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
"Url=../images/cluster-group-show.gif", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
"Url=../images/close-op.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
"Url=../images/cluster-pic.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
"Url=../images/type-ico-id.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
"Url=../ajax/stateview.ashx?info=Ozs7MDs7L33NlYaC5&_t=1415241852", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM,
LAST);
return 0;
}
Action() { lr_convert_string_encoding(lr_eval_string("{skey}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"UnicodeString"); //此时skey是一个汉字列表的参数,把它转换为utf-8,存入UnicodeString参数中 lr_save_string (lr_eval_string("{UnicodeString}"),"skey" ); //把UnicodeString参数的值又覆盖回skey参数,前2步相当于完成了skey参数的汉字到utf-8的转换 web_convert_param("skey", "SourceEncoding=PLAIN", "TargetEncoding=URL", LAST ); //这一步把skey参数从utf-8又转换到url编码 lr_output_message("Url编码 = %s",lr_eval_string("{skey}")); web_url("search.aspx", "URL=http://qikan.cqvip.com/zk/search.aspx?key=U%3D{skey}", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t39.inf", "Mode=HTML", EXTRARES, "Url=../images/bg.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, "Url=../images/search-left-bg.gif", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, "Url=../images/search-select-bg.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, "Url=../images/search-select-current.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, "Url=../images/cluster-name-bg.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, "Url=../images/cluster-group-show.gif", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, "Url=../images/close-op.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, "Url=../images/cluster-pic.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, "Url=../images/type-ico-id.png", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, "Url=../ajax/stateview.ashx?info=Ozs7MDs7L3prL3NlYXJjaC5hc3B4&_t=1415241852916", "Referer=http://qikan.cqvip.com/zk/search.aspx?key=U%3D%E4%B8%AD%E5%9B%BD", ENDITEM, LAST); return 0; }
lr_convert_string_encoding 字符串编码转换的函数
int lr_convert_string_encoding ( const char *sourceString, const char *fromEncoding, const char *toEncoding, const char *paramName);
该函数有4个参数,含义如下:
sourceString:被转换的源字符串。
fromEncoding:转换前的字符编码。
toEncoding:要转换成为的字符编码。
paramName:转换后的目标字符串。
例如:
lr_convert_string_encoding("中",LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"UnicodeString");
//把汉字"中"转化为utf-8的格式,输出为名字UnicodeString参数,注意,这是一个lr参数,而不是C变量
lr_save_string (lr_eval_string("{UnicodeString}"),"skey" );
//1、其中skey为前例web_url中被参数化的参数名,由于这是一个参数,所以需要用lr_save_string来为参数赋值;不能直接用=号赋值
2、由于UnicodeString是一个lr参数,不能直接作为字符串来赋值,需要用lr_eval_string来取得参数的值
lr_eval_string
lr_eval_string("{参数名}")
取得参数的数值。可取得已注册参数或DataFile类型参数的数值 eval就是evaluation的缩写
注意和lr_save_string的入参形式的区别,这里的参数名需要用{}括起来
lr_save_string
lr_save_string(“字符串变量”,"参数名")
将字符变量里的值传递给指定参数。通过该函数来改变DataFile类型参数的数值。该函数还会自动去掉字符串的“\x00”后缀
web_convert_param 可用于文本的url编解码
int web_convert_param( const char *ParamName, [char *SourceString] char *SourceEncoding, char *TargetEncoding, LAST );
ParamName 需要输入一个lr参数名,参数名对应的参数值值会被转换编码,然后覆盖原参数值
SourceString 需要转换的文本内容,注意该参数是可选项,如果传了,则会把该值转换编码后传入第一个参数;如果不传,则第一个参数本身对应的参数值会转换编码后覆盖原参数值
SourceEncoding 源数据的编码格式,有3个选项"SourceEncoding=HTML"、"SourceEncoding=URL"、"SourceEncoding=PLAIN"
TargetEncoding 源数据的编码格式,有3个选项"SourceEncoding=HTML"、"SourceEncoding=URL"、"SourceEncoding=PLAIN"
LAST 参数结束的标志
例如:
web_convert_param("skey", "SourceEncoding=PLAIN", "TargetEncoding=URL", LAST ); //其中skey是一个lr参数名
该例子把参数skey中的原来的值url编码后,覆盖了原skey参数
lr_output_message("Url编码 = %s",lr_eval_string("{skey}"));
HTML和PLAIN的区别,在http的相应头信息中,Content-Type字段可能有text/html和text/plain两种取值:
如果是html,浏览器在获取到这种文本时会自动调用html的解析器对文件进行相应的处理
如果是plain,意思是纯文本的形式,浏览器在获取到这种文件时并不会对其进行处理
项目中运用:
input() { lr_convert_string_encoding(lr_eval_string("{gardenname}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"UnicodeString"); //此时gardenname是一个汉字列表的参数,把它转换为utf-8,存入UnicodeString参数中 lr_save_string (lr_eval_string("{UnicodeString}"),"gardenname" ); //把UnicodeString参数的值又覆盖回gardenname参数,前2步相当于完成了gardenname参数的汉字到utf-8的转换 web_convert_param("gardenname", "SourceEncoding=PLAIN", "TargetEncoding=URL", LAST ); //这一步把gardenname参数从utf-8又转换到url编码 lr_output_message("Url编码=%s",lr_eval_string("{gardenname}")); lr_convert_string_encoding(lr_eval_string("{gardenid}"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"UnicodeString"); //此时gardenid是一个汉字列表的参数,把它转换为utf-8,存入UnicodeString参数中 lr_save_string (lr_eval_string("{UnicodeString}"),"gardenid" ); //把UnicodeString参数的值又覆盖回gardenid参数,前2步相当于完成了gardenid参数的汉字到utf-8的转换 web_convert_param("gardenid", "SourceEncoding=PLAIN", "TargetEncoding=URL", LAST ); //这一步把gardenid参数从utf-8又转换到url编码 lr_output_message("Url编码=%s",lr_eval_string("{gardenid}")); web_url("sort-asc.png", "URL=http://192.168.0.42/resource/Erp_Affiliate/plugin/ingrid/images/sort-asc.png", "Resource=1", "RecContentType=image/png", "Referer=http://0755.testqfang242.com/qfang-broker/house/search", "Snapshot=t24.inf", LAST); web_custom_request("queryGarden", "URL=http://0755.testqfang242.com/qfang-broker/house/search/dictionary/queryGarden", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=http://0755.testqfang242.com/qfang-broker/house/search", "Snapshot=t25.inf", "Mode=HTML", "Body=key={gardenname}&orgid={orgid}&longNumber={longnumber}&sqlId=com.qfang.dao.dictionary.GardenDao.getGardenByParams&type=ztree", LAST); web_custom_request("queryGarden_2", "URL=http://0755.testqfang242.com/qfang-broker/house/search/dictionary/queryGarden", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=http://0755.testqfang242.com/qfang-broker/house/search", "Snapshot=t26.inf", "Mode=HTML", "Body=key={gardenname}&orgid={orgid}&longNumber={longnumber}&sqlId=com.qfang.dao.dictionary.GardenDao.getGardenByParams&type=ztree", EXTRARES, LAST); web_custom_request("getBuildingsByGardens", "URL=http://0755.testqfang242.com/qfang-broker/house/search/getBuildingsByGardens", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=http://0755.testqfang242.com/qfang-broker/house/search", "Snapshot=t26.inf", "Mode=HTML", "Body=gardenIds=‘{gardenid}‘", LAST); lr_output_message("success:用户登录成功,楼盘id:%s",lr_eval_string("{gardenid}")); lr_output_message("success:用户登录成功,楼盘名称:%s",lr_eval_string("{gardenname}")); return 0; }
从数据库获取的原始变量值,有中文,需要转换
经常碰到在脚本中有输入值是中文,或者返回值中的中文是乱码的问题,今天就抽空把这个问题解决了。
主要使用到的方法是LR自带的lr_convert_string_encoding函数。废话少说,下面是小实验。看懂这个,就知道怎么处理中文乱码问题了。
1、输入值中有中文
Action() { //输入值中有中文 char tmp[50]; lr_convert_string_encoding( lr_eval_string("环球影院"), LR_ENC_SYSTEM_LOCALE, LR_ENC_UTF8, "str" ); lr_log_message("11str is %s",lr_eval_string("{str}")); /* 直接转换过来的内容后面,多了\x00,要通过strcpy函数复制到别的变量后再用,才不受\x00的影响。 */ strcpy(tmp,lr_eval_string("{str}")); //lr_log_message("str is %s",tmp); lr_save_string(tmp,"sorvalue"); lr_log_message("str is %s",lr_eval_string("{sorvalue}")); return 0; }
2、返回值中有中文
Action() { // 返回值中有中文 lr_convert_string_encoding( lr_eval_string("鐧戒簯鏈哄満"), LR_ENC_UTF8, LR_ENC_SYSTEM_LOCALE, "strcn" ); lr_log_message("乱码对应的中文是 %s",lr_eval_string("{strcn}")); return 0; }