使用Loadrunner進行文件的上傳和下載


最近使用loadrunner中需要錄制文件的上傳和下載,上傳功能模塊利用錄制可以直接實現,下載無法實現,在網上找到了一段代碼,自己動手試驗了下,發現沒有用

辛苦找到的,還是記錄下吧

 

(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://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.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;

}


免責聲明!

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



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