Friday, 4 March 2016

Strtok Function in Loadrunner

Sometimes we want to capture all the values that match our specified delimiters and conditions but strtok function itself only captures the current value in the char token. Adding a sprintf function will allow you to store each of the values that you want to capture for later use (like choosing random) in different parameters.

Below is the code:
getMultipleValues(char *Chain){
 
    char separators[] = ":,";
    char * token, tempvalue [100];
    int i;
 
    extern char * strtok(char * stringconst char * delimiters );
 
    token = (char *)strtok(Chain, separators);
 
    while (token != NULL ) {

        if(strcmp(token,"\"ID\"")==0){
         
            token = (char *)strtok(NULL, separators);
         
             
        sprintf(tempvalue,"ProjectId_%d",i);
         
        lr_save_string(token,tempvalue);
     
        i++;
   
           }
     
        token = (char *)strtok(NULL, separators);
     
    }
Where you call the function: getMultipleValues(lr_eval_string("{ParamName}"));
Assuming the string Items{“ID”:29990, “Name”: TestName1, “OtherArguments”:Test1}{“ID”:29991, “Name”: TestName2, “OtherArguments”:Test2}{“ID”:29992, “Name”: TestName3, “OtherArguments”:Test3}
The output would be:
ProjectId_0 = 29990
ProjectId_1 = 29991

ProjectId_2 = 29992

No comments:

Post a Comment