How to capture and save file on local machine in VuGen LoadRunner
As we know LoadRunner does not recognize the Client side activities, so the activity of saving file on local machine will not be recorded by LoadRunner. Below is the one way to resolve this.
Just before the URL which opens the PDF, capture the whole page into a string through correlation and write the captured string into a PDF file using File handling.
Sample Code:
Code Captures the PDF value in variable – c_PDF_Data
[cc lang=”c”]
char * filename = “c:\\PerfTuit.pdf”;
long File_Size;
long Download_time;
long HttpRetCode;
long file;
int flag;
/*Set the parameter size large enough to save the data.*/
web_set_max_html_param_len(“500000”);
/*Mention left and right boundary as blank of web_reg_save_param function to capture the pdf returned by the server.*/
web_reg_save_param(“c_PDF_Data “,”LB=”,”RB=”,”Search=Body”,LAST);
/*get the loading of PDF status in flag */
flag=web_submit_data(“PerfTuit.aspx”,
“Action=http://{p_SampleURL}/subDomain/area/ PerfTuit.aspx”,
“Method=POST”,
“RecContentType=application/pdf”,
“Referer=http://{p_SampleURL}/subDoamin/area/”,
“Snapshot=t01.inf”,
“Mode=HTML”,
LAST);
if(flag==1){
lr_error_message(“status fail”);
lr_exit(LR_EXIT_VUSER, LR_FAIL);
}
/* we have captured the pdf file into string, Now we will write the captured string into the PDF file.*/
/*First we will save the size of downloaded file into variable named sizePDF*/
File_Size= web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
/* other details*/
Download_time = web_get_int_property( HTTP_INFO_DOWNLOAD_TIME );
HttpRetCode = web_get_int_property( HTTP_INFO_RETURN_CODE );
lr_output_message(“File size is %d “, File_Size);
lr_output_message(“Download Time is %d “, Download_time);
lr_output_message(“HTTP Return Code is %d “, HttpRetCode);
/*Write the string captured into file.*/
if ((file = fopen(filename, “w+”)) == NULL) {
lr_output_message (“Unable to create %s”, filename);
return -1;
}
fwrite(lr_eval_string(“{c_PDF_Data}”), File_Size, 1, file);
fclose(file);
[/cc]
Very good Contents!
thanks alka..
Hi.. Nice content dude!
Could you include an article on creating custom request ! Also , an article on XML in Loadrunner will be in order.
Once again , congrats!
sure..wil start working on it..
Hi,
This works good for one pdf file, how to do if i want to run in controller with 300 users, how can i save and verify the download time for each pdf file.
Please reply me asap.
Thanks,
Prakash
@prakash
instead of using hard coded filename use below code to generate filenames. It will work for multiple users.
char fileName[50], * suffix = “pdf”;
web_save_timestamp_param(“tStamp”, LAST );
sprintf(fileName, “c:\\File_%s.%s”, lr_eval_string(“{tStamp}”), suffix);
lr_output_message (“The new file name is %s”, fileName);
*dilip
hi, can we findout the pdf download time also
@prakash
use below code get download time for each file,
lr_output_message(“Download time for file %s = %d”, fileName,Download_time);
*dilip
hi,
i just got stuck with this, could you please tell me where i have to keep the above code in the script.
also i placed the code before the pdf file opens but still its not working and
“Action=http://{p_SampleURL}/subDomain/area/ PerfTuit.aspx”, pls tell me wht exactly is this line?
please reply asap
Hi,
I edited my script using the above code, but the script still only captures header bytes as the File size.I am working with web_custom_request instead of web_submit_data. Does that makes any difference. Please let me know if there is a way to work around that one.
Following is the modified script of what i did:
lr_start_transaction(“Click_Download”);
flag = web_custom_request(“{Download}.ashx”,
“URL=https://{URL}/{Download}.ashx”,
“Method=POST”,
“Resource=1”,
“RecContentType=application/pdf”, //I addded this line later on after
//looking your code
“Referer=https://{URL}/{Page}.aspx”,
“EncType=application/x-www-form-urlencoded; charset=UTF-8”,
“Body=json=%5B%7B%22FileName%22%3A%{Dnld_Doc_Name}%22%2C%22RA1381336176565.pdf%22%3A%22RA1381336176565%22%2C%22Category%22%3A%22Result%20Alerts%22%2C%22FilePath%22%3A%22102013%22%7D%5D”,
LAST);
if(flag ==1) {
lr_error_message(“status fail”);
lr_exit(LR_EXIT_VUSER, LR_FAIL);
}
File_Size = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);
Download_time = web_get_int_property(HTTP_INFO_DOWNLOAD_TIME);
HttpRetCode = web_get_int_property(HTTP_INFO_RETURN_CODE);
lr_output_message(“File size is %d”, File_Size);
lr_output_message(“Download Time is %.3f Seconds”, Download_time/1000);
lr_output_message(“Download time for file %s is = %d”, filename, Download_time);
lr_output_message(“HTTP Return Code is %d”, HttpRetCode);
//WRITE THE STRING CAPTURED INTO FILE
if ((file = fopen(filename, “w+”)) ==NULL)
{
lr_output_message(“Unable to create %s”, filename);
return -1;
}
fwrite(lr_eval_string(“{c_PDF_Data}”), File_Size, 1, file);
fclose(file);
lr_end_transaction(“Click_Download”,LR_AUTO);
Your Helo will be much appreciated.
Thanks
Hi Anup,
Try running your script with proxy of fiddler and check if that request above which you have added web_reg_save_param() function is responding with PDF. As you said you added “RecContentType=application/pdf” later, this is weird. If server is responding with PDF then RecContentType should be there as PDF.
-dilip
Hi Anup,
There is no error in my vugen script after implementing the above code but pdf which is being saved as in my local system is not exactly downloading with exact size, it shows 1kb size though the pdf download size is more than 80kb.
please help.
Hi Anup,
PDF download functionality in my VuGen script works only for sometime without any errors but after an hour the pdf is not getting downloaded and the script replay log through error saying it fails at web_reg_save_param(“c_PDF_Data “, “LB=”, “RB=”, “Search=Body”, LAST);
Nice one.Thank you.
Excellent one. It helps me a lot.
hi
could you please help me for
while recording capturing data from gridview in ASP.NET,
actually when recording finish gridview data not recording please help me
Nice article. Really helped me. Thanks a TON 🙂
I need to download a repository which is rather large (to be cleaned up later) How would I go about doing that?