1. 程式人生 > >使用web_submit_data()和web_custom_request()函式編寫HTTP介面測試指令碼實戰

使用web_submit_data()和web_custom_request()函式編寫HTTP介面測試指令碼實戰

最近在做HTTP協議介面壓測時,遇到一些編寫指令碼方面的問題,在這裡總結記錄下,以便以後溫習,也希望能幫助到和我有同樣困惑的朋友吧。

//實戰程式碼如下所示:Action()
{
   lr_start_transaction("事物");

// 斷言內容:processcode":"0000","processdes:"處理成功"

   web_reg_save_param("processcode",
                      "NotFound=warning",
                      "LB=processcode\":\"",
                      
"RB=\",\"processdes", "Ord=1", "Search=Body", LAST); web_reg_save_param("processdes", "NotFound=warning", "LB=processdes\":\"", "RB=\",\"checkresult", "Ord=1",
"Search=Body", LAST); //介面1:傳送指定key:value引數的json報文 web_submit_data("request", "Action=http://IP:port/sss/idCardCheck11", "Method=POST", "Mode=HTTP", "RecContentType=application/json", ITEMDATA,
"Name=msgbody", "Value={\"app_sign\":\"287cca63e9316c031\",\"businesscode\":\"05\"," "\"note\":\"\",\"brnchcd\":\"4001\",\"b64pic\":\"\",\"inputtype\":\"11\",\"teller\":\"2666\"," "\"idcardno\":\"139891119211X\",\"name\":\"user\",\"wthrespic\":\"1\",\"app_id\":null," "\"bankcode\":\"31100396\",\"timestamp\":\"1536118078193\"}", ENDITEM, LAST); //介面2:傳送json格式報文 web_custom_request("request", "Url=http://IP:port/sss/idCardCheck11", "Method=POST", "Mode=HTTP", "EncType=application/json", "RecContentType=application/json", "Body={\"app_sign\":\"287cca63e9316c031\",\"businesscode\":\"05\",\"note\":\"\",\"brnchcd\":\"4001\"," "\"b64pic\":\"\",\"inputtype\":\"11\",\"teller\":\"2666\",\"idcardno\":\"139891119211X\","\"name\":" "\"user\",\"wthrespic\":\"1\",\"app_id\":null,\"bankcode\":\"31100396\",\"timestamp\":\"1536118078193\"}", LAST); //響應報文key值和預期結果比較,如果等於0則請求處理成功 if(strcmp(lr_eval_string("{processcode}"),"0000") == 0){ lr_end_transaction("事物", LR_PASS); }else{ lr_end_transaction("事物", LR_FAIL); //事物失敗後列印processdes欄位的值,也就是錯誤資訊,但是LR打印出來的中文出現亂碼,所以這裡做了轉碼的操作 lr_convert_string_encoding(lr_eval_string("{processdes}"),LR_ENC_UTF8,LR_ENC_SYSTEM_LOCALE,"resultMsg"); //解碼後返回單筆核對請求處理成功 lr_error_message("錯誤資訊:%s",lr_eval_string("{resultMsg}")); } // lr_end_transaction("事物",LR_AUTO); return 0; }

以下是web_submit_data()函式 API示例程式碼:

從示例可以看到傳送的是name,value對,提交表單的形式,但實際Value值也可能存在json陣列,xml等形式。

  // web_submit_data() API示例
  web_submit_data("default.aspx",
                "Action=http://lazarus/flightnet/default.aspx",
                "Method=POST",
                "TargetFrame=",
                "RecContentType=text/html",
                "Referer=http://lazarus/flightnet/",
                "Snapshot=t7.inf",
                "Mode=HTML",
                ITEMDATA,
                "Name=grpType", "Value=radRoundtrip", ENDITEM,
                "Name=lstDepartingCity", "Value=DEN", ENDITEM,
                "Name=lstDestinationCity", "Value=LAX", ENDITEM,
                "Name=btnAvailableFlights", "Value=Next >", ENDITEM,
            LAST );

介面請求報文如下:

msgbody:{app_sign:287cca63e9316c031,businesscode:05,note:,brnchcd:4001,b64pic:,inputtype:11,teller:2666,idcardno:139891119211X,name:user,wthrespic:1,app_id:null,bankcode:31100396,timestamp:1536118078193}

問題描述:

往常使用LR編寫處理HTTP介面指令碼,慣用的函式是web自定義請求函式 web_custom_request(),使用介面測試工具postman請求成功後就直接編寫指令碼,嘗試了幾種組body的方法均返回請求方法格式出錯或者直接請求失敗,嘗試請求方法如下所示:

  web_custom_request("request",
                  "Url=http://10.148.0.161:1111/check/idCardCheck",
                  "Method=POST",
                  "Mode=HTTP",
                  "EncType=application/json",
                  "RecContentType=application/json",
                  "Body={msgbody:{\"app_sign\":\"287cca63e9316c031\",\"businesscode\":\"05\",\"note\":\"\",\"brnchcd\":\"4001\"," 
                  "\"b64pic\":\"\",\"inputtype\":\"11\",\"teller\":\"2666\",\"idcardno\":\"139891119211X\","\"name\":" 
                  "\"user\",\"wthrespic\":\"1\",\"app_id\":null,\"bankcode\":\"31100396\",\"timestamp\":\"1536118078193\"}}",
                   LAST);
      
      web_custom_request("request",
                  "Url=http://10.148.0.161:1111/check/idCardCheck",
                  "Method=POST",
                  "Mode=HTTP",
                  "EncType=application/json",
                  "RecContentType=application/json",
                  "Body={\"msgbody\":{\"app_sign\":\"287cca63e9316c031\",\"businesscode\":\"05\",\"note\":\"\",\"brnchcd\":\"4001\"," 
                  "\"b64pic\":\"\",\"inputtype\":\"11\",\"teller\":\"2666\",\"idcardno\":\"139891119211X\","\"name\":" 
                  "\"user\",\"wthrespic\":\"1\",\"app_id\":null,\"bankcode\":\"31100396\",\"timestamp\":\"1536118078193\"}}",
                   LAST);
      
       web_custom_request("request",
                  "Url=http://10.148.0.161:1111/check/idCardCheck",
                  "Method=POST",
                  "Mode=HTTP",
                  "EncType=application/json",
                  "RecContentType=application/json",
                  "Body=msgbody:{\"app_sign\":\"287cca63e9316c031\",\"businesscode\":\"05\",\"note\":\"\",\"brnchcd\":\"4001\"," 
                  "\"b64pic\":\"\",\"inputtype\":\"11\",\"teller\":\"2666\",\"idcardno\":\"139891119211X\","\"name\":" 
                  "\"user\",\"wthrespic\":\"1\",\"app_id\":null,\"bankcode\":\"31100396\",\"timestamp\":\"1536118078193\"}",
                   LAST);

結論:如果提供的報文是key:value格式的就使用web_submit_data()函式,反之可選擇使用web_custom_request()函式,如以上內容表述或總結後期會不斷完善。