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.

2 comments: