转自:http://lht821008.blog.163.com/blog/static/4164786201141134535464/
思考曾经做过的一个黄金交易系统,里面针对后端进行报文发送,报文均是采用的xml格式,只不过传输和接受前后需要进行加解密,现在想来如果不是由于加解密包采用的是java,那我们完全可以采用LR的原生态C函数和XML协议函数做到更为优雅的测试脚本,利用LR的xml函数对整个xml报文实现动态封转。
函数:lr_xml_get_values
Action()
{
char *xml_input=
"<love>"
"<name_man>LHT</name_man>"
"<name_women>PHOEBE</name_women>"
"</love>";
lr_save_string(xml_input,"xml_param");
lr_xml_get_values("XML={xml_param}",
"ValueParam=OutputParam1",
"Query=/love/name_man",
LAST);
lr_xml_get_values("XML={xml_param}",
"ValueParam=OutputParam2",
"Query=/love/name_women",
LAST);
lr_message("husband is:%s",lr_eval_string("{OutputParam1}"));
lr_message("wife is:%s",lr_eval_string("{OutputParam2}"));
return 0;
}
//output
husband is:LHT
wife is:PHOEBE
函数:lr_xml_set_values
Action1() {
char * xml_input =
"<acme_org>"
" <accounts_dept>"
"<employee>"
" <name>John Smith</name>"
"<cubicle>227</cubicle>"
"<extension>2145</extension>"
"</employee>"
"</accounts_dept>"
"<engineering_dept>"
"<employee>"
"<name>Sue Jones</name>"
"<extension>2375</extension>"
"</employee>"
"</engineering_dept>"
"</acme_org>";
int i,NumOfValues;
char buf[64];
lr_save_string(xml_input,"XML_Input_Param"); // Save input as parameter
lr_save_string("lht","ExtensionParam_1");
lr_save_string("phoebe","ExtensionParam_2");
lr_xml_set_values("XML={XML_Input_Param}",
"ResultParam=NewXmlParam",
"ValueParam=ExtensionParam",
"SelectAll=yes",
"Query=//extension",
LAST);
NumOfValues= lr_xml_get_values("XML={NewXmlParam}",
"ValueParam=OutputParam",LAST);
for ( i = 0; i < NumOfValues; i++) { /* Print the multiple values of MultiParam */
sprintf (buf,"Retrieved value %d : {OutputParam_%d}",i+1,i+1);
lr_output_message(lr_eval_string(buf));
}
lr_message("new xml is:%s",lr_eval_string("{NewXmlParam}"));
return 0;
}
//output:
Virtual User Script started
Starting action vuser_init.
Ending action vuser_init.
Running Vuser...
Starting iteration 1.
Starting action Action.
Action.c(11): "lr_xml_get_values" succeeded,1 match processed
Action.c(16): "lr_xml_get_values" succeeded,1 match processed
husband is:LHT
wife is:PHOEBE
Ending action Action.
Starting action Action1.
Action1.c(47): "lr_xml_set_values" succeeded,2 matches processed
Action1.c(60): "lr_xml_get_values" succeeded,2 matches processed
Action1.c(73): Retrieved value 1 : lht
Action1.c(73): Retrieved value 2 : phoebe
new xml is:<acme_org> <accounts_dept><employee> <name>John Smith</name><cubicle>227</cubicle><extension>lht</extension></employee></accounts_dept><engineering_dept><employee><name>Sue Jones</name><extension>phoebe</extension></employee></engineering_dept></acme_org> Ending action Action1. Ending iteration 1. Ending Vuser... Starting action vuser_end. Ending action vuser_end. Vuser Terminated.