標籤:協助文檔 名稱 info fail 參數 duration auto null console
轉http://blog.163.com/yings_9371/blog/static/66196922010711115545137/
(1)LoadRunner上傳檔案
web_submit_data("importStudent.do",
"Action=https://testserver/console/importStudent.do",
"Method=POST",
"EncType=multipart/form-data",
"RecContentType=text/html",
"Referer=https://testserver/console/displayStudentList.do",
"Snapshot=t12.inf",
"Mode=HTTP",
ITEMDATA,
"Name=uploadFile", "Value=D:\\Excel\\data161955.zip", "File=yes", ENDITEM,
LAST);
註:指令碼錄製使用Http/Html協議或Click and Script協議均可,以上是使用Http/Html錄製的的指令碼。需要特別注意的是,無論使用哪種協議錄製,待上傳的檔案路徑及檔案名稱只能為英文字元,而不能為中文字元。否則,可能將檔案上傳的二進位流同時錄製到LR的指令碼中。
這個問題同時也說明,LR對中文的支援還遠不夠。
下面是該應用更詳細的樣本。
Action()
{
//定義double類型的變數trans_time,用來儲存事務的執行時間。
double trans_time;
//定義int類型的變數status,用來儲存事務執行的返回的值。
int status;
lr_start_transaction("測試");
//使用內建函數lr_get_transaction_duration擷取事務的執行時間。
trans_time=lr_get_transaction_duration("測試");
status= web_submit_data("importStudent.do",
"Action=https://testserver/console/importStudent.do",
"Method=POST",
"EncType=multipart/form-data",
"RecContentType=text/html",
"Referer=https://testserver/console/displayStudentList.do",
"Snapshot=t12.inf",
"Mode=HTTP",
ITEMDATA,
"Name=uploadFile", "Value=E:\\datafile\\data01.zip", "File=yes", ENDITEM,
LAST);
//增加手工事務,如果該測試事務執行傳回值為0,則事務執行成功,否則執行失敗。
if (status == 0)
lr_end_transaction("測試", LR_PASS);
else
lr_end_transaction("測試", LR_FAIL);
//列印事務執行時間。
if (trans_time)
lr_output_message("事務執行時間為:%f 秒",trans_time);
else
lr_output_message("事務執行時間未知!");
return 0;
}
以下是LR的協助文檔中的web_submit_data函數的一個應用樣本,從該樣本中也可見一斑。
web_submit_data("Attachments",
"Action=http://[email protected]/Attachments?YY=45434",
"Method=POST",
"EncType=multipart/form–data",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http:///[email protected]/Compose?YY=20435",
"Snapshot=t5.inf",
"Mode=HTML",
ITEMDATA,
"Name=userFile0",
"Value=E:\\sense_sensibility\\Elinor.txt",
"File=yes",
"ContentType=text/html", // Override default "text/plain" for .txt files
ENDITEM,
"Name=userFile1",
"Value=E:\\sense_sensibility\\Marianne.jpg",
"File=yes",
ENDITEM,
LAST);
(2)LoadRunner下載檔案
Action()
{
/*下載協助文檔*/
//定義一個整型變數儲存獲得檔案的大小
int flen;
//儲存檔案控制代碼
long filedes;
//儲存檔案路徑及檔案名稱
char file[256]="\0";
//定義一個隨機數
char * strNumber;
//Returns the string argument after evaluating embedded parameters.
strNumber=lr_eval_string("{RandomNum}");
//將路徑儲存到file變數中
strcat(file,"D:\\Excel\\");
//將獲得的隨機數拼接在file變數之後
strcat(file,strNumber);
//將下載的檔案尾碼名.rar拼接在file變數之後
strcat(file,".rar");
//到此為止一個LoadRunner已經獲得了一個完整的檔案名稱
//設定參數的最大長度,注意該值必須大於檔案的大小
web_set_max_html_param_len("20000");
//使用關聯函數擷取下載檔案的內容,在這裡不定義左右邊界,獲得伺服器響應的所有內容
web_reg_save_param("fcontent",
"LB=",
"RB=",
"SEARCH=BODY",
LAST);
/*下載協助檔案*/
lr_start_transaction("下載協助檔案");
//發送下載協助檔案的請求
web_url("DownLoadServlet",
"URL=https://testserver3/servlet/DownLoadServlet?filename=help",
"Resource=1",
"RecContentType=application/x-msdownload",
"Referer=",
"Snapshot=t41.inf",
LAST);
//擷取響應中的檔案長度
flen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
if(flen > 0)
{
//以寫方式開啟檔案
if((filedes = fopen(file, "wb")) == NULL)
{
lr_output_message("開啟檔案失敗!");
return -1;
}
//寫入檔案內容
fwrite(lr_eval_string("{fcontent}"), flen, 1, filedes);
//關閉檔案
fclose(filedes);
}
lr_end_transaction("下載協助檔案",LR_AUTO);
return 0;
}
loadrunner 上傳下載