首先还是录制脚本,将脚本录制完成后进行如下分析
1、航班的起始地址和目的地是从服务器端传过来的,这就表明我们不能使用参数化的 方法,而是使用关联的方法。关联方法就像一个盒子,他把服务器方发送来的信息放在盒子内,在LoadRuuner运行时会从服务器端取出信息,在进行提 交。例如登陆页面,当我们录制完成后,并不能确定登录系统,虽然脚本回放中,在log replay中显示没有错误,但是我们打开result view后可以看到实际并没有通过,打开登陆页面 http://127.0.0.1:1080/WebTours/ 后,查看界面源代码,可以注意到有一个用户的Session,它是服务器传来的,因为没有对他进行关联,所以会产生失败。解决的方法就是使用关联。同理,飞机启航的地址和目的地址也需要使用关联的方法。
2、对于用户的用户名,时间等,他并不从服务器传输,可以使用参数化的方法。
在录制时是分部分阶段的,这里整理一下代码
1、user_login
user_login()
{
lr_think_time(38);
web_submit_data("login.pl",
"Action=http://localhost:1080/WebTours/login.pl",
"Method=POST",
"RecContentType=text/html",
"Referer=http://localhost:1080/WebTours/nav.pl?in=home",
"Snapshot=t2.inf",
"Mode=HTML",
ITEMDATA,
"Name=userSession", "Value={WCSParam_Diff1}", ENDITEM,
"Name=username", "Value=jojo", ENDITEM,
"Name=password", "Value=bean", ENDITEM,
"Name=JSFormSubmit", "Value=off", ENDITEM,
"Name=login.x", "Value=42", ENDITEM,
"Name=login.y", "Value=14", ENDITEM,
LAST);
return 0;
}
2、user_filght(这里很重要,使用了web_reg_save_param函数)
choose_flight()
{
lr_think_time(24);
//在这里,打开页面之前进行关联
web_reg_save_param("Flight_Info","LB=/">","RB=</option>","ORD=ALL",LAST);
web_url("welcome.pl",
"URL=http://localhost:1080/WebTours/welcome.pl?page=search",
"Resource=0",
"RecContentType=text/html",
"Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=home",
"Snapshot=t3.inf",
"Mode=HTML",
EXTRARES,
"Url=FormDateUpdate.class", "Referer=", ENDITEM,
"Url=CalSelect.class", "Referer=", ENDITEM,
LAST);
return 0;
}
3、flight_order(最重要的一部分,这里实现了关联和航班时间的参数化)
///由于航班信息是系统发出的,这里使用参数化的方法就不合适,因此使用关联的方法
///对于航班时间,和输入的用户名,可以使用参数化的方法来实现
filght_order()
{
//定义离开和到达的航班的编号,因为这些编号在使用时是要连接在flight_info后面使用的。
int Depart_Num=0,Arrive_Num=0;
//sprintf用来接收格式更改色是一个***字符数组***,这点之前忽略了,应该注意
char Rand_Depart_Num[50],Rand_Arrive_Num[50];
char Depart_Info[50],Arrive_Info[50];
//获得一个随机数,把它付给Depart_Num,在获得Arrive_Num的值
Depart_Num=rand()%(lr_paramarr_len("Flight_Info")/2)+1;
if(Depart_Num==10)
Arrive_Num=Depart_Num-1;
else
Arrive_Num=Depart_Num+1;
//检查一下输出结果
lr_output_message("Depart_Num is %d,Arrive_Num is %d",Depart_Num,Arrive_Num);
//将Rand_Depart_Num和Rand_Arrive_Num写成Flight_Info_%d的形式,这样他就与录制的脚本形式相同了
sprintf(Rand_Depart_Num,"{Flight_Info_%d}",Depart_Num);
sprintf(Rand_Arrive_Num,"{Flight_Info_%d}",Arrive_Num);
//将Flight_Info_%d的形式再转换为value=%s的形式,之后就可以添加到代码中了
//这里总共进行了两次转换,第一次是将一个随机数转换为Flight_Info_能够接受的形式
//第二次是将Flight_Info_的形式在转换成Value=的形式
sprintf(Depart_Info,"Value=%s",lr_eval_string(Rand_Depart_Num));
sprintf(Arrive_Info,"Value=%s",lr_eval_string(Rand_Arrive_Num));
//检查一下输出结果
lr_output_message("Depart_Info is:%s",Depart_Info);
lr_output_message("Arrive_Info is:%s",Arrive_Info);
lr_think_time(29);
web_submit_data("reservations.pl",
"Action=http://localhost:1080/WebTours/reservations.pl",
"Method=POST",
"RecContentType=text/html",
"Referer=http://localhost:1080/WebTours/reservations.pl?page=welcome",
"Snapshot=t4.inf",
"Mode=HTML",
ITEMDATA,
"Name=advanceDiscount", "Value=0", ENDITEM,
"Name=depart", Depart_Info, ENDITEM, //Depart_Info替代之前的"Value=Frankfurt"
"Name=departDate", "Value={Depart_Time}", ENDITEM, //这里讲航班起飞日期参数化
"Name=arrive", Arrive_Info, ENDITEM, //Arrive_Info替代之前的"Value=Paris"
"Name=returnDate", "Value={Arrive_Time}", ENDITEM, //将航班到达时间参数化
"Name=numPassengers", "Value=1", ENDITEM,
"Name=seatPref", "Value=None", ENDITEM,
"Name=seatType", "Value=Coach", ENDITEM,
"Name=.cgifields", "Value=roundtrip", ENDITEM,
"Name=.cgifields", "Value=seatType", ENDITEM,
"Name=.cgifields", "Value=seatPref", ENDITEM,
"Name=findFlights.x", "Value=53", ENDITEM,
"Name=findFlights.y", "Value=5", ENDITEM,
LAST);
//检查时间的输出结果
lr_output_message("departDate is:%s",lr_eval_string("{Depart_Time}"));
lr_output_message("arriveDate is:%s",lr_eval_string("{Depart_Time}"));
return 0;
}
airlin_order()
{
lr_think_time(12);
web_submit_data("reservations.pl_2",
"Action=http://localhost:1080/WebTours/reservations.pl",
"Method=POST",
"RecContentType=text/html",
"Referer=http://localhost:1080/WebTours/reservations.pl",
"Snapshot=t5.inf",
"Mode=HTML",
ITEMDATA,
"Name=outboundFlight", "Value=140;128;03/04/2010", ENDITEM,
"Name=numPassengers", "Value=1", ENDITEM,
"Name=advanceDiscount", "Value=0", ENDITEM,
"Name=seatType", "Value=Coach", ENDITEM,
"Name=seatPref", "Value=None", ENDITEM,
"Name=reserveFlights.x", "Value=52", ENDITEM,
"Name=reserveFlights.y", "Value=5", ENDITEM,
LAST);
return 0;
}
4、payment_detail
payment_detail()
{
lr_think_time(9);
web_submit_data("reservations.pl_3",
"Action=http://localhost:1080/WebTours/reservations.pl",
"Method=POST",
"RecContentType=text/html",
"Referer=http://localhost:1080/WebTours/reservations.pl",
"Snapshot=t6.inf",
"Mode=HTML",
ITEMDATA,
"Name=firstName", "Value={First_Name}", ENDITEM, //将firstName参数化
"Name=lastName", "Value={Second_Name}", ENDITEM,//将secondName参数化
"Name=address1", "Value=234 Willow Drive", ENDITEM,
"Name=address2", "Value=San Jose/CA/94085", ENDITEM,
"Name=pass1", "Value=Joseph Marshall", ENDITEM,
"Name=creditCard", "Value=", ENDITEM,
"Name=expDate", "Value=", ENDITEM,
"Name=oldCCOption", "Value=", ENDITEM,
"Name=numPassengers", "Value=1", ENDITEM,
"Name=seatType", "Value=Coach", ENDITEM,
"Name=seatPref", "Value=None", ENDITEM,
"Name=outboundFlight", "Value=140;128;03/04/2010", ENDITEM,
"Name=advanceDiscount", "Value=0", ENDITEM,
"Name=returnFlight", "Value=", ENDITEM,
"Name=JSFormSubmit", "Value=off", ENDITEM,
"Name=.cgifields", "Value=saveCC", ENDITEM,
"Name=buyFlights.x", "Value=24", ENDITEM,
"Name=buyFlights.y", "Value=5", ENDITEM,
LAST);
//检查输出结果
lr_output_message("User's firsName is:%s",lr_eval_string("{First_Name}"));
lr_output_message("User's secondName is:%s",lr_eval_string("{Second_Name}"));
return 0;
}
5、user_logoff
user_logoff()
{
lr_think_time(16);
web_url("welcome.pl_2",
"URL=http://localhost:1080/WebTours/welcome.pl?signOff=1",
"Resource=0",
"RecContentType=text/html",
"Referer=http://localhost:1080/WebTours/nav.pl?page=menu&in=flights",
"Snapshot=t7.inf",
"Mode=HTML",
LAST);
return 0;
}