LoadRunner HTTP+Json 接口性能测试

接口的请求参数和返回结果均是JSON字符串,请求可以用POST或者GET方法。先说GET方法:

一、GET方法测试

  1. Insert - New step -选择Custom Request - web_url
  2. 填写参数;
  3. 生成脚本,进行脚本相应修改;
    Action()
    {
        //添加集合点
        lr_rendezvous("jihedian");
        lr_start_transaction("getTop10");
        //插入检查点,检查返回值是否包含testName
        web_reg_find(
           "Search=Body",
           "Text=testName", 
            LAST ); 
       //发送get请求
        web_url("www.xxx.com",     
            "URL=http://192.168.3.33:9200/_search?{\"query\":{\"bool\":{\"must\":[{\"term\":{\"plateNumNond\":\"<NewParam>\"}}]}", 
            "TargetFrame=",     
            "Resource=0",     
            "RecContentType=application/json",    
            "Snapshot=t1.inf",     
            "Mode=HTML",     
            LAST ); 
        lr_end_transaction("getTop10", LR_AUTO);
       //打印本次参数
        lr_output_message( "the platenum is #%s", lr_eval_string( "{NewParam}" ) ); 
        return 0;
    }
    说明:查看服务器返回的结果需在Vuser-Runtime-settings的log选项下,勾选Enable-logging、Extended log、Data returned by server ;


二、POST方法测试

    post方法有两种:web_submit_date和web_custom_request函数,web_submit_date不支持json串:

   脚本可以自行编码,也可以:

  1. Insert - New step -选择Custom Request - web_custom_request
  2. 填入相应参数
  3. 生成脚本,并修改如下(参数中的引号"前需要加斜杠\转译)

    Action()

    {web_submit_data("login",
    "Action=http://{IP}/MiddleWare/mem/login?",
    "Method=POST",
    "TargetFrame=",
    "RecContentType=application/json",
    "Snapshot=t5.inf",
    "Mode=HTTP",
    ITEMDATA,
    "Name=params","Value={\"param\":{\"userAccount\":\"{UserName}\",\"passWord\":\"e10adc3\",\"device\":\"IOS\"},\"version\":\"v1.0\",\"loginLog\":\"{testTimeParam}\"}",ENDITEM,
    LAST );
    或者:

    lr_start_transaction("querybypost");
        //插入检查点,检查返回值是否包含t_query_data
        web_reg_find(
           "Text=max_score", 
            LAST ); 
        web_custom_request("querybypost",                           //VuGen中树形视图中显示的名称
            "Url=http://192.168.3.33:9200/_search",   //请求url
            "Method=POST",
            "Resource=0",                               
            "Mode=HTTP", //请求方式
            "Referer=",        
            "EncType=application/json",                   //指定响应头的Content-Type,这里是JSON
            "RecContentType=application/json",            //指定请求头的Content-Type,这里是JSON
            "Body={\"query\":{\"bool\":{\"must\":[{\"term\":{\"plateNumNond\":\"<PlateNumNond>\"}}],\"must_not\":[],\"should\":[]}},\"from\":0}",    //body的内容
             LAST);
        lr_end_transaction("querybypost", LR_AUTO);
        lr_output_message( "PlateNumNond on iteration #%s", lr_eval_string( "<PlateNumNond>" ) );
    }
    说明:在LR中参数化标志是{},接口参数body里面也是{},所以在body里面参数化的时候用<>代替,
    设置方法:Tool - General Options - Parameterization 中将Parameter Braces 改为<>即可

三、web_custom_request和web_submit_data区别  

  • web_custom_request方法可以发送POST和GET类型的请求;
  • web_submit_data只能发送POST类型的请求;
  • 所有web_submit_data方法发送的请求都可以使用web_custom_request来实现
  • web_custom_request可以实现web_submit_data无法实现的请求,比如“查询所有邮件并删除”这个案例中,查询时我们使用关联把所有邮件对应的标识抓取成一个数组,如果使用web_submit_data来完成这个删除的请求,需要很多个web_submit_data请求才能完成,但使用web_custom_request就可以通过一个请求完成,方法是自己写代码拼一个eb_custom_request 
  • 方法POST请求的Body值。
    这两种实现请求的方法还有一个需要注意的地方就是web_custom_request中body中的属性值如果包含一些特殊字符,必须通过URL编码,否则Web服务器会返回500错误,一个典型的例子是如果Body中包含ViewState,ViewState中常常有“=”之类的特殊字符,此时必须通过URL编码,LoadRuner中提供了一个这样的编码转换函数:
      web_convert_param(“vs1″,
      “SourceEncoding=HTML”,“TargetEncoding=URL”, LAST);  
      3.web_custom_request函数详解
       A.语法:
      intweb_custom_request( const char
      *RequestName, ,
      [EXTRARES, ,] LAST );
       B.返回值:返回LR_PASS(0)代表成功,LR_FAIL(1)代表失败。
       C.参数:
      (1)RequestName:步骤的名称,VuGen中树形视图中显示的名称。
      (2)List of Attribute:属性列表,支持的属性有以下几种:
      a.URL:页面地址。
      b.Method:页面的提交方式,POST或GET。
      c.EncType:编码类型。
      d.TargetFrame:当前链接或资源所在Frame的名称。
      除了Frame的名字,还可以指定下面的参数:
      _BLANK:打开一个空窗口。
      _PARENT:把最新更改过的的Frame替换为它的上级。
      _SELF:替换最新更改过的的Frame。
      _TOP:替换整个页面。

猜你喜欢

转载自www.cnblogs.com/camilla/p/10757947.html
今日推荐