The LoadRunner provides C functions related to the string as follows:
Lr_save_datetime |
Save the current date and time to a parameter. |
Lr_save_int |
Save an integer as a parameter |
Lr_save_searched_string |
Saves the portion of a character array relative to a string. |
Lr_save_string |
Saves a string to a parameter. |
Lr_save_var |
Save part of the string as a parameter |
Lr_eval_string |
Returns the actual content of a parameter or the actual content of a string containing the parameter. |
The following describes these functions and provides examples.
VoidLr_save_datetime (Const char *Format,IntOffset,Const char *Name);
Parameter description:
Format |
Format of the retrieved date/time information |
Offset |
The offset of the current date and time, in the form of date_now, time_now, one_day, one_hour, one_min. Example: time_now + one_hour |
Name |
The name of the saved parameter to store the date/time information. |
Example:
Action()
{
lr_save_datetime("Tomorrow is %B %d %Y", DATE_NOW + ONE_DAY, "next");
lr_output_message(lr_eval_string("{next}"));
return 0;
}
The running result is:
Running vuser...
Starting iteration 1.
Starting action.
Action. C (5): Tomorrow is July 06 2011
Ending action.
Ending iteration 1.
Ending vuser...
IntLr_save_int (IntValue, Const char *Param_name);
Parameter description:
Value |
The integer to be saved to the parameter |
Param_name |
Saved parameter name |
Example:
Action()
{
int num;
num = 5;
lr_save_int(num * 2, "param1");
lr_output_message(lr_eval_string("{param1}"));
return 0;
}
The running result is:
Running vuser...
Starting iteration 1.
Starting action.
Action. C (6): 10
Ending action.
Ending iteration 1.
Ending vuser...
IntLr_save_searched_string(Const char *Buffer, LongBuf_size, Unsigned intOccurrence, Const char *Search_string, IntOffset, Unsigned intString_len, Const char *Parm_name);
Parameter description:
Buffer |
The string or carray buffer, part of whose contents you want to save. |
Buf_size |
The buffer size. |
Occurrence |
The occurrence number of the search_string (0-based count). For example, if the search_string occurs three times, and you want the second occurrence, SetOccurrenceTo 1. |
Search_string |
The string to search for in the buffer. |
Offset |
The number of characters to skip after the end of the search string occurrence. |
String_len |
The number of characters to save. |
Parm_name |
Parameter name to be used in subsequent LR statements to refer to the saved information. Name is enclosed in double-quotes. |
Example:
Action()
{
char cBuff[] = "abc Emma Woodhouse abc Elizabeth Bennet abc William Price";
lr_save_searched_string(cBuff, strlen(cBuff),
2, "abc", // Search for third occurrence of "abc"
1, // Skip the space after "abc"
4, // Put the next four characters...
"Fannys_brother"); // ... in parameter Fannys_brother.
lr_output_message("%s",lr_eval_string("Fannys_brother={Fannys_brother}"));
return 0;
}
The running result is:
Running vuser...
Starting iteration 1.
Starting action.
Action. C (9): fannys_brother = Will
Ending action.
Ending iteration 1.
Ending vuser...
IntLr_save_string (Const char *Param_value, Const char *Param_name);
IntLr_save_var (Const char *Param_value, Unsigned long constValue_len, Unsigned long constOptions, Const char *Param_name);
Char *Lr_eval_string (Const char *Instring);
These three functions are often used together. For example:
Example:
Action()
{
lr_save_string("testaaaab", "InName");
lr_output_message("%d",strlen("{InName}"));
lr_output_message("%d",strlen("{InName}")-1);
lr_save_var( lr_eval_string("{InName}"),
strlen("{InName}")-1, 0, "ShortName");
lr_output_message("%s",lr_eval_string("ShortName={ShortName}"));
lr_output_message("ShortName=%s",lr_eval_string("{ShortName}"));
return 0;
}
Starting iteration 1.
Starting action.
Action. C (5): 8
Action. C (6): 7
Action. C (9): shortname = testaaa
Action. C (10): shortname = testaaa
Ending action.
Ending iteration 1.
* *** Note: In LoadRunner, many string processing functions are used and their usage is simple. You can practice more quickly in project practice. *****