Loadrunner 基于webservice协议的接口压力测试(脚本实例)
接口功能如下:请求接口,报文只有一个参数为证件号码;返回报文中,有证件号码是否能查到对应数据,查到几条数据。
思路:请求webservice接口,将测试数据,测试结果拼装为html文件并保存
---------------------------------------------------------------------------------
vuser_init代码如下: //定义一个log文件,记录请求参数,期望结果,返回参数,测试结果和查询到的条数 //定义结果文件变量 long file_stream; //定义测试结果变量 char V_Result[1024]; vuser_init() { //取得文件名种子-----系统时间 lr_save_datetime("%Y%m%d%H%M%S",DATE_NOW,"now_date"); //拼接测试结果文件名称 strcpy(V_Result,"D:\ResultFile\"); strcat(V_Result,lr_eval_string("{PolValidate}")); strcat(V_Result,"_"); strcat(V_Result,lr_eval_string("{now_date}")); strcat(V_Result,".html"); //生成并打开测试结果文件 if ((file_stream = fopen(V_Result, "w+")) == NULL) { lr_error_message ("创建 %s 文件失败", V_Result); return -1; } //写入测试文件头部html信息 strcpy(V_Result,"<html><table border='1'><tr><td>接口名称</td><td>证件号码</td><td>预期值</td><td>实际值</td><td>结果</td><td>保单数量</td></tr>"); fputs(V_Result,file_stream); return 0; } Action代码如下: Action() { //存储测试结果状态 char V_testStatus[1024]; //定义预期值与实际值的比较结果 int result; //取得登录名 char *V_UserName=lr_eval_string("{UserID}"); //获取服务器还回的状态state //Correlation comment - Do not change!Original value='00' Name ='errorCode' //errorCode":"00","policyLevel // web_reg_save_param_ex( "ParamName=errorCode", "LB=errorCode":"", "RB="", "Notfound=warning", SEARCH_FILTERS, "Scope=All", "RequestUrl=*/PolicyValidateTrueByIdService*", LAST); //保单数量 //Correlation comment - Do not change!Original value='2' Name ='CorrelationParameter_1' //policyCount":2,"errorCode web_reg_save_param_ex( "ParamName=policyCount", "LB=policyCount":", "RB=,"errorCode", "Notfound=warning", SEARCH_FILTERS, "Scope=All", "RequestUrl=*/PolicyValidateTrueByIdService*", LAST); lr_think_time(2); lr_start_transaction("保单验真"); web_custom_request("PolicyValidateTrueByIdService", "URL=http://{UrlAddress}/pcis/services/PolicyValidateTrueByIdService?wsdl", "Method=POST", "TargetFrame=", "Resource=0", "RecContentType=text/xml", "Referer=", "Snapshot=t2.inf", "Mode=HTML", "EncType=", "Body=<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:out="http://outer.interfaces.webservice.pcis.isoftstone.com/"> " " <soapenv:Header/> " " <soapenv:Body> " " <out:validateById> " " <!--Optional:--> " " <arg0><![CDATA[ " "{"reqData": " "{ " ""insuredFlag": "03", " ""idType": "0101", " ""customerId": "{UserID}", " ""minAmt": "1000", " ""lossDay": "7", " ""serialNo": "999999201702180302110001" " "} " "} " "]]></arg0> " " </out:validateById> " " </soapenv:Body> " "</soapenv:Envelope>", LAST); lr_end_transaction("保单验真", LR_AUTO); //比较预期值和实际值是否相等 result=strcmp( lr_eval_string("00"), lr_eval_string("{errorCode}") ); if(result==0){ strcpy( V_testStatus, " bgcolor="#00FF00">" ); strcat( V_testStatus, "PASS" ); }else{ strcpy( V_testStatus, " bgcolor="#FF0000">" ); strcat( V_testStatus, "FAIL" ); } strcpy( V_Result,"<tr>" ); //写入接口名称 strcat( V_Result,"<td id='interFaceName'>" ); strcat( V_Result,lr_eval_string("PolicyValidateTrue") ); strcat( V_Result, "</td>" ); //写入测试参数 strcat( V_Result, "<td id='uname'>" ); strcat( V_Result, V_UserName ); strcat( V_Result, "</td>" ); //写入预期结果 strcat( V_Result, "<td id='expected'>" ); strcat( V_Result, lr_eval_string("00") ); strcat( V_Result, "</td>" ); //写入实际结果 strcat( V_Result, "<td id='reality'>" ); strcat( V_Result, lr_eval_string("{errorCode}") ); strcat( V_Result, "</td>" ); //写入测试状态 strcat( V_Result, "<td id='status'" ); strcat( V_Result, V_testStatus ); strcat( V_Result, "</td>" ); //写入保单数量 strcat( V_Result, "<td id='status'>" ); strcat( V_Result, lr_eval_string("{policyCount}") ); strcat( V_Result, "</td></tr>" ); fputs( V_Result, file_stream ); return 0; } vuser_end代码如下: vuser_end() { //结束并关闭文件 strcpy(V_Result,"</table></html>"); fputs(V_Result,file_stream); if (fclose(file_stream)) lr_error_message ("文件 %s 关闭出错", V_Result); return 0; }
参考资料:http://blog.csdn.net/jiang1986829/article/details/48368829