Thursday, September 6, 2012

Function to replace a string in Loadrunner

Here is one Function which we can use for Replace a string:

As you know there Is no inbuilt function is available in C to replace a string, but it can be done using a manual function and here is the function.

And there are lot of manual functions are available to replace a string, but those are all not work all the time in Loadrunner, if it runs also we will get some irrelevant output and errors.
That’s why The following code will work in all the conditions of the Loadrunner.
And it is more useful for those who are getting the response of special characters in Binary or hexadecimal form..

For example:
If you are getting a response of address like this “37\x20WALNUT\x20DRIVE\x2C\x20LARNE\x2C\x20BT40\x202WQ” and if you are passing this value as “37 WALNUT DRIVE, LARNE, BT40 2WQ” it is necessary to convert the all hexadecimal values into normal form, but if you know the real values of hexadecimal response like “\x20=  “ and “\x2C = ,” then you can replace the whole string with the new string.

And incase if you are getting the date as “2012\x2D02\x2D09” and the real date format you are passing is like “2012-02-09”, it is more headache to convert this date type with the normal C replace string functions in Loadrunner;
Because when you are doing with the C code, if you are assigning any values like “2012\x2D02\x2D09”  to a variable it will directly convert these hexadecimal value into “2012-02-09” without any replace function while assigning to a variable, But it will not happen with the Loadrunner response data, because the response will be stored in a register variable as a whole string format and if we assign this value to another variable also will not convert the string into normal form.

That’s why the Below function is very useful for those who wants to replace their hexadecimal response string to as they want.

Note:
This will Not work with normal ‘C-User’ Protocol But it will Work in ‘HTTP/HTML’ Protocol because here we are using a Web protocol function” web_conver_parm();”.
If you want to work this code in other protocols means some slight changes required.
And This Function can still Optimized depending on your need.

Here is the Function:

char *strReplace(const char *src, const char *from, const char *to) 
 { 
   char *value; 
   char *dst; 
   char *match; 
   int size; 
   int fromlen; 
   int tolen; 

   size = strlen(src) + 1; 
   fromlen = strlen(from); 
   tolen = strlen(to); 
    
   value = (char *)malloc(size); 
    
   dst = value; 
    
   if ( value != NULL ) 
   { 
     for ( ;; ) 
     { 
       match = (char *) strstr(src, from); 
       if ( match != NULL ) 
       { 
         size_t count = match - src; 
                                 char *temp; 
                                 size += tolen - fromlen; 
                                 temp = (char *)realloc(value, size); 
    
         if ( temp == NULL ) 
         { 
          free(value); 
           return NULL; 
         } 
    
         dst = temp + (dst - value); 
         value = temp; 
    
         memmove(dst, src, count); 
         src += count; 
         dst += count; 
    
         memmove(dst, to, tolen); 
         src += fromlen; 
         dst += tolen; 
       } 
       else
       { 
         strcpy(dst, src); 
         break; 
       } 
     }
   } 
   return value; 
 } 

 int lr_replace( const char *lrparam, char *findstr, char *replacestr ) 
 { 
   int res = 0; 
   char *result_str; 
   char lrp[1024]; 
    
   sprintf( lrp, "{%s}", lrparam); 
    
   result_str = strReplace( lr_eval_string(lrp), findstr, replacestr ); 
    
   if (result_str != NULL ) 
   { 
     lr_save_string( result_str, lrparam ); 
     free( result_str ); 
     res = 1; 
   } 
   return res; 
 }

Action()
{
                char *SetUpDt;
SetUpDt=lr_eval_string("{ParamSetUpDt}");
lr_output_message(" The Original String = %s", SetUpDt); 

lr_save_string(SetUpDt, "MyPar"); 
web_convert_param( "MyPar", 
                "SourceEncoding=PLAIN", 
                 "TargetEncoding=URL",
                LAST);

lr_output_message(" The Converted String = %s", lr_eval_string("{MyPar}")); 

lr_replace("MyPar", "%5Cx2D", "-" ); 

lr_output_message("The Replaced String = %s", lr_eval_string("{MyPar}"));

Return 0;
}

Output:
The original String =2012\x2D02\x2D09
The Converted String = 2011%5Cx2D04%5Cx2D11
The Replaced String = 2012-02-09

2 comments:

  1. Hello Shashikant,
    The Article on Function to replace a string in Loadrunner gives detailed information about it. Thanks for Sharing the information about the Function to replace a string in Loadrunner For More information check the detail on the Loadrunner testing here Software Testing Company

    ReplyDelete
  2. Wow, What a Excellent post. I rceally found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Visit here for Penetration testing servicces

    ReplyDelete