LoadRunner系列之—-01 接口压力测试脚本

 LoadRunner中一般用如下函数进行接口测试:

<一>. http或soap协议下的get请求接口,样例如下:

web_url("integrated_query.jsp", 
"URL=http://{UrlAddress}/pcis/policy/query/integrated_query.jsp?CASOPDPT=00&random=1483694327031", 
"Resource=0", 
"RecContentType=text/html", 
"Referer=http://{UrlAddress}/pcis/core/left.jsp?opCde=015", 
"Snapshot=t8.inf", 
"Mode=HTML", 
EXTRARES, 
LAST);

<二>.. http或soap协议下的post请求接口,用web_submit_data函数,web_custom_request函数

1. 有时需用到(web_reg_save_param)或(web_reg_find)函数,来判断接口请求的结果

2. 接口中有要求添加特殊的信息头,则可用函数(web_add_auto_header)

【方法一】:使用web_submit_data()

web_submit_data("insert", 
  "Action=http://116.211.23.123/SNS/Publish.htm ",       
  "Method=POST",       
  "Referer=http://116.211.23.123/SNS/Publish.htm ", 
   "Mode=HTML",       
ITEMDATA, 
   "Name= SNSID ","Value=6601",ENDITEM,       
  "Name= UserID ","Value=123",ENDITEM,       
  "Name= CommentsTypeID ","Value=1",ENDITEM,       
  "Name= CommentsID ","Value=456",ENDITEM,        
  "Name= AuthorID","Value=789",ENDITEM, 
   "Name= CommentsContent ","Value=Just for testing",ENDITEM, 
LAST);

【方法二】:使用web_custom_request()

//更利于拼接参数
char str[1000]; 

strcpy(str,"SNSID=7999&UserID=1&CommentsTypeID=1&CommentsID=1&AuthorID=1&CommentsContent=1"); 
web_add_auto_header("channel_id","1");
web_custom_request("Publish", 
  "Url= http://116.211.23.123/SNS/Publish.htm", 
  "Method=POST", 
  "Referer=http://116.211.23.123/SNS/Publish.htm", 
  "Mode=HTTP", 
  str, 
  LAST);

方法一:适合一些xml结构的根元素下的子元素同处于根元素下面,且子元素数目较少的情况下;

方法二、如果xml结构比较复杂,比如说根元素下面有很多子元素,或者xml树结构分叉较多的时候

<三>.. webservices 接口

【方法一】 通过web_service_call函数,也就是导入wsdl文件或者URL的方式;

1). New Virtual user---选择 WebServices;

2). 在页面左上角,工具栏下,点击Manager Services,点击Import,录入wsdl的URL;

3). 点击Add Service Call,在New Web Service Call 页面中选择Service, Operation,其中需填写  BEGIN_ARGUMENTS、 END_ARGUMENTS、BEGIN_RESULT、END_RESULT。用的函数为web_service_call,

注意:

1). 请求地址可以在Manage Services 里面修改,如下图所示:

2)。 如果请求报文内容放在 CDATA中间,例如 其中包含【 <arg0><![CDATA[document]]></arg0>】,那么在web_service_call 函数中需要删除 <![CDATA[ ]]>部分

【方法二】 通过soap_request函数,通过导入xml文件来实现;

soap_request("StepName=SOAP Request",
"URL=http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx",
"SOAPEnvelope="
"<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://WebXml.com.cn/">"
"<soapenv:Header></soapenv:Header>"
"<soapenv:Body>"
"<web:getSupportCityString>"
"<web:theRegionCode>?</web:theRegionCode>"
"</web:getSupportCityString>"
"</soapenv:Body>"
"</soapenv:Envelope>",
"SOAPAction=",
"ResponseParam=response",
"Snapshot=t1320736948.inf",
LAST);

【方法三】 通过http协议来手写脚本来实现。

注意:

1).  在一个方法中的某个参数,如果太长,需要换行,则每行单独用双引号括起来;

2).  获取response 中结果

int i, NumOfValues;

char buf[64];

 

lr_output_message(lr_eval_string("{response}"));

 

NumOfValues= lr_xml_get_values("XML={response}", 
"ValueParam=OutputParam", 
"Query=/soap:Envelope/soap:Body/ns1:inquireResponse/return",//xml 位置
"SelectAll=yes", LAST);


for ( i = 0; i < NumOfValues; i++) { /* Print multiple values of OutputParam */

sprintf (buf, "Retrieved value %d : {OutputParam_%d}", i+1, i+1);
lr_output_message(lr_eval_string(buf));
}

小知识

.  LR脚本创建后,会默认每个Action为一个事务,这样会出现自己定义了一个事务,但是执行后会多出很多事务。这个设置需要根据具体脚本需求,来决定是否使用该项(本例子中,就不需要)。“Vuser”-->“运行时设置”-->“其它”中,“每个Action定义为一个事务(D)”该复选框控制。

原文地址:https://www.cnblogs.com/liuyitan/p/7345727.html