Downloading and Saving a File Using LoadRunner

From PeformIQ Upgrade
Jump to navigation Jump to search

Capturing a File

LoadRunner Code Fragment

   ...

   char            msgbuf[256];
   char           *szBuf;
   unsigned long   nLength;

   ...

   //---------------------------------------------------------------------------------------------
   //  Now <Preview>

   lr_think_time(5);

   // LabelsRep0000000000004427150720081408356037812

  web_reg_save_param("ReportName",
     "LB/IC=ReportOutputTemp/",
     "RB/IC=.pdf",
     "RelFrameId=1",
     LAST);

   web_reg_save_param("Report_PDF",
      "LB/IC=\r\n\r\n",
      "RB/IC=",
      "RelFrameId=1",
      LAST);

   // lr_set_debug_message(log_profile, LR_SWITCH_ON);

   lr_start_transaction("LabelsReprint_GenAndDownload");

   web_submit_data("LabelsReprint_GenAndDownload", 
                   "Action=http://{System}/LabelsReprint.asp", 
                   "Method=POST", 
                   "RecContentType=application/pdf", 
                   "Referer=http://{System}/LabelsReprint.asp?UserID={UserID}", 
                   "Snapshot=t46.inf", 
                   "Mode=HTTP", 
                   ITEMDATA, 
                   "Name=txtUserId",          "Value={UserID}",             ENDITEM, 
                   "Name=txtFileName",        "Value=null",                 ENDITEM, 
                   ...
                   LAST);

   t_tx = lr_get_transaction_duration("LabelsReprint_GenAndDownload");
   lr_end_transaction("LabelsReprint_GenAndDownload", LR_AUTO);

   sprintf(msgbuf, "[LabelsReprint_GenAndDownload]  Date: [%s]  Response time: [%f]",
      lr_eval_string("{Date}"),
      t_tx);
   userLog(msgbuf);
   
   lr_eval_string_ext("{Report_PDF}", strlen("{Report_PDF}"), &szBuf, &nLength, 0, 0, -1);

   sprintf(msgbuf, "C:\\Temp\\%s_%s_%s_%s.pdf",
      login_id,
      lr_eval_string("{DC_ID}"),
      lr_eval_string("{YMD}"),
      lr_eval_string("{ReportName}"));
   userLog(msgbuf);

   WriteDataToFile(msgbuf, szBuf, nLength);  // <Save> PDF

   sprintf(msgbuf, "DC_ID: [%s]  YMD: [%s]  Length: [%d]",
      lr_eval_string("{DC_ID}"),
      lr_eval_string("{YMD}"),
      nLength);
   userLog(msgbuf);

   lr_set_debug_message(log_profile, LR_SWITCH_OFF);

WriteDataToFile() Method

This is contained in the library code, as follows:

//-------------------------------------------------------------------------------------------------

int WriteDataToFile(char *szFileName, const char *szBuf, int len)
{
    int hFile;
    
    hFile = fopen(szFileName,"wb");

    if (hFile == NULL)
    {
        lr_error_message("Could't create or open the file: %s", szFileName);
        return LR_FAIL;
    }

    fwrite(szBuf, len, 1, hFile);
    fclose(hFile);
 
    return LR_PASS;
}  // WriteDataToFile
 
//-------------------------------------------------------------------------------------------------