Wednesday, March 24, 2010

Loadrunner - How to download and save file.

Loadrunner - How to download and save file.

Based on the test scenario, we may need to replicate downloading the file by number of users.
While downloading file, we come across multiple windows based pop-ups, unfortunately actions on these pop-ups are not recorded by load runner. To over come this limitation, we need to capture the file stream and save this data as actual file using c file system.


Following points to be taken into consideration while designing the code.
1. Each file is given unique name, otherwise it would create conflict with the OS. I take virtual user id and time stamp from the parameter lit for creating file names, so that it is unique always.
2. Create file names with proper file extension (.XLS or .PFD) similar to the file you are planning to download.
3. Always use "\\" for file path.
4. Make sure you have created folder in the load injector. (c:\LR Files)
5. Always use the function "lr_eval_string_ext_free" to free the allocated memory.

Attaching sample code

    // Variable declaration 
    long filePointer;
    char *DataPointer;
    unsigned long DataLength;
    char FilePath[50],FilePath1[50]; 

    // Creating unique file names and constructing the folder path
    strcat(FilePath1,lr_eval_string("{UserId}"));
    strcat(FilePath1,"_");
    strcat(FilePath1,lr_eval_string("{DateTime}"));
    strcat(FilePath1,".xls");
    strcat(FilePath,"c:\\load\\");
    strcat(FilePath,FilePath1);

    // Just displaying the path that you have constructed above (debugging purpose only) 
    lr_output_message("File path:%s",FilePath);
    lr_output_message("File path1:%s",FilePath1);
    

    //Start creating file (File Pointer) for writing in the desired path.
    filePointer = fopen(FilePath,"wb");
    
    //Increase parameter size to accommodate your file size. 
    web_set_max_html_param_len("9999");
    
    //This function is used to capture the entire data returned by the server only in the body.
    web_reg_save_param("FILE_DATA","LB=","RB=","Search=Body",LAST);

    Loadrunner function to initiate click event and start the download process

    //This function is used to create data buffer and assign a pointer to it, so that this data can be used in file processing.  Also calculate length for the above captured data.
    // "0, 0, -1" are reserved values by loadrunner. Check the help file for more details 
    lr_eval_string_ext("{FILE_DATA}", strlen("{FILE_DATA}"), &DataPointer, &DataLength, 0, 0, -1);
    
    //This function write data into above specified file.
    fwrite(DataPointer, DataLength, 1, filePointer );



   //This function frees the above created data buffer, best practice as per loadrunner documentation.  
   lr_eval_string_ext_free(DataPointer);
    
    //This function close the file pointer
   fclose(filePointer);


-----

3 comments:

  1. filePointer = fopen(FilePath,"wb");

    This command is actually not creating a file named with the value saved "FilePath". There is no file created at all.

    Please Help.

    ReplyDelete
  2. Hello Mubassar,

    "filePointer = fopen(FilePath,"wb");" create a file pointer, it will not create a file. Actual file is create by "fwrite(DataPointer, DataLength, 1, filePointer );".

    - Bharath.

    ReplyDelete
  3. Hi Bharath,

    I have a question.

    Why cannot we use predefined structure FILE for file pointer?

    ReplyDelete