Friday, March 17, 2023

Capture dynamic data and write it to an external file in LoadRunner

To capture dynamic data and write it to an external file in LoadRunner, you can use the web_reg_save_param function to capture the data and the standard C file I/O functions to write it to a file. Here's an sample code of mine.

Action()
{
char *dynamic_data;
web_reg_save_param("dynamic_data", "LB=<start>", "RB=<end>", LAST);
// Make the request that returns the dynamic data

web_url("example.com", "URL=http://example.com/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t1.inf", "Mode=HTML", LAST);

// Retrieve the captured dynamic data

dynamic_data = lr_eval_string("{dynamic_data}");

// Write the captured data to a file

FILE *fp = fopen("output.txt", "w"); // Replace "output.txt" with your desired file name
if (fp != NULL)
{
fputs(dynamic_data, fp);
fclose(fp);
}
else
{
lr_error_message("Failed to open file for writing.");
}
return 0;
}

Here the web_reg_save_param will capture the dynamic data between the specified left and right boundaries, and store it in a parameter named "dynamic_data". Then, we make a request that returns the dynamic data, causing it to be captured and saved in the parameter. Next, we retrieve the captured dynamic data using lr_eval_string and store it in the dynamic_data variable. Finally, we use standard C file I/O functions to open a file named "output.txt" for writing, write the dynamic data to the file using fputs, and then close the file using fclose.

No comments: