lr中web_custom_request、web_submit_data函數


 

web_custom_request 

LR幫助文檔的定義

Allows you to create a custom HTTP request with any method supported by HTTP. 
Allows you to create a custom HTTP request using any method or body. By default, VuGen generates this function only for requests that could not be interpreted with other web functions.

 int web_custom_request( const char *RequestName, <List of Attributes>,[EXTRARES, <List of Resource Attributes>,] LAST ); 

web_custom_request()函數是一個可以用於自定義http請求的“萬能”函數。頁面的提交方式,POST、GET、PATCH、PUT等。

 

數據提交形式:

A、post+JSON格式參數:

1)參數都寫在一行: "Body={"arg1":int_value1,"arg2":"str_value2",…, "argN":"valueN"}",

2)參數不都在同一行

"Body={"arg1":value1,"

        ""arg2":"str_value2","

        ""arg3":"str_value3","

         ……

        ""argN":"valueN"}",

 

B、post+非JSON格式參數:

 “Body=屬性名稱=屬性值&屬性名稱=屬性值&……”

 

示例:

web_custom_request(
……
"BodyUnicode=REPRICE"
"BodyBinary=\\x08\\x00\\xCC\\x02\\x00\\x00"
"Body=.\r\n"
"-dxjjtbw/(.tp?eg:ch/6--\r\n",
LAST);

不同的應用中,請求體分別通過Body、BodyBinary或者BodyUnicode參數來傳遞。請求體可以只使用其中一個參數,也可以使用一連串的分開的參數組成多請求體。

在上面的代碼中,使用了3個參數來划分請求體,一個是Unicode,一個是二進制,最后一個是常規的字符串。最終的請求體是這3個參數按照在函數中的順序連接起來的值。還有一個很少用到的參數,Binary。它也能描述二進制請求體,但只允許函數中只有一個請求體參數。所有的請求體都是ASCII字符,以null結束。

它也可以使用二進制字符串。 

char *abc= .../* a pointer to the raw data */ 
web_custom_request("StepName", 
"URL=http://some.url ", 
"Method=POST", 
RAW_BODY_START, 
"abc", 
3, 
RAW_BODY_END, 
LAST); 

 

   當使用web_custom_request()函數來發送http的get和post請求時,如果未使用web_add_header相關函數來自定義添加頭部,請求中帶有默認的http請求頭部字段

web_url("file.html", "URL=http://lazarus/html/forms/file.html", "TargetFrame=_TOP", LAST ); 

web_add_header("Content–Type","multipart/form–data; boundary=–––––––––––––––––––––––––––292742461228954"); 

web_custom_request("post_query.exe", "Method=POST", 
    "URL=http://lazarus/cgi–bin/post_query.exe", 
    "Body=–––––––––––––––––––––––––––––292742461228954\r\nContent–Disposition: form–data; name=\"entry\"\r\n\r\nText\r\n–––––––––––––––––––––––––––––292742461228954\r\nContent–Disposition: f–––––––––––292742461228954––\r\n", 
    "TargetFrame=", 
    LAST ); 

如需要修改默認的頭部字段或增加其他頭部字段就在web_custom_request()函數的前面使用web_add_header()函數來添加

如果要減少某個頭部字段或全部自動添加的頭部字段就在web_custom_request()函數的前面使用web_remove_auto_header()和web_revert_auto_header(),頭部字段所有請求中公共的一些頭部可以放在web_add_auto_header()函數中,配合web_add_header系列函數來完成自定義的業務腳本。 

 

轉自Loadrunner 關聯 web_custom_request綜合實例 的一個實例。

Loadrunner 關聯web_custom_request,針對自帶的訂票系統的一個綜合實例,相信看了本文大家對學習loadrunner腳本會有很大的幫助.
本實例要解決的問題:
(1)動態刪除Loadrunner訂票系統的一條訂單;
(2)動態判斷表單訂單條目,執行取消第一條訂單;
(3)Loadrunner關聯 
web_custom_request以及循環語句的應用。
代碼不足之處:變量定義太多,希望和大家共同交流。
以下為我的腳本的源碼:
------------------------------------------------------
#include "web_api.h"
 
Action()
{
int i,k;
char form[1024];
char temp[1024];
char 
tmp[1024];
char tp[1024];
char tp1[1024];
char 
tp2[1024];
 
 web_url("MercuryWebTours",
     "URL=http://192.168.8.9/MercuryWebTours/",
     "Resource=0",
     "RecContentType=text/html",
     "Referer=",
     "Snapshot=t1.inf",
     "Mode=HTML",
     LAST);
 lr_think_time(10);
 web_submit_form("login.pl",
     "Snapshot=t2.inf",ITEMDATA,
     "Name=username","Value=zhangming", ENDITEM,
     "Name=password","Value=666666", ENDITEM,
     "Name=login.x", "Value=50",ENDITEM,
     "Name=login.y", "Value=11",ENDITEM,
     LAST);
 lr_think_time(4);
  
 web_reg_save_param("flightID",
     "LB=INPUT TYPE=\"hidden\" NAME=\"flightID\"VALUE=\"",
     "RB=\"",
     "ORD=ALL",
     "search=body",
     LAST);
 
 web_image("Itinerary Button",
     "Alt=Itinerary Button",
     "Snapshot=t3.inf",
     LAST);
      
strcpy(form,"Body=1=on");
i=atoi(lr_eval_string("{flightID_count}"));
for(k=1;k<=i;k++)
 {
sprintf(temp,"{flightID_%d}",k);
 
strcpy(tmp,lr_eval_string(temp));//取出flightID的值,並把值傳給tmp
 
sprintf(tp,"&flightID=%s",tmp);
 
strcat(form,tp);
}
 
for(k=1;k<=i;k++)
 {
sprintf(tp2,"&.cgifields=%d",k);
strcat(tp1,tp2);
 }
strcat(form,tp1);
strcat(form,"&removeFlights.x=137&removeFlights.y=13");
 
lr_output_message("form 
的值為=%s",form);
web_custom_request("itinerary.pl",
     "url=http://192.168.8.9/MercuryWebTours/itinerary.pl",
     "Method=POST",
     "RecContentType=text/xml",
     form,
     "Snapshot=t7.inf",
     LAST);
return 0;
}
View Code

 

 

 

web_submit_data

LR幫助文檔的定義

Performs an "unconditional" or "contextless" form submission. It allows you to generate GET and POST requests as made by the HTML forms. You do not need to have a form context to execute this request.

 int web_submit_data( const char *StepName, const char *Action, <List of Attributes>, ITEMDATA, <List of data>, [ EXTRARES, <List of Resource Attributes>,] LAST ); 

 

示例:

數據提交形式:“Name=屬性名稱,”,“Value=屬性值” 

In the following example, the web_submit_data function submits a form using the POST method: 
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=txtDepartureDate", "Value=8/19/2003", ENDITEM, "Name=txtReturnDate", "Value=8/19/2003", ENDITEM, "Name=txtQuantity", "Value=1", ENDITEM, "Name=radClass", "Value=1", ENDITEM, "Name=radSeat", "Value=1", ENDITEM, "Name=btnAvailableFlights", "Value=Next >", ENDITEM, LAST ); In the following example, the web_submit_data function submits two files using the POST method: web_submit_data("Attachments", "Action=http://barton.cottage@.Devonshire.uk/Attachments?YY=45434", "Method=POST", "EncType=multipart/form–data", "TargetFrame=", "RecContentType=text/html", "Referer=http:///barton.cottage@.Devonshire.uk/Compose?YY=20435", "Snapshot=t5.inf", "Mode=HTML", ITEMDATA, "Name=userFile0", "Value=E:\\sense_sensibility\\Elinor.htm", "File=yes", "ContentType=text/html", // Override default "text/plain" for .txt files FilePath=Elinor.txt", "ContentTransferEncoding=html/text", ENDITEM, "Name=userFile1", "Value=E:\\sense_sensibility\\Marianne.jpg", "File=yes", ENDITEM, LAST );

http接口實例

 loadrunner接口測試原理是web_submit_data函數發送post或者get請求,將測試用例數據進行參數化,使用關聯獲取響應的結果值,與預期結果進行比對,將過程中相關的參數保存到html文件中,從而實現接口自動化,實現代碼如下:

1、Init部分:生成html格式文件,文件名稱為 test _系統時間(%Y%m%d%H%M%S)_虛擬用戶編號,並寫入測試結果文件的html開始標識

  //定義結果文件變量

  long file;

  //定義虛擬用戶編號變量

  char *vusernum;

  //定義測試結果變量

  char V_Result[1024];

  vuser_init()

  {

  //取得虛擬用戶編號

  vusernum=lr_eval_string ("_{vuserid}");

  //取得系統時間

  lr_save_datetime("%Y%m%d%H%M%S", DATE_NOW, "now_date");

  //拼接測試結果文件名稱

  strcpy(V_Result,"d://test/Result/test");

  strcat(V_Result,lr_eval_string("_{now_date}"));

  strcat(V_Result,vusernum);

  strcat(V_Result,".html");

  //生成並打開測試結果文件

  file=fopen(V_Result,"at+");

  //寫入測試文件頭部html信息

  strcpy(V_Result,"<html><table  border='1'><tr>< td>IMSI號碼</td><td>預期值</td><td>返回值< /td><td>結果</td></tr>");

  fputs(V_Result,file);

  return 0;

  }

2、Action部分:從參數化文件讀取測試參數和預期結果、發送請求並獲得服務器返回實際結果,比較測試結果后寫入測試結果文件。

  Action()

  {

  //測試結果文本

  char V_testres[1024];

  //定義返回結果是否正確變量

  int result;

  //取得IMSI號碼

  char *V_imsi=lr_eval_string ("{IMSI}");

  //設置頁面接收最大的字節數,該設置應大於服務器返回內容的大小

  web_set_max_html_param_len("20000");

  //取得服務器返回內容

  web_reg_save_param("filecontent",

  "LB=",

  "RB=",

  "Search=Body",

  LAST);

  //發送請求

  web_submit_data("login",

  "Action=http://host:port/autonavit/search?cmd=clientlogin&termver=5&termcode=30001&termdbver=3 ",

  "Method=POST",

  "RecContentType=text/html",

  "Referer=",

  "Snapshot=t9.inf",

  "Mode=HTTP",

  ITEMDATA,

  "Name=imsi", "Value={IMSI}", ENDITEM,

  LAST);

  //比較預期值和實際值是否相等

  result=strcmp(lr_eval_string("{YQJG}"),lr_eval_string("{filecontent}"));

  if ( result == 0 )

  {

  strcpy(V_testres,"通過");

  }

  else

  {

  strcpy(V_testres,"失敗");

  }

  strcpy(V_Result,"<tr><td>");

  //寫入測試參數

  strcat(V_Result,V_imsi);

  strcat(V_Result,"</td>");

  strcat(V_Result,"<td id='yq'>");

  //寫入預期結果

  strcat(V_Result,lr_eval_string("{YQJG}"));

  strcat(V_Result,"</td>");

  strcat(V_Result,"<td id='sj'>");

  //寫入實際結果

  strcat(V_Result,lr_eval_string("{filecontent}"));

  strcat(V_Result,"</td>");

  strcat(V_Result,"<td>");

  //寫入測試是否通過

  strcat(V_Result, V_testres);

  strcat(V_Result,"</td></tr>");

  fputs(V_Result,file);

  return 0;

  }

3、End部分:寫入測試結果文件尾部html信息,關閉文件並結束測試。

  vuser_end()

  {

  //結束並關閉文件

  strcpy(V_Result,"</table></html>");

  fputs(V_Result,file);

  fclose(file);

  return 0;

  }
View Code

 

 

 

 

如何選擇錄制方式,會生成上述兩種函數?

區別:

1、從理論上講,web_custom_request函數屬於萬能函數,完全可以替代web_link()、web_url()、web_submit_data()這些函數的存在。 request函數頁面的提交方式,POST、GET、PATCH、PUT等,web_submit_data只能提交html頁面的get和post請求。

2、header?

3、

 

使用web_custom_request的方式:

1.批量提交多條同屬性名稱的數據的請求。這種形式也手動換為request函數(有時需要“&”都替換成%26)。
2.header屬性x-requested-by值為XMLHttpRequest的POST請求。

 

比如“查詢所有郵件並刪除”中,查詢時使用關聯把所有郵件對應的標識抓取成一個數組,如果使用web_submit_data來完成這個刪除的請求,需要很多個web_submit_data請求才能完成,但使用web_custom_request就可以通過一個請求完成,方法是寫代碼拼一個web_custom_request 方法POST請求的Body值。

 

手動寫web_custom_request時需要注意的問題:

1、特殊字符

  web_custom_request中body中的屬性值如果包含一些特殊字符,必須通過URL編碼,否則Web服務器會返回500錯誤,一個典型的例子是如果Body中包含ViewState,ViewState中常常有“=”之類的特殊字符,此時必須通過URL編碼。

2、EncType編碼類型

  此參數給出一個內容類型(Content-Type),指定其做為回放腳本時“Content-Type”請求頭的值,例如“text/html”。Web_custom_request函數不處理未編碼的請求體。請求體參數將會使用已經指定的編碼方式。因此,如果指定了不匹配HTTP請求體的“EncType”,會引發服務端的錯誤。通常我們建議不要手動修改錄制時的“EncType”值。 
  任何對於“EncType”的指定都會覆蓋web_add_[auto_]header函數指定的Content-Type。當指定了“EncType=”(空值)時,不會產生“Content-Type” 請求頭。當省略了“EncType”時,任何一個web_add_[auto_]header函數都會起作用。如果既沒有指定EncType也沒有web_add_[auto_]header函且“Method=POST”,“application/x-www-form-urlencoded”會做為默認值來使用。其他情況下,不會產生Content-Type請求頭。

 

參考資料:

web_custom_request函數詳解

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM