There is always a confusion related to variables in LoadRunner script. Because some variables require defining at the start of the script while some variables can be used directly without any initialization. Let’s discuss what is the exact difference in them?
Basically, there are two types of variable in the LoadRunner:
1. Language-Specific Variables:
Language-specific variables are those variables which need both declaration and definition (optional) at the start of the script such as:
int count;
float amount;
You can use these variables to perform some mathematical or file handling operations in the script.
2. LoadRunner Variables:
These variables do not require any explicit declaration. When a function calls then LoadRunner declares these variables internally. Such as:
lr_save_string("ABC","myVar");
web_reg_save_param("correlationVar", "LB =", "RB =", LAST);
LoadRunner variable is used with curly braces in the script. For example, the correlation variable will be written as {correlationVar}.
Differences:
Language-Specific Variable | LoadRunner Variable |
---|---|
Required Declaration at the beginning | Do not require any declaration |
Mostly used by the language-specific or custom functions like strcpy(), rand() | Mostly used by protocol specific functions like web_url(), web_reg_find() |
Language-specific functions do not use LR variables directly. | LoadRunner functions accept both type of variables |
To use the value of LR variable in the language-specific function, it should be copied into a Language-Specific variable. | To copy the value of language-specific variable to LR variable lr_save_string(), lr_save_int() functions are used. |
Language-specific variables can be used directly in the script. | While using LR variable, they should be enclosed within Curly Braces {} |
In Action
int Num1,Num2,Result;
float d1,d2,fR;
char ch;
char *stringArray1; // preferred way of declaring strings
char stringArray2[15] ="Array";
char *stringArray3; // preferred way of declaring strings
Integer Operations
//INTEGERS
Num1 = 10;
Num2=20;
Result = Num1 + Num2;
lr_output_message("Addition of %d and %d is %d",Num1, Num2, Result);
//Comparing Numbers
if(Num1==Num2){
lr_output_message("Numbers are equal");
}
else{
lr_output_message("Numbers are not equal");
}
Float
//FLOAT
d1=10.25;d2=33.55;
fR =d1+d2;
lr_output_message("Float Addition of %f and %f is %.2f",d1, d2, fR);
Character
//Character
ch = 'M';
lr_output_message("Character placeholder %c",ch);
String Array
// String Array
stringArray1 = "String";
lr_output_message("String placeholder %s",stringArray1);
//String Array
lr_output_message("String array placeholder %s",stringArray2);
STRING COMPARISON
//STRING COMPARISON
stringArray1 = "Test";
stringArray3 = "Test";
//strcmp will return either <0,0,or >0....## 0==Equal
if(atoi(strcmp(stringArray1,stringArray3))==0){
lr_output_message("Strings are equal");
}else{
lr_output_message("Strings are not equal");
}
Conversions
int iValue =100;
char *sBuff = "string of characters";
float fValue = 234.43;
ASSIGNING A VARIABLE (C WORLD) TO ANOTHER VARIABLE (LR WORLD)
To copy the value of language-specific variable to LoadRunner variable, lr_save_int() and lr_save_string() functions are used.
lr_save_int(iValue,"LR_iValue");
lr_save_string(sBuff,"SOMEVALUE");
lr_output_message("Assigning int %d",lr_eval_string("{LR_iValue}"));
lr_output_message("Assigning string of characters %s",lr_eval_string("{SOMEVALUE}"));
//Assigning Float Value
lr_param_sprintf("LR_Float","%f",fValue);
lr_param_sprintf("LR_INT","%d",iValue);
lr_param_sprintf("Token","Bearer %s",sBuff);
//Assigning everything to a param value
lr_param_sprintf("LR_COMBINED","iValue is %d, with strings as : %s",iValue,sBuff);
ASSIGNING A VARIABLE (LR WORLD) TO ANOTHER VARIABLE (C WORLD)
lr_eval_string() is an important function in LoadRunner which helps to convert LoadRunner variable into language-specific variable. Whenever you want to use the value of LoadRunner variable in a language-specific function then you need to first evaluate the parameter. lr_eval_string function evaluates the parameter and returns the value in a string data type.
Example:
A LoadRunner variable (say lrVarStr) stores a value ‘PerfMatrix’, so lr_eval_string(“{lrVarStr}”) will return ‘PerfMatrix’ which will be copied in a language-specific variable (say lsVarStr).
char lsVarStr[15];
..
strcpy(lsVarStr, lr_eval_string("{lrVarStr}");
Similarly, let’s assume another LoadRunner variable (say lrVarInt) stores a value ‘100’, so lr_eval_string(“{lrVarInt}”) will return ‘100’ in string format which will be first converted into integer using atoi function and then assigned to a language-specific variable (say lsVarInt).
int lsVarInt;
..
lsVarInt = atoi(lr_eval_string("{lrVarStr}"));
Note:
- lr_eval_string() function always returns string value irrespective of datatype of parameter
- In case of an integer, the value returned by lr_eval_string() needs to be converted from string to integer explicitly
- atoi() function converts any string to an integer. Hence I used it in the above example.
sBuff = lr_eval_string("This is {LR_Param}");
lr_output_message("%s", sBuff);
//CONVERTING FROM INT TO STRING
iValue = atoi("3");
lr_output_message("%d", iValue);
INT TO STRING
char C_VARIBALE[2048]; // declare an array with size, otherwise it'll throw error.
strcpy(C_VARIBALE,lr_eval_string("Bearer {LR_TOKEN}"));
//declare sBuff as an array char sBuff[10];
sprintf(sBuff,"%d",iValue);
lr_output_message("%s",sBuff);
LR EXIT User
To exit an user from proceeding further in case of a step failure
