原创

Loadrunner各种类型脚本实例

1、Tudedo协议-xml报文

vuser_init

#include "FDK.h"

vuser_init()
{

    #define username "usrname=monitor"
    #define passwd "passwd=monitor"

    static char *env_allow_array[] = { 
        "",
        "",
        NULL
    }; 


    //WSNADDR=//10.153.179.85:8801,TUXDIR=E:\\tuxedo
    fdk_connect_tux(env_allow_array, lr_eval_string("{WSNADDR}"), lr_eval_string("{TUXDIR}"), username, passwd);
    //fdk_connect_tux(env_allow_array, lr_eval_string("{WSNADDR}"), lr_eval_string("{TUXDIR}"), username, passwd);

    lrt_abort_on_error(); //if has ERROR stop now !

    /*test WSNADDR and TUXDIR 
    *******************************
    lr_message("ns1 = %s", lr_eval_string("?"));
    lr_message("ns2 = %s", lr_eval_string("?"));
    *******************************
    */



    return 0;


}

Action

Action()
{

    char*  sendBuf;
    char*  recvBuf;
    long recvLen=0;
    int TotalLen;

    sendBuf = lrt_tpalloc("STRING", "", 10000);
    recvBuf = lrt_tpalloc("STRING", "", 10000);


    sprintf(sendBuf,lr_eval_string( "<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\" ?>\n"
                            "<ROOT>\n"
                            "<ACCTID type=\"string\"></ACCTID>\n"
                            "<ACCTTYPE type=\"string\"></ACCTTYPE>\n"
                            "<BEGINDATE type=\"string\">20150101</BEGINDATE>\n"
                            "<ENDDATE type=\"string\">20150510</ENDDATE>\n"
                            "<SVCNUM type=\"string\">13739248661</SVCNUM>\n"
                            "<DEMAND type=\"string\">0</DEMAND>\n"
                            "</ROOT>\n"
                                    ));


    lr_start_transaction("Qpayrec_缴费记录查询");

    //lr_message("报文 = %s",sendBuf);

    TotalLen = strlen(sendBuf);

    //lr_message("报文长度 = %d",TotalLen);

    tpresult_int = lrt_tpcall("Qpayrec", (char *)sendBuf, TotalLen, (char **)&recvBuf, &recvLen, 0);

    //lr_message("aaaaaaa%s", recvBuf);

    fdk_save_search_LRBound_string(recvBuf, "<RETURN_CODE type=\"long\">", "</RETURN_CODE>", "RespondCode");


    if(strcmp("000000", lr_eval_string ("{RespondCode_1}")) == 0) 
    {
        lr_end_transaction("Qpayrec_缴费记录查询", LR_AUTO);
    }
    else
    {
        lr_end_transaction("Qpayrec_缴费记录查询", LR_FAIL);
        //fdk_save_search_LRBound_string(recvBuf,"<RETURN_CODE type=\"long\">","</RETURN_CODE>","RETURN_CODE");
        lr_error_message("RETURN_CODE = %s\n;Id reciveBuf:\n%s", lr_eval_string("{RETURN_CODE_1}"), recvBuf);
    }

    lrt_tpfree(sendBuf);
    lrt_tpfree(recvBuf);
    free(sendBuf);
    free(recvBuf);

    return 0;
}

2、Tuxedo协议-json报文-嵌套

vuser_init

#include "FDK.h"

vuser_init()
{

    #define username "usrname=111111"
    #define passwd "passwd=111111"

    static char *env_allow_array[] = { 
        "",
        "",
        NULL
    }; 



    fdk_connect_tux(env_allow_array, lr_eval_string("{WSNADDR}"), lr_eval_string("{TUXDIR}"), username, passwd);

    lrt_abort_on_error(); //if has ERROR stop now !

    /*test WSNADDR and TUXDIR 
    *******************************
    lr_message("ns1 = %s", lr_eval_string("?"));
    lr_message("ns2 = %s", lr_eval_string("?"));
    *******************************
    */


//10.153.195.36:10551
    //return 0;


}

Action

