lr_read_file Loadrunner Function to read the content of a text file

Loadrunner
Overview:

1. When we record a file import in a web app using load runner, we might some times find that the files content will also be passed in the web_custom_request as a JSON request.
2. When we iterate the script we need to pass unique files as inputs and the files content also change.
3. So we need to read the files content using C Program in loadrunner and convert it into JSON format and save it in a variable and pass the variable to the web_custom_request.
4. lr_read_file loadrunner function reads the files content and saves it in a loadrunner variable.
5. As part of the JSON request CR(Carriage Return) & LF(Line Feed) at the end of each line in the text file needs to be removed. This can be achieved using strtok (StringToken) C function.

C Program:

long file;
int flength;
int fcontent;
char *mbuffer;
char *delimiter = "\r\n";
char *token;
char  *temp;
char  *tempall;
char *finalfile;
int res;

temp = (char *)calloc(8000000,sizeof(char));
tempall = (char *)calloc(8000000,sizeof(char));
finalfile = (char *)calloc(8000000,sizeof(char));
 
res = lr_read_file("C:\\ACH EFT Files\\ACH_10K_50003918_0.txt", "P_fileContent", 0);
lr_message("res = %d\n data = %s\n", res, lr_eval_string("{P_fileContent}"));
 
token = (char *)strtok(lr_eval_string("{P_fileContent}"), delimiter); // capture 1st sub string based on defined delimiter

while (token != NULL ) // While valid tokens are returned

{
        lr_output_message ("%s", token );
         strcpy(temp, token);
         strcat(temp, "\\r\\n");
        lr_output_message ("%s", temp );
       
token = (char *)strtok(NULL, delimiter); // Get the next token
strcat(tempall, temp);
 }
 
memcpy(finalfile, tempall, strlen(tempall) -8);

lr_save_string(finalfile,"P_fileContent1");
lr_output_message("%s", lr_eval_string("{P_fileContent1}"));

We can also pass parameter values to the files path and name in lr_read_file function using lr_eval_string function

Example:

res = lr_read_file("C:\\ACH EFT Files\\ACH_10K_50003918_0.txt", "P_fileContent", 0);

In the above code replace C:\\ACH EFT Files\\ACH_10K_50003918_0.txt with the parameter P_FileName.

Now the code becomes,

res = lr_read_file(lr_eval_string("{P_FileName}"), "P_fileContent", 0);



12 comments: