Tuesday, November 20, 2012

How to Save the downloaded file to local system

Problem Statement: During some of our non functional testing scenarios, there might be a need to download some files. We are also asked to check the transaction time for that download process, but very rarely there is a need to save the downloaded files to your local system for some verification later. If this is the case then how can we do so...

Solution: There is a solution to do so, all you need to do is get the data from the server response and save it to a file using normal file operations. But you need to know beforehand the type of file you are going to download.

In this sample example let's consider that you are trying to download a .pdf file and want to save it to local system. Here is a sample code snippet to do so:
**************************************************

//declare the variables
int fp;
long i;

//create file for writing.
fp = fopen("c://test_file.pdf","wb");

//Start a transaction to measure the download time.
lr_start_transaction("file_download");

//Set the parameter size large enough to save the data.
web_set_max_html_param_len("100000");

//Use web_reg_save_param with the correct boundary to capture the data returned by the server (for the download)
web_reg_save_param("FILEDATA","LB=","RB=","Search=Body",LAST);

//HTTP call to download the .pdf file
web_url("http://serverURL:port/app/resource/getme.pdf");

//Get the download size.
i = web_get_int_property( HTTP_INFO_DOWNLOAD_SIZE );

//Write the data saved to an output file.
fwrite(lr_eval_string("{FILEDATA}"),i,1,fp);

//End the transaction
lr_end_transaction("file_download", LR_AUTO);

//Close the file pointer.
fclose(fp);
**************************************************

Hope this helps in your scenario.