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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);

17 Comments

  1. Alka Panda says:

    Very good Contents!

  2. Rahul says:

    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!

  3. Prakash says:

    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

  4. prakash says:

    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

  5. Anup says:

    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

    • admin says:

      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

  6. Venkat says:

    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.

  7. Venkat says:

    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);

  8. Nikki says:

    Nice one.Thank you.

  9. Ashis says:

    Excellent one. It helps me a lot.

  10. Gajmal Dusane says:

    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

  11. Kalyan says:

    Nice article. Really helped me. Thanks a TON 🙂

Leave a Reply