Saturday, June 8, 2013

Write data to an external file in load runner | Save Dynamic Parameters into a Text File using load runner

I got a requirement of capturing dynamic values and write it to a notepad file using load runner vugen. Here is the code snippet i have tried and worked well. i captured the order ids and I stored in local file and provided them dev team later for easy debug. Here is the code snippet.

Action()
{
char MainID;
int i;
char Length[100];
long file;

// This is to create the file for writing order IDs
char * filename = "C:\\Users\\Raviteja\\Downloads\\OredrID.txt";

//Open file with Read Permission
if ((file = fopen(filename, "a+" )) == NULL)
{
lr_output_message("Unable to create %s", filename);
return -1;
}

web_reg_save_param("OrderID","LB=value=’","RB=’/","RelFrameId=1″,LAST);

web_url("HondaCity",
"URL=https://Mynewhondacity.com/Homepage#",
"Resource=0″,
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
LAST);

web_reg_find("Text=This Order is created","SaveCount=OrderIDCount",LAST);

lr_start_transaction("Digital_HondaCity_01_OrderID");

web_submit_data("mycar.jsp",
"Action=https://Mynewhondacity.com/GenerateOrder/Valuenumber",
"Method=POST",
"RecContentType=text/html",
"Referer=https://Mynewhondacity.com/GenerateOrder/",
"Snapshot=t2.inf",
"Mode=HTML",
ITEMDATA,
"Name=sessionId", "Value={OrderID}", ENDITEM,
"Name=browserTime1″, "Value=Sat Jan 1 05 EST 2011″, ENDITEM,
"Name=browserTime2″, "Value=Wed Jun 15 05 EDT 2011″, ENDITEM,
"Name=browserOffset1″, "Value=300″, ENDITEM,
"Name=browserOffset2″, "Value=240″, ENDITEM,
"Name=clientTimeZone", "Value=", ENDITEM,
"Name=appId", "Value=WorkHondatwin", ENDITEM,
"Name=userId", "Value=Zelensky", ENDITEM,
"Name=password", "Value=Strawberry@123", ENDITEM,
LAST);


if (atoi(lr_eval_string("{OrderIDCount}")) > 0)
{
lr_output_message("Orders Page found and loaded successfully.");
}

else

{
lr_error_message("Orders Page is not found.");
lr_exit( LR_EXIT_MAIN_ITERATION_AND_CONTINUE,LR_FAIL );
return(0);
}

lr_end_transaction("Digital_HondaCity_01_OrderID",LR_AUTO);

//Writing the values into the file

sprintf(Length,"\n%s,",lr_eval_string("{OrderID}"));
i = fwrite(&Length,sizeof(Length), 1, file);
if ( i > 0)
lr_output_message("Order Ids written successfully to the file %d records", i );
fclose(file);
return 0;
}