Wednesday, August 13, 2014

String Replacer function for LoadRunner scripting

This article discusses the replacer function that may be needed in LoadRunner scripting. We use web_reg_save_param_ex() / web_reg_save_param() function to capture the dynamic data from the server response. One of the attributes that this function supports is the "Convert" either from HTML_TO_URL or HTML_TO_TEXT.

One can also use the function web_convert_param() explicitly to convert the HTML content to either URL encoded format or to a Plain Text. However, this function may not serve purpose all the time and sometimes a performance tester may need to convert a captured value to something else by explicitly replacing a sub-string with another.

For example: The dynamic value captured from the server response is "Welcome To Performance Testing Training Online" and the next request sends the data Welcome**To**Performance**Testing**Training**Online". It is clear that the Space in the dynamic data is replaced by **.

To replace every space in the dynamic data with  **, the string replacer function below is helpful.


char *Replacer(char *capValue, char *replace, char *replacewith)
{
char *pos;
int offset;
char output[1000];

pos = (char *)strstr(capValue, replace);
strcpy(output, "");
while(pos!=0)
{
offset = (int) (pos - capValue);
strncat(output, capValue, offset);
strcat(output, replacewith);
capValue = (char *) (pos + strlen(replace));
pos = (char *)strstr(capValue, replace);
}
strcat(output, capValue);
//lr_output_message("%s", output);
return output;
}

All that is needed is you can add this code before your action function or you can Add new files to script and copy paste the code there so that the function can be reused any number of times.

All that you need to do is call the function as below:

temp = Replacer(lr_eval_string("{DynData}"), " ", "**");
lr_save_string(temp, "ModDynData");

With the above two statements in your action block, all the spaces in dynamic data are replaced with ** and the modified data is saved to an LR parameter called "ModDynData".

Please note that the above function primarily depends on the strstr function to identify all the spaces in the dynamic data. To understand better about the strstr function, please read my previous blog.

If you like this blog and article please click on +1 or comment. Please feel free to share about this blog with your friends.

Sunday, August 10, 2014

strstr function in LoadRunner

The most common problem that a performance tester face with automation in LoadRunner is string manipulations. To understand and implement string manipulations, one need to know the important functions that are used to do so. the function strstr() is one of the most important functions that are used for string manipulations.

The function strstr() returns the address location of a sub-string in a main string. If the sub-string is not found in the main string, the function would return zero. Hence this function can be used to validate if a sub-string is found in the main string or not.

To understand the function better, let us consider a small example:

char MainStr[1000] = "Welcome to Performance Testing Training online";
char SubStr[10] = "Train";
char * position;

position = (char *)strstr(MainStr, SubStr);

In the above example, when a MainStr variable is declared, a variable is created and 1000 bytes are allocated and the variable "MainStr" contains the starting address location. Let us assume that in the current example the Memory address location allocated for the variable MainStr is from 12001 to 13000. Now the variable MainStr contains the address location 12001.

When the strstr() function is executed, the compiler looks for the characters in the substring starting from the first address location i.e. 12001 in this case. Now, the first character in the SubString is 'T' which would be first found at the address location,  12024 (as in Testing), however the second character in the Substring does not match with the character in the next address location. So, the compiler ignores the address location 12024 and proceeds further scanning for the character 'T' which would be found at the location 12032. All the subsequent characters do match with the characters in the subsequent address locations and hence the function strstr would the return the address location 12032. The function is type casted such that the return value is explicitly defined as character pointer.

This is the most used function for most of the string manipulations and the coming blogs illustrate the usage of this function.

I hope this information is useful. If you like the content in this blog, please click +1/ Comment. Please feel free to share with your friends.