友好连接

2008年11月17日星期一

Code Snippets for LoadRunner

1. How to choose the option randomly?
Description:

If one option needs to be chosen randomly in below figure, what can we do in LR?
图略


Solution:

1. Use web_reg_save_param with proper LB, RB and Ord=All to get all options’ value into an array.

2. Get a random value from array.

3. In next web_sumbit_form or web_url use the value.

Code:

Action()

{

//declare variables

int flightCount;

char Flight[100];

//replace with your web_reg_save_param

web_reg_save_param("flightList",

"LB=outboundFlight value=",

"RB=>",

"Ord=All", //Make sure Ord's value is All.

LAST);

//replace with your web submit or web url

web_submit_form("reservations.pl",

"Snapshot=t4.inf",ITEMDATA,

"Name=depart", "Value=Shanghai", ENDITEM,

"Name=arrive", "Value=Beijing", ENDITEM,

LAST);

//get the count of flightList array.

flightCount=atoi( lr_eval_string("{flightList_count}") );

/*save random flight to Flight string.

1 + rand() % flightCount: Generate a Random Number

{flightList_1}: get the first value in flightList

*/

sprintf (Flight,"{flightList_%d}",1 + rand() % flightCount );

//save Flight to String RandFlight

lr_save_string( lr_eval_string (Flight),"RandFlight" );

lr_message( lr_eval_string("{RandFlight}") );

//Use RandFlight in next step

web_submit_form("reservations.pl_2",

"Snapshot=t5.inf",ITEMDATA,

"Name=outboundFlight", "Value={RandFlight}", ENDITEM,

LAST);

return 0;

}

2. How to generate a unique input?
Description:

In the below figure we need to input unique values for the Username. How can we make sure that every username is unique during thousand iterations run?

Solution:

Controller will assign a unique VuserId to every Vuser when we start to run a Vuser. The following is two solutions we can use.

1. VuserId + iteration No

2. VuserId + Current Time (If name will be created twice in one iteration)

Code:

Action()

{

//declare variables

char testname[100];

char *VuserId;

//create a VuserId type parameter named RunningVuserId in Parameter list

VuserId=lr_eval_string("{RunningVuserId}");

//The value saved in ParamTimeStamp is the number of milliseconds since midnight January // 1st, 1970.

web_save_timestamp_param("ParamTimeStamp", LAST);

//Create testname as format: VuserId_ ParamTimeStamp.

strcpy(testname,VuserId);

strcat(testname,"_");

strcat(testname,lr_eval_string("{ParamTimeStamp}"));



lr_save_string(testname,"UniqueTestname");

lr_output_message("the value is %s", lr_eval_string("{UniqueTestname}"));

return 0;

}

3. How to check for download function?
Description:

Sometimes we do stress test that download a file from server. When we record this step, our script will generate web_url when we click download link or button. But when you replay it, this web_url always will pass and no file will actually be downloaded in the controller’s location. We can use web_reg_find to check whether we load page properly. How can we check here?

Solution:

The file won't be downloaded (Physically) to the folder but you can determine whether the request was successful or not by determining the size returned by the request. You can use the function "web_get_int_property" and "HTTP_INFO_DOWNLOAD_SIZE” option in that function to determine the size.

Code:

int flen;

//your web url that download file

// get the download file's size.

flen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);

if(flen > 0)

{

lr_end_transaction("S1_D_download", LR_AUTO);

}

else

{

lr_end_transaction("S1_D_download", LR_FAIL);

}



4. How to save file when you download in LR?
Description:

In case 3, if you also want to save the file to a location, what can be done?

Solution:

We can use web_reg_save_param to save all bytes downloaded from server and then these bytes could be written to a location.

Code:

int flen;

long filedes;

int i;



web_reg_save_param("FILED","LB=","RB=","Search=Body",LAST);

//your web url that download file



// get the download file's size.

flen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE);

if(flen > 0)

{

//create a new file. If you run the script in controller, you can use unique //name in Case 2 to generate a unique file.

if((filedes = fopen("c:\\test.xls", "wb")) == NULL)

{

lr_output_message("Open File Failed!");

lr_end_transaction("file_download", LR_FAIL);

}

//write the data which are got from server to the file created in your

// location.

i = fwrite(lr_eval_string("{FILED}"), flen, 1, filedes);

lr_output_message("Successfully wrote %d", i );

lr_end_transaction("file_download", LR_AUTO);

fclose(filedes);

}

else

{

lr_end_transaction("S1_D_download", LR_FAIL);

}

5. How to get a substring in LR?
Description:

If the requirement demands us to parse a string, we can use LR_SAVE_VAR.

Solution: See code

Code:

Action()

{

char *mystr = "I come from Shanghai";

lr_save_var(mystr + 12, 8, 0, "Mycity");

lr_output_message("My city is %s",lr_eval_string("{Mycity}"));

return 0;

}



6. How to reformat date?
Description:

When we get some date, but the format is not our expectation, what can we do?

Solution: See code

Code:



char *ReqDate;

lr_save_string("10/11/2006","MyDate");

lr_save_var(lr_eval_string("{MyDate}"),2,0,"Date");

lr_save_var(lr_eval_string("{MyDate}")+3,2,0,"Month");

lr_save_var(lr_eval_string("{MyDate}")+6,4,0,"Year");

ReqDate = lr_eval_string("{Month}/{Date}/{Year}");

lr_output_message("date is %s",ReqDate);
转自:http://www.cnblogs.com/junzhongxu/

没有评论: