云朵的冥想

导航

loadrunner飞机订票系统从登陆订票退票登出的脚本实现代码调试通过

在LR自带的飞机订票系统中,完整模拟一个用户从登陆->订票->退票->登出这样一个业务流程,分解每个事务为一个Action:

 进入首页->登陆->进入订票页面->选择出发城市到达城市->选择航班->付款->进入个人已订航班线路页面->取消航班->退出登陆。

这样子逻辑比较清晰,一个Action就是一个事务,对应到VuGen中创建脚本的Action参考下图:

 

1. 分析哪些内容需要进行关联,哪些需要进行参数化,要服务器提供出来的动态数据需要用关联函数进行关联,客户端提供给服务器的数据可以用参数化。

(1)登陆界面usersession每次打开页面服务器都给一个变化的动态码,需要用关联函数关联

web_reg_save_param("usersession","LB=userSession value=","RB=>",LAST);
//放在into_index()中

 

(2)出发城市到达城市等服务器可能更新会有变动,非固定不变的,需要进行关联

    web_reg_save_param("City","LB=>","RB=</option>","ORD=All",LAST);
    web_reg_save_param("seatPref","LB=seatPref\" value=\"","RB=\"","ORD=All",LAST);
    web_reg_save_param("seatType","LB=seatType\" value=\"","RB=\"","ORD=All",LAST);
//放入into_flight()中

 

(3)航班编号等信息为服务器反馈的数据,客户端无法自动生成,需要进行关联

    web_reg_save_param("outboundFlight","LB=outboundFlight value=","RB={departDate}","ORD=All",LAST);
//订票时待选择的航班班次,放入select_city()中
    web_reg_save_param("itinerary","LB=flightID\" value=\"","RB=\"",LAST);
//退票时选择的班次,放入into_itinerary()只

 

(4)启程时间返回时间是客户端提交给服务器端的数据,是用户自己选择的,我们可以采用参数化,启程时间固定为当前时间+1,返回时间固定为当前时间+2,参数化设置如下图,参数类型=Date/time,format=%m%d%Y,offset偏移量出发时间设置为1,返回时间设置为2.

 

2. 关联函数后获得的数据如何使用,使用方法有好几种,下边分别举例说明:

(1)获取到的城市列表使用LR自带的函数lr_paramarr_random来随机获取值,随机取到的值还不能直接在web_submit_data中使用,需要用sprintf数据拼接保存到变量的方式或者是采用lr_save_string来保存为参数:

采用lr_save_string的优点在于保存为参数后后边所有函数只要需要这个值就可以直接采用参数的方式调用“{}”

    char *arrivecity="London";
    //随机生成到达城市
    arrivecity=lr_paramarr_random("City");
//lr_save_string保存成参数 lr_save_string(arrivecity,
"arrivecity"); web_submit_data( ....... "Name=arrive", "Value={arrivecity}", ENDITEM, ....... )

 采用数据拼接保存到变量的方式需要对变量进行声明,如果后续函数有用到,需要把变量变成全局变量或者是更改变量声明位置到函数前面。

    char departcity[25];
    //随机生成到达城市
//数据拼接方式 sprintf(departcity,"Value=%s",lr_paramarr_random("City"));
        web_submit_data(
         .......
         "Name=depart", departcity, ENDITEM, 
         .......
         )

 

 (2)用下标随机的方式到关联数据中取值,代码量比使用LR自带函数要多,让"{City_i}"中的i随机。

    int i;
    char arrivecitys[10];
        char arrivecity[25];
    srand(time(NULL));
    i=rand()%atoi(lr_eval_string("{City_count}"))/2+1;
    sprintf(arrivecitys,"{City_%d}",i);
    sprintf(arrivecity,"Value=%s",lr_eval_string(arrivecitys));

 

 在随机选择关联函数关联出来的数据时:LR自带函数lr_paramarr_random()用起来比较简单,代码量少;采用下标随机的方式就会比较麻烦,代码复杂。

随机获取的数据怎么在web_submit_data中使用时:LR自带函数lr_save_string()可以把变量保存成参数,不用对参数进行声明,后续函数都可以使用此参数,比较方便;用sprintf数据拼接保存为变量的方式需要多声明字符串变量进行赋值,声明时需要考虑后续函数是否会用到此变量,会比较麻烦。

 

3. 接下来就把各个Action的源代码贴出来,供大家参考,所有代码均已调试通过:

into_index()
{

    web_reg_save_param("usersession","LB=userSession value=","RB=>",LAST);


    web_url("WebTours", 
        "URL=http://127.0.0.1:2080/WebTours/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t2.inf", 
        "Mode=HTML", 
        LAST);

    return 0;
}

 

submit_login()
{

    lr_think_time(3);


    web_reg_find("Text=Welcome","Savecount=welcome_count",LAST);

    web_submit_data("login.pl", 
        "Action=http://127.0.0.1:2080/WebTours/login.pl", 
        "Method=POST", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:1080/WebTours/nav.pl?in=home", 
        "Snapshot=t3.inf", 
        "Mode=HTML", 
        ITEMDATA, 
        "Name=userSession", "Value={usersession}", ENDITEM, 
        "Name=username", "Value=jojo", ENDITEM, 
        "Name=password", "Value=bean", ENDITEM, 
        "Name=JSFormSubmit", "Value=off", ENDITEM, 
        "Name=login.x", "Value=0", ENDITEM, 
        "Name=login.y", "Value=0", ENDITEM, 
        LAST);

    if(atoi(lr_eval_string("{welcome_count}"))>0)
    {
        lr_output_message("Total found \"Welcome\" %s times,login pass",lr_eval_string("{welcome_count}"));
        return 0;
    }
    else
    {
        lr_output_message("Total found \"Welcome\" %s times,login fail",lr_eval_string("{welcome_count}"));
        return -1;
    }


}

 

into_flight()
{

    lr_think_time(3);

    web_reg_save_param("City","LB=>","RB=</option>","ORD=All",LAST);
    web_reg_save_param("seatPref","LB=seatPref\" value=\"","RB=\"","ORD=All",LAST);
    web_reg_save_param("seatType","LB=seatType\" value=\"","RB=\"","ORD=All",LAST);

    web_url("welcome.pl", 
        "URL=http://127.0.0.1:2080/WebTours/welcome.pl?page=search", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:2080/WebTours/nav.pl?page=menu&in=home", 
        "Snapshot=t4.inf", 
        "Mode=HTML", 
        LAST);

    return 0;
}

 

select_city()
{

    char num_Passengers[1];
    char *arrivecity="London";
    char *departcity="Denver";
    char *seatPref="None";
    char *seatType="Coach";
    //随机生成乘客个数,从1~10.
    srand(time(NULL));
    itoa(rand() %10+1,num_Passengers,10);
    lr_save_string(num_Passengers,"numPassengers");
    //随机生成到达城市
    arrivecity=lr_paramarr_random("City");
    lr_save_string(arrivecity,"arrivecity");
    //随机生成出发城市,并判断如果跟到达城市相同重新生成
    departcity=lr_paramarr_random("City");
    //while(*arrivecity==*departcity)也可以使用
    while(strcmp(arrivecity,departcity)==0){
        departcity=lr_paramarr_random("City");
    }
    lr_save_string (departcity,"departcity");
    //生成座位
    seatPref=lr_paramarr_random("seatPref");
    lr_save_string(seatPref,"seatPref");
    seatType=lr_paramarr_random("seatType");
    lr_save_string(seatType,"seatType");




    web_reg_save_param("outboundFlight","LB=outboundFlight value=","RB={departDate}","ORD=All",LAST);

    lr_think_time(5);


    web_submit_data("reservations.pl", 
        "Action=http://127.0.0.1:2080/WebTours/reservations.pl", 
        "Method=POST", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:2080/WebTours/reservations.pl?page=welcome", 
        "Snapshot=t5.inf", 
        "Mode=HTML", 
        ITEMDATA, 
        "Name=advanceDiscount", "Value=0", ENDITEM, 
        "Name=depart", "Value={departcity}", ENDITEM, 
        "Name=departDate", "Value={departDate}", ENDITEM, 
        "Name=arrive", "Value={arrivecity}", ENDITEM, 
        "Name=returnDate", "Value={returnDate}", ENDITEM, 
        "Name=numPassengers", "Value={numPassengers}", ENDITEM, 
        "Name=seatPref", "Value={seatPref]", ENDITEM, 
        "Name=seatType", "Value={seatType}", ENDITEM, 
        "Name=.cgifields", "Value=roundtrip", ENDITEM, 
        "Name=.cgifields", "Value=seatType", ENDITEM, 
        "Name=.cgifields", "Value=seatPref", ENDITEM, 
        "Name=findFlights.x", "Value=37", ENDITEM, 
        "Name=findFlights.y", "Value=8", ENDITEM, 
        LAST);

    return 0;
}

 

select_flight_departing()
{

    char *outboundFlight;
    outboundFlight=lr_paramarr_random("outboundFlight");
    lr_save_string(outboundFlight,"outboundFlight");


    lr_think_time(5);


    web_submit_data("reservations.pl_2", 
        "Action=http://127.0.0.1:2080/WebTours/reservations.pl", 
        "Method=POST", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:2080/WebTours/reservations.pl", 
        "Snapshot=t6.inf", 
        "Mode=HTML", 
        ITEMDATA, 
        "Name=outboundFlight", "Value={outboundFlight}{departDate}", ENDITEM, 
        "Name=numPassengers", "Value={numPassengers}", ENDITEM, 
        "Name=advanceDiscount", "Value=0", ENDITEM, 
        "Name=seatType", "Value={seatType}", ENDITEM, 
        "Name=seatPref", "Value={seatPref}", ENDITEM, 
        "Name=reserveFlights.x", "Value=56", ENDITEM, 
        "Name=reserveFlights.y", "Value=5", ENDITEM, 
        LAST);

    return 0;
}

 

payment()
{

    lr_think_time(19);

    web_submit_data("reservations.pl_3", 
        "Action=http://127.0.0.1:2080/WebTours/reservations.pl", 
        "Method=POST", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:2080/WebTours/reservations.pl", 
        "Snapshot=t7.inf", 
        "Mode=HTML", 
        ITEMDATA, 
        "Name=firstName", "Value=Joseph", ENDITEM, 
        "Name=lastName", "Value=Marshall", ENDITEM, 
        "Name=address1", "Value=234 Willow Drive", ENDITEM, 
        "Name=address2", "Value=San Jose/CA/94085", ENDITEM, 
        "Name=pass1", "Value=Joseph Marshall", ENDITEM, 
        "Name=pass2", "Value=", ENDITEM, 
        "Name=pass3", "Value=", ENDITEM, 
        "Name=creditCard", "Value=", ENDITEM, 
        "Name=expDate", "Value=", ENDITEM, 
        "Name=oldCCOption", "Value=", ENDITEM, 
        "Name=numPassengers", "Value={numPassengers}", ENDITEM, 
        "Name=seatType", "Value={seatType}", ENDITEM, 
        "Name=seatPref", "Value={seatPref}", ENDITEM, 
        "Name=outboundFlight", "Value={outboundFlight}{departDate}", ENDITEM, 
        "Name=advanceDiscount", "Value=0", ENDITEM, 
        "Name=returnFlight", "Value=", ENDITEM, 
        "Name=JSFormSubmit", "Value=off", ENDITEM, 
        "Name=.cgifields", "Value=saveCC", ENDITEM, 
        "Name=buyFlights.x", "Value=50", ENDITEM, 
        "Name=buyFlights.y", "Value=14", ENDITEM, 
        LAST);

    return 0;
}

 

into_itinerary()
{

    web_reg_save_param("itinerary","LB=flightID\" value=\"","RB=\"",LAST);
    lr_think_time(4);

    web_url("welcome.pl", 
        "URL=http://127.0.0.1:2080/WebTours/welcome.pl?page=itinerary", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:2080/WebTours/nav.pl?page=menu&in=home", 
        "Snapshot=t14.inf", 
        "Mode=HTML", 
        LAST);

    return 0;
}

 

cancel_itinerary()
{

    web_reg_find("Text=No flights have been reserved","SaveCount=Cancel_Count",LAST);
    lr_think_time(6);

    web_submit_data("itinerary.pl", 
        "Action=http://127.0.0.1:2080/WebTours/itinerary.pl", 
        "Method=POST", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:2080/WebTours/itinerary.pl", 
        "Snapshot=t15.inf", 
        "Mode=HTML", 
        ITEMDATA, 
        "Name=flightID", "Value={itinerary}", ENDITEM, 
        "Name=.cgifields", "Value=1", ENDITEM, 
        "Name=removeAllFlights.x", "Value=43", ENDITEM, 
        "Name=removeAllFlights.y", "Value=15", ENDITEM, 
        LAST);
     if (atoi(lr_eval_string("{Cancel_Count}")) > 0){ 
         lr_output_message("Cancel successful."); 
         return 0;
        } 

     else{ 
        lr_error_message("Cancel Fail"); 
        return 1;
        } 


}

 

logout()
{

    lr_think_time(7);

    web_url("welcome.pl_2", 
        "URL=http://127.0.0.1:2080/WebTours/welcome.pl?signOff=1", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:2080/WebTours/nav.pl?page=menu&in=flights", 
        "Snapshot=t8.inf", 
        "Mode=HTML", 
        LAST);

    return 0;
}

 

上述代码都是使用LR自带函数来实现关联数据随机,lr_paramarr_random(),lr_save_string()变量参数化。

 

在select_city()中采用下标随机生成到达城市,采用sprintf()字符拼接保存到变量的方式应用到web_submit_data()中的代码,供参考:

char numPassengers[10];
char arrivecity[25];
char departcity[25];
char seatPref[15];
char seatType[16];

select_city()
{

    int num_Passengers;
    int i;
    char arrivecitys[10];

    //随机生成乘客个数,从1~10.
    srand(time(NULL));
    num_Passengers=rand()%10+1;

    sprintf(numPassengers,"Value=%d",num_Passengers);
    lr_output_message("numPassengers=%s",numPassengers);
    //随机生成到达城市
    //采用下标随机取值方法
    i=rand()%atoi(lr_eval_string("{City_count}"))/2+1;
    sprintf(arrivecitys,"{City_%d}",i);
    sprintf(arrivecity,"Value=%s",lr_eval_string(arrivecitys));
    lr_output_message("arrivecity=%s",arrivecity);

    //随机生成出发城市,并判断如果跟到达城市相同重新生成
    //采用LR自带函数随机取值方法
    sprintf(departcity,"Value=%s",lr_paramarr_random("City"));
    while(strcmp(arrivecity,departcity)==0){
        sprintf(departcity,"Value=%s",lr_paramarr_random("City"));
    }
    //生成座位类型
    sprintf(seatPref,"Value=%s",lr_paramarr_random("seatPref"));
    sprintf(seatType,"Value=%s",lr_paramarr_random("seatType"));




    web_reg_save_param("outboundFlight","LB=outboundFlight value=","RB={departDate}","ORD=All",LAST);

    lr_think_time(5);


    web_submit_data("reservations.pl", 
        "Action=http://127.0.0.1:2080/WebTours/reservations.pl", 
        "Method=POST", 
        "RecContentType=text/html", 
        "Referer=http://127.0.0.1:2080/WebTours/reservations.pl?page=welcome", 
        "Snapshot=t5.inf", 
        "Mode=HTML", 
        ITEMDATA, 
        "Name=advanceDiscount", "Value=0", ENDITEM, 
        "Name=depart", departcity, ENDITEM, 
        "Name=departDate", "Value={departDate}", ENDITEM, 
        "Name=arrive", arrivecity, ENDITEM, 
        "Name=returnDate", "Value={returnDate}", ENDITEM, 
        "Name=numPassengers", numPassengers, ENDITEM, 
        "Name=seatPref", seatPref, ENDITEM, 
        "Name=seatType", seatType, ENDITEM, 
        "Name=.cgifields", "Value=roundtrip", ENDITEM, 
        "Name=.cgifields", "Value=seatType", ENDITEM, 
        "Name=.cgifields", "Value=seatPref", ENDITEM, 
        "Name=findFlights.x", "Value=37", ENDITEM, 
        "Name=findFlights.y", "Value=8", ENDITEM, 
        LAST);

    return 0;
}

 

posted on 2019-01-17 01:11  springyun  阅读(1997)  评论(0编辑  收藏  举报