LoadRunner HTTP+Json 接口性能測試

接口的請求參數和返回結果均是JSON字符串,請求能夠用POST或者GET方法。先說GET方法:web

1、GET方法測試json

  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 ;


2、POST方法測試數組

    post方法有兩種:web_submit_date和web_custom_request函數,web_submit_date不支持json串:服務器

   腳本能夠自行編碼,也能夠:app

  1. Insert - New step -選擇Custom Request - web_custom_request
  2. 填入相應參數
  3. 生成腳本,並修改以下(參數中的引號"前須要加斜槓\轉譯)

    Action()函數

    {post

    web_submit_data("login",
    "Action=http://xxx/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;charset=UTF-8",            //指定請求頭的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_convert_string_encoding(lr_eval_string("{
    PlateNumNond
    }"), 「utf-8」,LR_ENC_SYSTEM_LOCALE,"BM");
    lr_error_message(lr_eval_string("{BM}"));
    }
    說明:在LR中參數化標誌是{},接口參數body裏面也是{},因此在body裏面參數化的時候用<>代替,
    設置方法:Tool - General Options - Parameterization 中將Parameter Braces 改成<>便可
    post請求參數有三種格式:1.application/x-www-form-urlencoded 鍵值對;2.multipart/form-data 表單;
    三、application/json Json串,根據本身的須要修改就EncType值能夠;
    鍵值對和表單寫法一致:
    1.  ITEMDATA,  
    2.         "Name=Name1", "Value=Value1", ENDITEM,  
    3.         "Name=Name2", "Value=Value2", ENDITEM,  
    4.         LAST);  
      再來認識下lr_convert_string_encoding
      //響應中文亂碼解決
      int lr_convert_string_encoding( const char *sourceString, const char *fromEncoding,const char *toEncoding, const char *paramName);

      參數解釋:編碼

      sourceString:被轉換的源字符串。url

      fromEncoding:轉換前的字符編碼。

      toEncoding:要轉換成爲的字符編碼。

      paramName:轉換後的目標字符串。

      常量和值得關係:

      Constant

      Value

      LR_ENC_SYSTEM_LOCALE

      NULL

      LR_ENC_UTF8

      "utf-8"

      LR_ENC_UNICODE

      "ucs-2"

3、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:替換整個頁面。
相關文章
相關標籤/搜索