LoadRunner中自定义C函数实现字符串替换

1.在globals.h 中定义一个函数ReplaceStr,实现字符串的替换:
 
int ReplaceStr(char* sSrc, char* sMatchStr, char* sReplaceStr)
{
        int StringLen;
        char caNewString[1024];
        char* findPos;
        merc_timer_handle_t timer_ReplaceStr = lr_start_timer();
 
        lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG ,"Notify:Function 'ReplaceStr' started.");
        findPos =(char *)strstr(sSrc, sMatchStr);
        if( (!findPos) || (!sMatchStr) ){
            lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG ,"Notify:Function 'ReplaceStr' ended with error!");
            return -1;
        }               
 
        while( findPos)
        {
                memset(caNewString, 0, sizeof(caNewString));
                StringLen = findPos - sSrc;
                strncpy(caNewString, sSrc, StringLen);
                strcat(caNewString, sReplaceStr);
                strcat(caNewString, findPos + strlen(sMatchStr));
                strcpy(sSrc, caNewString);
 
                findPos =(char *)strstr(sSrc, sMatchStr);
        }
        lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG,"Result:%s",sSrc);
        free(findPos);
        lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG ,"Notify:Function 'ReplaceStr' ended (Duration: %lf)",
                         lr_end_timer(timer_ReplaceStr));
        return 0;
}
 
 
 
2.在init/Action/end 中调用:
 
char strView[1024];
 
 
  web_reg_save_param("viewState5",
        "LB=\"javax.faces.ViewState\" value=\"",
        "RB=\"",
        LAST);
 
sprintf(strView,lr_eval_string("{viewState5}"));
 
 ReplaceStr(strView,"+","+");
 ReplaceStr(strView,"/","/");
 ReplaceStr(strView,"=","-");
 
  lr_save_string(lr_eval_string(strView), "viewState6");
 
memset(strView,0,sizeof(strView));

 

--------------------------------------------------------------------------------

关注微信公众号(测试工程师小站)即可在手机上查阅,并可接收更多测试分享,发送【测试资料】更可获取百G测试教程~

posted @ 2016-08-15 23:02  ☆星空物语☆  阅读(300)  评论(0编辑  收藏  举报