Monday, April 2, 2012

How to create Array of String in Scripting

Arrays of string are very useful in scripting but not easy to create, whenever we need to save multiple string and we don’t want to create separate string variable for each value we can use Array of String.
When we correlate any value by giving “ORD=ALL” values het saved in array and we can refer them by correlated Parameter_name_1, Parameter_name_2 so on. But these values are getting saved by Vugen internally and variable logic is not visible to us. Same kind of logic I am going to describe below.
Array of string in C language can be created by multidimensional arrays (e.g. char MutiD_Array[10][10]) but multidimensional arrays are useful when dealing with numbers or single character but when it comes to saving a full string value it becomes bit difficult to deal with it.
Best way to create array of string is pointer to pointer variables.
If using pointer to pointer variable then need to allocate memory dynamically as per our need and then Free it once we are done
Following program/script action block will illustrate how to create array of string
Action()
{
char **Param_Name;
Param_Name= (char**)calloc(sizeof(char**),10);
Param_Name[0]="Hello";
Param_Name[1]="This";
Param_Name[2]="Is";
Param_Name[3]="Performance Testing";
lr_output_message("%s, %s, %s, %s",Param_Name[0],Param_Name[1],Param_Name[2],Param_Name[3]);
free(Param_Name);
return 0;
}
output of the following can be seen in replay log :


Virtual User Script started
Starting action vuser_init.
Web Turbo Replay of LoadRunner 9.10.0 for WIN32; WebReplay85 build 5896 [MsgId: MMSG-27143]
Run Mode: HTML [MsgId: MMSG-26000]
Run-Time Settings file: "C:\Users\DELL\AppData\Local\Temp\noname1\\default.cfg" [MsgId: MMSG-27141]
Ending action vuser_init.
Running Vuser...
Starting iteration 1.
Starting action Action.
Action.c(19): Hello, This, Is, Performance Testing
Ending action Action.
Ending iteration 1.
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated




We can use Index variable as well instead of referring Array values by index 0,1,2 so on.
e.g.
int Index=1;
Param_Name[Index]="Hello";
lr_output_message("%s”,Param_Name[Index]);

Note: Do not forget to free dynamically allocated memory otherwise it could be memory issue during the test on LGs.

No comments:

Post a Comment