Action()
{

    char*  sendBuf;
    char*  recvBuf;
    long recvLen=0;
    int TotalLen;

    sendBuf = lrt_tpalloc("STRING", "", 10000);
    recvBuf = lrt_tpalloc("STRING", "", 10000);


    //************************先调用获取业务流水***********************************/
    //json
    sprintf(sendBuf,lr_eval_string("{\"ROOT\":{\"UserAreaCode\":\"A\",\"OptType\":\"P\"}}")); 

    lr_start_transaction("getoptsn_获取流水");

    //lr_message("报文 = %s",sendBuf);

    TotalLen = strlen(sendBuf);

    //lr_message("报文长度 = %d",TotalLen);

    tpresult_int = lrt_tpcall("getoptsn", (char *)sendBuf, TotalLen, (char **)&recvBuf, &recvLen, 0);

    //lr_message("aaaaaaa%s", recvBuf);

    fdk_save_search_LRBound_string(recvBuf, "\"RETURN_CODE\":\"", "\",", "RespondCode");

    if(strcmp("000000", lr_eval_string ("{RespondCode_1}")) == 0) 
    {
        lr_end_transaction("getoptsn_获取流水", LR_AUTO);
    }
    else
    {
        lr_end_transaction("getoptsn_获取流水", LR_FAIL);
        lr_error_message("RETURN_CODE = %s\n;Id reciveBuf:\n%s", lr_eval_string("{RETURN_CODE_1}"), recvBuf);
    }

    //================获取业务流水,作为下次使用=======
    fdk_save_search_LRBound_string(recvBuf, "\"NewOptSn\":\"", "\"}", "RespondCodeSN");


    lr_save_var( lr_eval_string("{RespondCodeSN_1}"), 14, 0, "DateTime_1");

    //sprintf(sendBuf,lr_eval_string("{\"ROOT\":{\"ImSetAccount\":\"G\",\"LastOptTime\":\"{Current_time}{DateTime}\",\"Balance\":\"403.81\",\"LateFeeFavored\":\"1\",\"PayManner\":\"-1\",\"NoSMSFlag\":\"0\",\"GroupPayType\":\"0\",\"PayFlag\":\"\",\"PayNumStr\":\"\",\"TERMINALTYPE\":\"1\",\"PayPath\":\"0\",\"OptCode\":\"G\",\"INVCCOUNT\":\"1\",\"advanFlag\":\"\",\"INVCSTR1\":\"M2001    {RespondCodeSN_1}|安徽移动通信客户交费凭证|2013|12|25|潘霈晨|品牌标识:全球通|{Phone_NO}|||壹圆零角零分|1.00|项目|金额(元)|||本次交费|1.00|||||||||||上次结余|2006641.00|||本次结余|2006642.00|||||||||||||||||||||||||||||||||||截止到2013年06月20日已使用话费:0.00||||截止到2013年06月20日可用余额:2006642.00||||2013|12|01|2013|12|31|全椒县马厂镇黄庵佩武通信器材经营部|\",\"INVCSTRNEW1\":\"2013年06月20日|邮政通信业-电信|||收款方名称:中国移动通信集团安徽有限公司滁州分公司||||税务登记证号:341102743081745||||付款方名称:潘霈晨|品牌标识:全球通|||移动号码:{Phone_NO}||||合计金额:壹圆零角零分|¥ 1.00|||||||项目|金额(元)|||本次交费|1.00|||||||||||||||||||||||||||||||||||||||||||||||||||当前可用余额:2006642.00元||||流水号:{RespondCodeSN_1}|工号:M00000SYS|全椒县马厂镇黄庵佩武通信器材经营部||\",\"IN_DATA\":{\"DATA\":{\"DATA01\":\"1\",\"DATA02\":\"{RespondCodeSN_1}\",\"DATA03\":\"1\",\"DATA04\":\"{RespondCodeSN_1}\",\"DATA05\":\"\",\"DATA06\":\"\",\"DATA07\":\"\",\"DATA08\":\"111000001\",\"DATA09\":\"{ID_NO}\",\"DATA10\":\"{Contract_NO}\",\"DATA11\":\"001\",\"DATA12\":\"W1018884\",\"DATA13\":\"{Phone_NO}\",\"DATA14\":\"0.00\",\"DATA15\":\"0.00\",\"DATA16\":\"0.00\",\"DATA17\":\"5.00\",\"DATA18\":\"5.00\",\"DATA19\":\"{Current_time}{DateTime}\",\"DATA20\":\"20991231235959\",\"DATA21\":\"0\",\"DATA22\":\"F\",\"DATA23\":\"0\",\"DATA24\":\"A00000SYS\",\"DATA25\":\"111000000\",\"DATA26\":\"\",\"DATA27\":\"前台交费\",\"DATA28\":\"0\",\"DATA29\":\"\",\"DATA30\":\"1\",\"DATA31\":\"0\",\"DATA32\":\"1001\",\"DATA33\":\"0\",\"DATA34\":\"0\",\"DATA35\":\"5.00\",\"DATA36\":\"\",\"DATA37\":\"01020\",\"DATA38\":\"\",\"DATA39\":\"\",\"DATA40\":\"\",\"DATA41\":\"\",\"DATA42\":\"\",\"DATA43\":\"\",\"DATA44\":\"\"}}}}"));
    sprintf(sendBuf,lr_eval_string("{\"ROOT\":{\"ImSetAccount\":\"G\",\"LastOptTime\":\"{Current_time}{DateTime}\",\"Balance\":\"403.81\",\"LateFeeFavored\":\"1\",\"PayManner\":\"-1\",\"NoSMSFlag\":\"0\",\"GroupPayType\":\"0\",\"PayFlag\":\"\",\"PayNumStr\":\"\",\"TERMINALTYPE\":\"1\",\"PayPath\":\"0\",\"OptCode\":\"G\",\"INVCCOUNT\":\"1\",\"advanFlag\":\"\",\"INVCSTR1\":\"M2001    {RespondCodeSN_1}|安徽移动通信客户交费凭证|2013|12|25|潘霈晨|品牌标识:全球通|{Phone_NO}|||壹圆零角零分|1.00|项目|金额(元)|||本次交费|1.00|||||||||||上次结余|2006641.00|||本次结余|2006642.00|||||||||||||||||||||||||||||||||||截止到2013年06月20日已使用话费:0.00||||截止到2013年06月20日可用余额:2006642.00||||2013|12|01|2013|12|31|全椒县马厂镇黄庵佩武通信器材经营部|\",\"INVCSTRNEW1\":\"2013年06月20日|邮政通信业-电信|||收款方名称:中国移动通信集团安徽有限公司滁州分公司||||税务登记证号:341102743081745||||付款方名称:潘霈晨|品牌标识:全球通|||移动号码:{Phone_NO}||||合计金额:壹圆零角零分|¥ 1.00|||||||项目|金额(元)|||本次交费|1.00|||||||||||||||||||||||||||||||||||||||||||||||||||当前可用余额:2006642.00元||||流水号:{RespondCodeSN_1}|工号:M00000SYS|全椒县马厂镇黄庵佩武通信器材经营部||\",\"IN_DATA\":{\"DATA\":{\"DATA01\":\"1\",\"DATA02\":\"{RespondCodeSN_1}\",\"DATA03\":\"1\",\"DATA04\":\"{RespondCodeSN_1}\",\"DATA05\":\"\",\"DATA06\":\"\",\"DATA07\":\"\",\"DATA08\":\"111000001\",\"DATA09\":\"{Contract_NO}\",\"DATA10\":\"{ID_NO}\",\"DATA11\":\"001\",\"DATA12\":\"W1018884\",\"DATA13\":\"{Phone_NO}\",\"DATA14\":\"0.00\",\"DATA15\":\"0.00\",\"DATA16\":\"0.00\",\"DATA17\":\"5.00\",\"DATA18\":\"5.00\",\"DATA19\":\"{Current_time}{DateTime}\",\"DATA20\":\"20991231235959\",\"DATA21\":\"0\",\"DATA22\":\"F\",\"DATA23\":\"0\",\"DATA24\":\"A00000SYS\",\"DATA25\":\"111000000\",\"DATA26\":\"\",\"DATA27\":\"前台交费\",\"DATA28\":\"0\",\"DATA29\":\"\",\"DATA30\":\"1\",\"DATA31\":\"0\",\"DATA32\":\"1001\",\"DATA33\":\"0\",\"DATA34\":\"0\",\"DATA35\":\"5.00\",\"DATA36\":\"\",\"DATA37\":\"01020\",\"DATA38\":\"\",\"DATA39\":\"\",\"DATA40\":\"\",\"DATA41\":\"\",\"DATA42\":\"\",\"DATA43\":\"\",\"DATA44\":\"\"}}}}"));

    //************************开始交费***********************************/

    lr_start_transaction("jf000032_营业交费");

    //lr_message("报文 = %s",sendBuf);

    TotalLen = strlen(sendBuf);

    //lr_message("报文长度 = %d",TotalLen);

    tpresult_int = lrt_tpcall("jf000032", (char *)sendBuf, TotalLen, (char **)&recvBuf, &recvLen, 0);

    //lr_message("aaaaaaa%s", recvBuf);

    fdk_save_search_LRBound_string(recvBuf, "\"RETURN_CODE\":\"", "\",", "RespondCode");

    if(strcmp("000000", lr_eval_string ("{RespondCode_1}")) == 0) 
    {
        lr_end_transaction("jf000032_营业交费", LR_PASS);
    }
    else
    {
        lr_end_transaction("jf000032_营业交费", LR_FAIL);

        lr_error_message("RETURN_CODE = %s\n;Id reciveBuf:\n%s", lr_eval_string("{RETURN_CODE_1}"), recvBuf);
    }

    lrt_tpfree(sendBuf);
    lrt_tpfree(recvBuf);
    free(sendBuf);
    free(sendBuf);

    return 0;

}

3、WebService接口-json报文-Http调用

Action

Action()
{

    lr_start_transaction("QryNoInfoByNo_资源查号码信息");

    web_reg_save_param("Return_Code","LB=RETURN_CODE\":\"","RB=\",",LAST);

    web_custom_request("sGSQ_QueryScore", 
        //"URL=http://10.152.30.101:51300/esbWS/rest/MainService_QT",
         "URL=http://152.55.249.46:51000/esbWS/rest/MainService", 
        "Method=POST",
        "TargetFrame=", 
        "Resource=1", 
        "RecContentType=text/html", 
        "Referer=", 
        "Mode=HTML", 
        "EncType=application/json",
        "Body={ \"ROOT\": { \"REQUEST_METHOD\": \"QryNoInfoByNo\", \"BAT_ID\": \"\", \"SEND_TIME\": \"\", \"REQUEST_INFO\": { \"PHONE_NO\": \"{Phone_no}\", \"OP_CODE\": \"1000\", \"RES_STATUS\": \"0\", \"LOGIN_NO\": \"M3ABB0076\", \"CHECK_FLAG\": \"1\" } } }",
        LAST);

   // lr_message("RETURN_CODE = %s\n", lr_eval_string("{Return_Code}"));
    if(strcmp("0000", lr_eval_string ("{Return_Code}")) == 0) 
    {
        lr_end_transaction("QryNoInfoByNo_资源查号码信息", LR_AUTO);
    }
    else
    {
        lr_end_transaction("QryNoInfoByNo_资源查号码信息", LR_FAIL);
        lr_error_message("RETURN_CODE = %s\n", lr_eval_string("{Return_Code}"));
    }


    return 0;
}

4、Webservice接口-xml报文-http调用

Action

Action()
{

    lr_start_transaction("QryNoInfoByNo_资源查号码信息");

    web_reg_save_param("Return_Code","LB=RETURN_CODE type=\"string\">","RB=<",LAST);

    web_url("callService", 
        "URL=http://10.152.30.95:51000/esbWS/services/MainService/callService?pin=<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"no\" ?><ROOT>   <REQUEST_METHOD type=\"string\" >QryNoInfoByNo</REQUEST_METHOD><BAT_ID type=\"string\" ></BAT_ID>   <SEND_TIME type=\"string\" ></SEND_TIME> <REQUEST_INFO><PHONE_NO type=\"string\" >{Phone_no}</PHONE_NO><OP_CODE type=\"string\" >1000</OP_CODE><RES_STATUS type=\"string\" >0</RES_STATUS><LOGIN_NO type=\"string\" >M3ABB0076</LOGIN_NO><CHECK_FLAG  type=\"string\" >1</CHECK_FLAG></REQUEST_INFO></ROOT>", 
        "TargetFrame=", 
        "Resource=1", 
        "RecContentType=application/xml", 
        "Referer=", 
        "Snapshot=t1.inf", 
        LAST);

    // lr_message("RETURN_CODE = %s\n", lr_eval_string("{Return_Code}"));

    if(strcmp("0000", lr_eval_string ("{Return_Code}")) == 0) 
    {
            lr_end_transaction("QryNoInfoByNo_资源查号码信息", LR_AUTO);
    }
    else
    {
            lr_end_transaction("QryNoInfoByNo_资源查号码信息", LR_FAIL);
            lr_error_message("RETURN_CODE = %s\n", lr_eval_string("{Return_Code}"));
    }

    return 0;
}

5、WebService接口-Soap调用

需要导入wsdl文件或者地址
Action

#include "FDK.h"
Action()
{

    lr_start_transaction("QuickPayment");

    web_service_call( "StepName=callService_101",
        "SOAPMethod=QuickPayment|QuickPaymentHttpSoap11Endpoint|callService",
        "ResponseParam=response",
        "Service=QuickPayment",
        "ExpectedResponse=SoapResult",
        "Snapshot=t1431587102.inf",
        BEGIN_ARGUMENTS,
        "pin=<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                        "<ROOT>\n"
                            "<STATUS type=\"string\">D</STATUS>\n"
                            "<ORDER_ID type=\"string\">{date_time}{random}</ORDER_ID>\n"
                            "<ACJFCODE type=\"string\">ACJF10902</ACJFCODE>\n"
                            "<ACJFSEQ type=\"string\">01</ACJFSEQ>\n"
                            "<PAY_CHN type=\"string\">09</PAY_CHN>\n"
                            "<BANK_CODE type=\"string\">ACJF10902</BANK_CODE>\n"
                            "<LOGIN_NO type=\"string\">{Login_no}</LOGIN_NO>\n"
                        "<REGIN_NO type=\"string\"/>\n"
                        "<OPT_CODE type=\"string\"/>\n"
                        "<PERCENT type=\"string\">99</PERCENT>\n"
                        "<PAY_PERSON type=\"string\">{Phone_no}</PAY_PERSON>\n"
                        "<PAY_PERSON_TYPE type=\"string\">1</PAY_PERSON_TYPE>\n"
                        "<ISROLLBACK type=\"string\"/>\n"
                        "<IN_DATA>\n"
                            "<DATA>\n"
                                "<FOREIGNSN type=\"string\">{date_time}{random}</FOREIGNSN>\n"
                                "<FOREIGNTIME type=\"string\">20150513192520</FOREIGNTIME>\n"
                                "<PAY_ID type=\"string\">{Phone_no}</PAY_ID>\n"
                                "<PAY_ID_TYPE type=\"string\">1</PAY_ID_TYPE>\n"
                                "<PAY_AMOUNT type=\"string\">5000</PAY_AMOUNT>\n"
                            "<DISCOUNT type=\"string\"/>\n"
                        "<CHARGEMONEY type=\"string\"/>\n"
                        "<ACTIVITYNO type=\"string\"/>\n"
                        "<PRODUCTNO type=\"string\"/>\n"
                        "<RESERVE1 type=\"string\"/>\n"
                        "<RESERVE2 type=\"string\"/>\n"
                        "<RESERVE3 type=\"string\"/>\n"
                        "<RESERVE4 type=\"string\"/>\n"
                        "<RESERVE5 type=\"string\"/>\n"
                        "</DATA>\n"
                        "</IN_DATA>\n"
                        "</ROOT>\n",


        END_ARGUMENTS,


        BEGIN_RESULT,
        "return=Param_return_Code",
        END_RESULT,
        LAST);

        //lr_log_message("Param_return_Code=%s", lr_eval_string("{Param_return_Code}"));

        fdk_save_search_LRBound_string(lr_eval_string("{Param_return_Code}"), "RETURN_CODE type=\"long\">", "</RETURN_CODE>", "Return_Code");

        //lr_log_message("Return_Code=%s", lr_eval_string("{Return_Code_1}"));

        //判断返回是否正确
        if(strcmp("000000", lr_eval_string ("{Return_Code_1}"))==0)
        {
            lr_end_transaction("QuickPayment", LR_AUTO);
        }
        else
        {
            lr_end_transaction("QuickPayment", LR_FAIL);
            //fdk_save_search_LRBound_string(recvBuf,"<RETURN_CODE type=\"long\">","</RETURN_CODE>","RETURN_CODE");
            lr_error_message("Id reciveBuf:\n%s", lr_eval_string("{Param_return_Code}"));
        }


    return 0;
}

6、Rest请求-Http调用

Action

Action()
{

    lr_start_transaction("QryNoInfoByNo_资源查号码信息");

    web_reg_save_param("Return_Code","LB=RETURN_CODE\":\"","RB=\",",LAST);

    web_custom_request("sGSQ_QueryScore", 
        //"URL=http://10.152.30.101:51300/esbWS/rest/MainService_QT",
         "URL=http://152.55.249.46:51000/esbWS/rest/MainService", 
        "Method=POST",
        "TargetFrame=", 
        "Resource=1", 
        "RecContentType=text/html", 
        "Referer=", 
        "Mode=HTML", 
        "EncType=application/json",
        "Body={ \"ROOT\": { \"REQUEST_METHOD\": \"QryNoInfoByNo\", \"BAT_ID\": \"\", \"SEND_TIME\": \"\", \"REQUEST_INFO\": { \"PHONE_NO\": \"{Phone_no}\", \"OP_CODE\": \"1000\", \"RES_STATUS\": \"0\", \"LOGIN_NO\": \"M3ABB0076\", \"CHECK_FLAG\": \"1\" } } }",
        LAST);

   // lr_message("RETURN_CODE = %s\n", lr_eval_string("{Return_Code}"));
    if(strcmp("0000", lr_eval_string ("{Return_Code}")) == 0) 
    {
        lr_end_transaction("QryNoInfoByNo_资源查号码信息", LR_AUTO);
    }
    else
    {
        lr_end_transaction("QryNoInfoByNo_资源查号码信息", LR_FAIL);
        lr_error_message("RETURN_CODE = %s\n", lr_eval_string("{Return_Code}"));
    }


    return 0;
}

7、Socket接口

Action

#include "lrs.h"


Action()
{
    char *recvbuf;
    int revclen=0;
    int rc;
    lr_start_transaction("TEST");

    //创建socket连接
    rc=lrs_create_socket("socket0", "TCP", "RemoteHost=10.243.29.114:12130",  LrsLastArg);

    if(rc==0){

        lr_output_message("Socket was successfully created");
    }

    else{

        lr_output_message("An error occurred while creating the socket,Error Code:%d",rc);

    }


    // 发送消息
    lrs_send("socket0", "buf0", LrsLastArg);
    lrs_receive("socket0", "buf1",  LrsLastArg); 
    lrs_get_last_received_buffer("socket0",&recvbuf,&revclen);

    //根据字节数参数化取值
    //lrs_save_param_ex("socket0", "received", "buf1", 87, 6,"ebcdic", "param_1_ex"); 

    //根据左右边界参数化取值
    lrs_save_searched_string ("socket0", NULL, "param_1_ex", "LB/BIN=~!~", "RB/BIN=~!~", 1, 0, -1); 

    if(strcmp("0000", lr_eval_string ("<param_1_ex>")) == 0){

        lrs_close_socket("socket0");
        lr_end_transaction("TEST", LR_AUTO);
    }
    else
    {
            lr_end_transaction("TEST", LR_FAIL);
            lr_error_message("errorinfo:%s", recvbuf);

    }

    lrs_free_buffer(recvbuf);

    return 0;
}

data.ws

;WSRData 2 1

send buf0 5
    "000209~!~01~!~01~!~L<Ram_No>8621<Time>~!~13655536203~!~12310006999867~!~12~!~00~!~S01~!~101~!~M0AAA0H01~!~~!~38~!~~!~60016~!~10000~!~~!~1~!~12288~!~2500~!~U1~!~20161001000000~!~20161214235959~!~1~!~LLBP0820~!~"
recv buf1 908
""


-1

8、WEB-HTTP/HTML脚本

Action

Action()
{
    lr_start_transaction("秒杀业务");


    web_add_cookie("WEBTRENDS_ID=11.11.11.11-1477658331.175031; DOMAIN=www.xx.xx.cn");

    web_add_cookie("WT_FPC=id=202c8214ec2beda87021477658706449:lv=1477658716342:ss=1477658706449; DOMAIN=www.xx.xx.cn");

    web_url("zd_title_1.html", 
        "URL=http://act.xxxxx.cn:8000/activity/deploy/CPMS/flash/AC20161028194926/zd_title_1.html", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t8.inf", 
        "Mode=HTML", 
        EXTRARES, 
        "Url=style/images/qg_title_bg.png", ENDITEM, 
        "Url=images/gotop.png?t=1477673349787", ENDITEM, 
        "Url=http://www.xx.xx.cn/webtrends/dcs.gif?&dcsdat=1477673349772&dcssip=act.xxxxx.cn&dcsuri=/activity/deploy/CPMS/flash/AC20161028194926/zd_title_1.html&WT.co_f=20f9a3aab89725ce2941477673350421&WT.Branch=sc_cp&WT.es=http%253A%252F%252Fact.xxxxx.cn%253A8000%252Factivity%252Fdeploy%252FCPMS%252Fflash%252FAC20161028194926%252Fzd_title_1.html&WT.host=act.xxxxx.cn&WT.vt_f=2", ENDITEM, 
        "Url=http://www.xx.xx.cn/webtrends/dcs.gif?&dcsdat=1477673356128&dcssip=act.xxxxx.cn&dcsuri=/activity/deploy/CPMS/flash/AC20161028194926/zd_title_1.html&WT.co_f=26f950d7975ab46cea91477673356128&WT.Branch=sc_cp&WT.es=http%253A%252F%252Fact.xxxxx.cn%253A8000%252Factivity%252Fdeploy%252FCPMS%252Fflash%252FAC20161028194926%252Fzd_title_1.html&WT.host=act.xxxxx.cn&WT.vt_f=2&WT.event=JavaScript%253A%25E9%25A9%25AC%25E4%25B8%258A%25E6%258A%25A2&WT.dl=22&WT.nv=scroll_content_20161028235105&"
        "WT.pos=277x147", ENDITEM, 
        "Url=http://www.xx.xx.cn/webtrends/dcs.gif?&dcsdat=1477673356130&dcssip=act.xxxxx.cn&dcsuri=/activity/deploy/CPMS/flash/AC20161028194926/zd_title_1.html&WT.co_f=2b46c8f9c3ecb93f2411477673356130&WT.Branch=sc_cp&WT.es=http%253A%252F%252Fact.xxxxx.cn%253A8000%252Factivity%252Fdeploy%252FCPMS%252Fflash%252FAC20161028194926%252Fzd_title_1.html&WT.host=act.xxxxx.cn&WT.vt_f=2&WT.event=%2520msq&WT.nv=xsms20160527", ENDITEM, 
        "Url=http://www.xx.xx.cn/webtrends/dcs.gif?&dcsdat=1477673361908&dcssip=act.xxxxx.cn&dcsuri=/activity/deploy/CPMS/flash/AC20161028194926/zd_title_1.html&WT.co_f=24af6782512b7292aae1477673361908&WT.Branch=sc_cp&WT.es=http%253A%252F%252Fact.xxxxx.cn%253A8000%252Factivity%252Fdeploy%252FCPMS%252Fflash%252FAC20161028194926%252Fzd_title_1.html&WT.host=act.xxxxx.cn&WT.vt_f=2&WT.event=JavaScript%253A%25E7%25A1%25AE%25E8%25AE%25A4&WT.dl=22&WT.nv=pop-1&WT.pos=151x197", ENDITEM, 
        LAST);

    web_submit_data("getStockInfo.do", 
        "Action=http://act.xxxxx.cn:8000/activity/flash/getStockInfo.do?rd=1477673349885", 
        "Method=POST", 
        "RecContentType=text/html", 
        "Referer=http://act.xxxxx.cn:8000/activity/deploy/CPMS/flash/AC20161028194926/zd_title_1.html", 
        "Snapshot=t9.inf", 
        "Mode=HTML", 
        ITEMDATA, 
        "Name=actId", "Value=AC20161028194926", ENDITEM, 
        "Name=periodId", "Value=05c0c6e3ddfe460e90ea34c00c175b4d", ENDITEM, 
        "Name=acProductId", "Value=a1497ecfe6944819b23f2cadfef21902", ENDITEM, 
        EXTRARES, 
        "Url=http://download1.operacdn.com/res/servicefiles/browserjsfiles/mobile/browserjs-Mobile-12.10-20160602.js", "Referer=", ENDITEM, 
        LAST);

    //{"code":"000000",
    web_reg_save_param("Return_Code","LB=\"code\":\"","RB=\",",LAST);


    lr_rendezvous("秒杀开始");


    lr_start_transaction("点击抢购");


    web_custom_request("index.do", 
        "URL=http://act.xxxxx.cn:8000/activity/flash/index.do?rd=1477673356130&actId=AC20161028194926&platformId=CPMS&productId=a1497ecfe6944819b23f2cadfef21902&keyWords=Unlimited&acType=flash&operatorType=0&productType=QTDD", 
        "Method=POST", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://act.xxxxx.cn:8000/activity/deploy/CPMS/flash/AC20161028194926/zd_title_1.html", 
        "Snapshot=t11.inf", 
        "Mode=HTML", 
        "EncType=", 
        LAST);

    if(strcmp("000000", lr_eval_string ("{Return_Code}")) == 0) 
        {
            lr_end_transaction("点击抢购", LR_PASS);
        }
        else
        {
            lr_end_transaction("点击抢购", LR_FAIL);

            lr_error_message("RETURN_CODE = %s\n", lr_eval_string("{Return_Code}"));
        }

    lr_end_transaction("秒杀业务", LR_AUTO);

    return 0;
}

9、WEB-HTTP/HTM java脚本

Action


/*
 * LoadRunner Java script. (Build: _build_number_)
 * 
 * Script Description: 
 *                     
 */

import lrapi.*;

public class Actions
{

    public int init() throws Throwable {
        return 0;
    }//end of init


    public int action() throws Throwable {
        String url = "http://localhost:8080/AutoTest/";

        lr.start_transaction("登录");

        web.reg_save_param("return_code_login",new String[]{
                           "LB={\"returnCode\":",
                           "RB=","SaveOffset=0","SaveLen=1",
                           "LAST"});

        web.submit_data("loginAction",
                       "Action="+url+"user-toLogin",
                       new String[]{
                       "Method=POST",
                       "RecContentType=application/json",
                       "Mode=HTML"
                       },
                       new String[]{
                       "Name=username","Value=admin",web.ENDITEM,
                       "Name=password","Value=q708162543",web.ENDITEM,
                       web.LAST
                       });

        if(lr.eval_string("{return_code_login}").equals("0")){
            lr.end_transaction("登录",lr.AUTO);
        }else{
            lr.error_message("登录失败:returnCode="+lr.eval_string("{return_code_login}"));
            lr.end_transaction("登录",lr.FAIL);
        }


        lr.start_transaction("获取登录信息");
        web.reg_save_param("return_code",new String[]{
                           "LB={\"returnCode\":",
                           "RB=","SaveOffset=0","SaveLen=1",
                           "LAST"});
        web.url("localhost", 
                "URL="+url+"user-getLoginUserInfo", 
                new String[]{ 
                    "TargetFrame=", 
                    //"TargetBrowser=Mercury Technologies", 
                    "Resource=0", 
                    "RecContentType=application/json", 
                    "Snapshot=t1.inf", 
                    "Mode=HTML", 
                    "LAST"});

        if(lr.eval_string("{return_code}").equals("0")){
            lr.end_transaction("获取登录信息", lr.AUTO);
        }else{
            lr.error_message(lr.eval_string("{return_code}"));
            lr.end_transaction("获取登录信息", lr.FAIL);
        }    

        return 0;
    }//end of action


    public int end() throws Throwable {
        return 0;
    }//end of end
}

附:共用函数库 FDK.h

```
/*公用函数酷.


  • 程序名称: Fin Developer's Kit
  • 当前版本: 2010-03-16 V1.7 beta
  • 程序说明: 函数酷
  • 开发作者: 李锋
  • 更新记录:
          1 .加入函数 fdk_save_search_LRBound_string
          2 .加入函数 CreatData_CONTACT_ID_14
          3 .加入函数 fdk_socket_SendRecv
          4 .加入函数 fdk_string_equals
          5 .加入函数 fdk_out_message_to16
          6 .修改函数 CreatData_CONTACT_ID_14 增加新交易代码 GZL7 长度14位
          7 .修改函数 CreatData_CONTACT_ID_14 增加新交易代码 GRW1 长度14位
          8 .加入函数 fdk_connect_tux
          9 .加入函数 fdk_substring_parm
          10.修改函数 CreatData_CONTACT_ID_14 增加新交易代码 GJF1 长度25位
          11.加入函数 fdk_floatadd_dotwo
          12.加入函数 fdk_initFile
    

*/

/Include Files/

#include "lrt.h"

#include "lrun.h"

#include "web_api.h"

#include "lrw_custom_body.h"

#include "lrs.h"

/Global Variables/

/ DISPATCH INVOKE FUNCTIONS/
void fdk_save_search_LRBound_string(char buf, char leftBound, char rightBound, char PreFix);
void CreatData_CONTACT_ID_14(char OPTCODE, char** CONTACT_ID);
int fdk_socket_SendRecv(char
src_socket, char buffer, int length, char** pRebuffer);
int fdk_string_equals(char
_string1, char _string2);
void fdk_out_message_to16(void
_hexbuf, int _bufsize);
void fdk_connect_tux(char env_allow_array[],char WSNADDR, char TUXDIR, char username, char passwd);
fdk_substring_parm(char
str, int i, int j, char parm);
double atof (const char
string);
void fdk_floatadd_dotwo(char initBalance, float addvalues, char paramName);
int fdk_writeFile(char inputData, char filename);
int fdk_initFile(char paramName, char filename);

/ FUNCTIONS /
int fdk_writeFile(char inputData, char filename)
{
/**
说明:向文件里追加数据 ">>" 方式
参数: inputData 写入的数据
filename 文件名
copyright 2010/03/16 by Fin.
*/
long fp;

    //写文件。。。
    if ( (fp = fopen(filename, "at+b")) == NULL)
    {
        lr_message("open logfile failed!\n");
        return -1;
    }
    fprintf(fp, "%s", inputData);

    fclose(fp);

    return 0;

}

int fdk_initFile(char paramName, char filename)
{
/**
说明:向文件里追加数据 ">" 方式
参数: inputData 写入的数据
filename 文件名
copyright 2010/03/16 by Fin.
*/
long fp;

    //写文件。。。
    if ( (fp = fopen(filename, "w+b")) == NULL)
    {
        lr_message("open logfile failed!\n");
        return -1;
    }
    fprintf(fp, "%s", paramName);

    fclose(fp);

    return 0;

}

fdk_substring_parm(char str, int i, int j, char parm)
{
/**
说明:截取字符串中第i 位开始的长度为j的字符串并保存到参数parm中
参数: str = 原始字符串 i = N位开始
j = 位结束 parm = 参数名
copyright 2010/02/03 by Fin.
*/
lr_save_var(str + i, j, 0, parm);
}
void fdk_connect_tux(char env_allow_array[],char WSNADDR, char TUXDIR, char username, char passwd)
{
/**
说明:连接TUX文件句柄
参数:
env_allow_array 句柄结构
WSNADDR = wsnaddr TUXDIR = TUXDIR
username = username passwd = passwd
copyright 2010/02/02 by Fin.
*/
lrt_set_env_list(env_allow_array);

lrt_tuxputenv(WSNADDR);
lrt_tuxputenv(TUXDIR);

tpresult_int = lrt_tpinitialize(username, passwd, LRT_END_OF_PARMS);

lr_message("connect to Tuxedo's flag was %d..",tpresult_int);

//lrt_abort_on_error(); //if has ERROR stop now !

}

void fdk_save_search_LRBound_string(char buf, char leftBound, char rightBound, char PreFix)
{
/**
说明:搜索字符串,通过前后匹配的方式
参数: buf = 原字符串 leftBound = 左边界
rightBound = 右边界 PreFix = 输出参数集
copyright 2009/07/07 by Fin.
*/
int i,j,k,intTmp, offsetA, offsetB;
char strTmpA;
char
strTmpB;
char strTmpC;
char
strTmpD;
char strPos;
intTmp = strlen(buf) + 1;
if ((strTmpA = (char
)malloc(intTmp sizeof(char))) == NULL) {
lr_output_message("Insufficient memory available");
return;
}
if ((strTmpB = (char
)malloc(intTmp sizeof(char))) == NULL) {
lr_output_message("Insufficient memory available");
return;
}
if ((strTmpC = (char
)malloc(intTmp sizeof(char))) == NULL) {
lr_output_message("Insufficient memory available");
return;
}
if ((strTmpD = (char
)malloc(intTmp sizeof(char))) == NULL) {
lr_output_message("Insufficient memory available");
return;
}
i = 0;
strPos = (char
)strstr(buf, leftBound);
if(strPos != NULL){
offsetA = (int)(strPos - buf);
}
else offsetA = -1;
strPos = (char *)strstr(buf, rightBound);
if(strPos != NULL) offsetB = (int)(strPos - buf);
else offsetB = -1;
if((offsetA < 0)||(offsetB < 0)){
sprintf(strTmpC, "%s_count", PreFix);
lr_savestring ("0", strTmpC);
free(strTmpA);
free(strTmpB);
free(strTmpC);
free(strTmpD);
return;
}
if(offsetA < offsetB){
memcpy(strTmpA, buf + offsetA + strlen(leftBound), offsetB - offsetA - strlen(leftBound));
sprintf(strTmpA + offsetB - offsetA - strlen(leftBound), "\x0");
i++;
sprintf(strTmpC, "%s
%d", PreFix, i);
lr_save_string (strTmpA, strTmpC);
if(strlen(buf) > (offsetB + strlen(rightBound))){
memcpy(strTmpB, buf + offsetB + strlen(rightBound), strlen(buf) - offsetB - strlen(rightBound));
sprintf(strTmpB + strlen(buf) - offsetB - strlen(rightBound), "\x0");
}
else sprintf(strTmpB, "");
}
if(offsetA > offsetB){
memcpy(strTmpB, buf + offsetB + strlen(rightBound), strlen(buf) - offsetB - strlen(rightBound));
sprintf(strTmpB + strlen(buf) - offsetB - strlen(rightBound), "\x0");
}

 while(strcmp(strTmpB, "") != 0){
  strPos = (char *)strstr(strTmpB, leftBound);
  if(strPos != NULL) offsetA = (int)(strPos - strTmpB);
        else offsetA = -1;
  strPos = (char *)strstr(strTmpB, rightBound);
  if(strPos != NULL) offsetB = (int)(strPos - strTmpB);
  else offsetB = -1;
  if((offsetA < 0)||(offsetB < 0)){
   sprintf(strTmpC, "%s_count", PreFix);
   sprintf(strTmpD, "%d", i);
   lr_save_string (strTmpD, strTmpC);
   free(strTmpA);
   free(strTmpB);
   free(strTmpC);
   free(strTmpD);
   return;
  }
  if(offsetA < offsetB){
   memcpy(strTmpA, strTmpB + offsetA + strlen(leftBound), offsetB - offsetA - strlen(leftBound));
   sprintf(strTmpA + offsetB - offsetA - strlen(leftBound), "\x0");
   i++;
   sprintf(strTmpC, "%s_%d", PreFix, i);
   lr_save_string (strTmpA, strTmpC);
   sprintf(strTmpC, "%s_count", PreFix);
   sprintf(strTmpD, "%d", i);
   lr_save_string (strTmpD, strTmpC);
   if(strlen(buf) > (offsetB + strlen(rightBound))){
    memcpy(strTmpC, strTmpB + offsetB + strlen(rightBound), strlen(strTmpB) - offsetB - strlen(rightBound));
    sprintf(strTmpC + strlen(strTmpB) - offsetB - strlen(rightBound), "\x0");
   }
   else sprintf(strTmpC, "");
  }
  if(offsetA > offsetB){
   memcpy(strTmpC, strTmpB + offsetB + strlen(rightBound), strlen(strTmpB) - offsetB - strlen(rightBound));
   sprintf(strTmpC + strlen(strTmpB) - offsetB - strlen(rightBound), "\x0");
  }
  sprintf(strTmpB, "%s", strTmpC);

 }
 free(strTmpA);
 free(strTmpB);
 free(strTmpC);
 free(strTmpD);
return;

}

void CreatData_CONTACT_ID_14(char OPTCODE, char CONTACT_ID)
{
/**

说明: 无重复输出14位长度的数字
参数: OPTCODE = 交易代码(GKG1)
CONTACT_ID = 输出变量
copyright 2010/01/07 by Fin.
**
/
//55000000002584
//20100122193125000-045727-PM
char stn;
char
begin_x;
char end_x;
char x[20];
char
token;

    token = (char*)strtok(lr_eval_string("{_Time}"),".");
    begin_x = token;        
    token = (char*)strtok(NULL, ".");
    end_x = token;
    //lr_message("begin_x = %s, end_x = %s",begin_x,end_x);

    strcpy(x, begin_x);
    strcat(x,end_x);
    strcat(x,lr_eval_string("{Ran}"));
    //lr_message("x = %s", x);
    *CONTACT_ID = (char*)malloc(sizeof(char) * 30); 

    if(stricmp(OPTCODE, "GJF1") == 0)
    {
        strcpy(*CONTACT_ID, x);
        strcat(*CONTACT_ID,"PM");
        *(*CONTACT_ID + 25) = '\0';
    }
    if(stricmp(OPTCODE, "GKG1") == 0)
    {
        strcpy(*CONTACT_ID, "88");
        strcat(*CONTACT_ID, x);
        *(*CONTACT_ID + 14) = '\0';

    }
    if(stricmp(OPTCODE, "GKG2") == 0)
    {
        strcpy(*CONTACT_ID, "99");
        strcat(*CONTACT_ID, x);
        *(*CONTACT_ID + 14) = '\0';

    }
    if(stricmp(OPTCODE, "GZL7") == 0)
    {
        strcpy(*CONTACT_ID, "77");
        strcat(*CONTACT_ID, x);
        *(*CONTACT_ID + 14) = '\0';

    }
    if(stricmp(OPTCODE, "GRW1") == 0)  
    {
        strcpy(*CONTACT_ID, "28");
        strcat(*CONTACT_ID, x);
        *(*CONTACT_ID + 14) = '\0';

    }
}

int fdk_socket_SendRecv(char src_socket, char buffer, int length, char pRebuffer)
{
/**

说明: 发送接收报文
参数: src_socket = 连接句柄名称 buffer = 发送报文
length = 报文长度
pRebuffer = 返回报文
copyright 2010/01/19 by Fin.
*
/
char *recbuf;
int recsize;

/* 发送报文 
 */
lrs_set_send_buffer(src_socket, buffer, strlen(buffer));
lrs_length_send(src_socket, "buf0", 0, LrsLastArg);

/* 
接收服务器返回的消息包 
*/

// 设置接收超时 
lrs_set_recv_timeout(30,0);
lrs_set_recv_timeout2(0,500);
lrs_length_receive(src_socket, "buf1", 0, LrsLastArg);
lrs_get_last_received_buffer(src_socket, &recbuf, &recsize);

if(recsize > 0)
{
    *pRebuffer = (char*) malloc(sizeof(char)*recsize + 1);
    memcpy(*pRebuffer, recbuf, recsize);
    lrs_free_buffer(recbuf);
    *(*pRebuffer+recsize) = '\0';

    return 0;

}else
{
    lr_message("warning: Receive size is 0.");
    return -1;
}

}

int fdk_string_equals(char _string1, char _string2)
{
/**
说明:比较字符串,相同返回0 不相同返回 -1
参数: _string1 = 字符串1 _string2 = 字符串2
copyright 2009/01/20 by Fin.
*/

int flag;

flag = strcmp(_string1, _string2);
if(flag == 0)
{
    return 0;
}

return -1;

}

void fdk_out_message_to16(void _hexbuf, int _bufsize)
{
/**
说明:16进制方式方式打印出参数内容
参数: _hexbuf = string
_bufsize = 3 (字符串长度)
copyright 2009/07/07 by Fin.
**
/
int _i;
unsigned char _buf_str;
unsigned char
_x;

_buf_str = (unsigned char *)malloc(_bufsize * 4 + 1);
_x = _buf_str;    
memset(_x, 0, _bufsize*4+1);

for(_i = 0; _i < _bufsize; _i++)
{    
    sprintf(_x, "%x ",((unsigned char *)_hexbuf)[_i]);
    _x = _x+3;
}

lr_message("[Fin] =>(to16) %s = #%s# \n", _hexbuf, _buf_str);

free
正文到此结束
本文目录