Monday, September 8, 2014

Find Sub string from captured value in LoadRunner

This article discusses the about how we can find a sub string from a captured LR parameter by specifying an LB and RB in the captured value . We use web_reg_save_param_ex() / web_reg_save_param() or web_reg_save_param_regex() function to capture the dynamic data from the server response.

In majority of the cases the attributes used with the correlation function are good enough to capture the exact dynamic data. We can either use the attributes "SaveLen", "SaveOffset", or text flags to capture the exact dynamic data. We can also use the regular expressions to capture the exact values.

The function that is most helpful in this case is lr_save_param_regexp() where you can pass the LR parameter and specify the regular expression as below:



 lr_save_param_regexp (lr_eval_string("{DynData}"),
                     strlen(lr_eval_string("{DynData}")),
               "RegExp=(to.*?by)",
               "Ordinal=All",
               "ResultParam=reMatchesParam",
               LAST );

Now the above function would retrieve all the occurrence of strings that match the regular expression to and some string followed by by. Now the sample output of the above statement can be as below:

Action.c(7): Notify: Saving Parameter "reMatchesParam_1 = to performance by".
Action.c(7): Notify: Saving Parameter "reMatchesParam_2 = to selenium by".
Action.c(7): Notify: Saving Parameter "reMatchesParam_3 = to management by".
Action.c(7): Notify: Saving Parameter "reMatchesParam_4 = to sales by".


The only problem with the above statement is that, it is introduced in the new version LoadRunner. Below is the "C" implementation of the same.


FindAllSubStrings(char *Src, char *LB, char *RB, char * LRParam)
{

    char buff[1000], temp[100];
    char *pos1, *pos2;
    int offset, i=0;

    // Capture the position of Left boundary
    pos1 = (char *)strstr(Src, LB);
 
    //If the RB and LB both are the same, ensure that you search for RB only after LB
    if(pos1 !=0)
        {
            pos2 = (char *)strstr(pos1 + strlen(LB), RB);
        }
 
    do
    {
        // Make Buff an empty string
        strcpy(buff, "");
     
        // Should return only if both Pos1 and Pos2 are not null
        if(pos1 !=0 && pos2 !=0)
        {
            //Measure the length of the string that is to be saved into the LR parameter
            offset = (int)(pos2 -(pos1+strlen(LB)));
         
            //Save the sub string into the buff variable
            strncat(buff, pos1+strlen(LB), offset);
            i++;
            //Create the LR parameter for example "TestData_i" where i will be incremented wtih each iteration
            sprintf(temp, "%s_%d",LRParam,i);
            //Save the sub string into the parameter "TestData_i"
            lr_save_string(buff, temp);
         
            //change the pointer from where you should look for the next LB and RB
            Src = (char *)(pos2 + strlen(RB));
         
        }
        pos1 = (char *)strstr(Src, LB);
        if(pos1 !=0)
        {
            pos2 = (char *)strstr(pos1 + strlen(LB), RB);
        }
        //Repeat the process until all the values are stored to the LR parameters
     
    }while(pos1 !=0 &&pos2!=0);
 
    //Create an LR parameter that stores the count of sub strings for example "TestData_count"
    sprintf(temp, "%s_count", LRParam);
    //Save the count into the created parameter
    lr_save_int(i, temp);
 
    return 0;
}

The additional advantage of having this function is, because this is a C implementation this function can be used with any version of LoadRunner. The only modification that may be needed to change the size of the variables used in this function.  

The usage of this function is similar to that of Replacer function that I shared earlier.

Please let me know if you have any questions. If you like this article, please hit +1 / Like / Tweet / Comment.

2 comments: