各位新年好!
前两周将LR在手机应用测试实现作了简单介绍,相信各位对如何使用有一定的了解,当然仅仅是包括了最基本如何去实现问题,今天也不例外,因为注意到LR自带的订票系统(11.5的,之前的订票系统可能有些不一样)是不少像我一样的初学者拿来做实验的,网上有部分脚本,但我看了下不是很完整,所以就在此补充了。欢迎各位大侠拍砖。
vuser_init
HomePage()
{
web_reg_save_param("UserSession",
"LB=<input type=hidden name=userSession value=",
"RB=>",
"ORD=ALL",
LAST);
web_url("webtours",
"URL=http://127.0.0.1:1080/WebTours/",
"TargetFrame=",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
LAST);
lr_think_time(26);
lr_paramarr_len("UserSession");
return 0;
}
第三部分:
SubmitLogin
SubmitLogin()
{
web_reg_find("Search=Body",
"Text=Welcome, <b>karsen</b>, to the Web Tours reservation pages.",
LAST);
web_submit_data("login.pl",
"Action=http://127.0.0.1:1080/cgi-bin/login.pl",
"Method=POST",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http://127.0.0.1:1080/webtours/nav.pl?in=home",
"Mode=HTML",
ITEMDATA,
"Name=userSession", "Value={UserSession_3}", ENDITEM,
"Name=username", "Value=karsen", ENDITEM,
"Name=password", "Value=123456", ENDITEM,
"Name=JSFormSubmit", "Value=on", ENDITEM,
LAST);
return 0;
}
第四部分:
Flights
Flights()
{
web_reg_save_param("FlightInfo",
"LB=\">",
"RB=</option>",
"Ord=ALL",
LAST);
web_reg_find("Search=Body",
"Text=Find Flight",
LAST);
web_image("Search Flights Button",
"Alt=Search Flights Button",
"Snapshot=t16.inf",
EXTRARES,
"Url=../WebTours/classes/FormDateUpdate.class", "Referer=", ENDITEM,
"Url=../WebTours/classes/CalSelect.class", "Referer=", ENDITEM,
"Url=../WebTours/classes/Calendar.class", "Referer=", ENDITEM,
LAST);
return 0;
}
第五部分
FlightInfo
FlightInfo()
{
char DepartCity[20]={NULL},
ArriveCity[20]={NULL},
FlightLines[20]={NULL},
FlightParam[20]={NULL};
int FlightNumbers,FlightIndex;
srand((unsigned)time(NULL));
FlightNumbers =lr_paramarr_len("FlightInfo")/2;
FlightIndex = rand()%FlightNumbers + 1;
sprintf(FlightParam,"{FlightInfo_%d}",FlightIndex);
lr_output_message ("FlightIndex is %d",FlightIndex );
sprintf(DepartCity,"%s",lr_eval_string (FlightParam));
sprintf(ArriveCity,"%s",lr_eval_string (FlightParam));
lr_save_string(DepartCity,"DepartCity");
while (strcmp(&DepartCity,&ArriveCity)==0) {
FlightIndex = rand()%FlightNumbers + 1;
sprintf(FlightParam,"{FlightInfo_%d}",FlightIndex);
sprintf(ArriveCity,"%s",lr_eval_string (FlightParam));
lr_save_string(ArriveCity,"ArriveCity");
}
lr_output_message("The Depart city is:%s",lr_eval_string("{DepartCity}"));
lr_output_message ("The Arrive city is :%s",lr_eval_string("{ArriveCity}"));
return 0;
}
第六部分
SubmitFlight
int NumPassengers=0;
SubmitFlight()
{
char SubmitFlightCustomerRequest[500];
int i=1;
lr_think_time(27);
/* web_submit_data("reservations.pl",
"Action=http://127.0.0.1:1080/webtours/reservations.pl",
"Method=POST",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http://127.0.0.1:1080/webtours/reservations.pl?page=welcome",
"Snapshot=t4.inf",
"Mode=HTML",
ITEMDATA,
"Name=advanceDiscount", "Value=1", ENDITEM,
"Name=depart", "Value={DepartCity}", ENDITEM, // you can use departCity directly if you set the string format. pay attention to the ""
"Name=departDate", "Value={DepartDate}", ENDITEM,
"Name=arrive", "Value={ArriveCity}", ENDITEM,
"Name=returnDate", "Value={ReturnDate}", ENDITEM,
"Name=numPassengers", "Value=1", ENDITEM,
"Name=roundtrip","Value=on", 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=38", ENDITEM,
"Name=findFlights.y", "Value=8", ENDITEM,
LAST);*/
srand( (unsigned)time(NULL) );
NumPassengers=rand()%9+1;
i=1;
lr_output_message("NumPassengers is :%d", NumPassengers);
lr_output_message("Value i is :%d", i);
if(i==0)
{
sprintf( SubmitFlightCustomerRequest,
"Body=advanceDiscount=1&depart=%s&departDate=%s&arrive=%s&returnDate=%s&numPassengers=%d&roundtrip=on&seatPref=%s&seatType=%s&.cgifields=roundtrip&.cgifields=seatType&.cgifields=seatPref&findFlights.x=38&findFlights.y=8",
lr_eval_string("{DepartCity}"),
lr_eval_string("{DepartDate}"),
lr_eval_string("{ArriveCity}"),
lr_eval_string("{ReturnDate}"),
NumPassengers,
lr_eval_string("{SeatPref}"),
lr_eval_string("{SeatType}") );
}
else
{
sprintf( SubmitFlightCustomerRequest,
"Body=advanceDiscount=1&depart=%s&departDate=%s&arrive=%s&returnDate=%s&numPassengers=%d&seatPref=%s&seatType=%s&.cgifields=roundtrip&.cgifields=seatType&.cgifields=seatPref&findFlights.x=38&findFlights.y=8",
lr_eval_string("{DepartCity}"),
lr_eval_string("{DepartDate}"),
lr_eval_string("{ArriveCity}"),
lr_eval_string("{ReturnDate}"),
NumPassengers,
lr_eval_string("{SeatPref}"),
lr_eval_string("{SeatType}") );
}
lr_output_message("the SubmitFlightCustomerRequest is :%s", SubmitFlightCustomerRequest);
web_reg_save_param_ex(
"ParamName=OutBoundFlightParam",
"LB=<input type = radio name=outboundFlight value=",
"RB/RE= checked|>",
"Ordinal=all",
SEARCH_FILTERS,
LAST);
web_reg_save_param_ex(
"ParamName=ReturnFlightParam",
"LB=<input type = radio name=returnFlight value=",
"RB/RE= checked|>",
"NotFound=warning",
"Ordinal=all",
SEARCH_FILTERS,
LAST);
// attribution "Notfound=warning" to export information when params not found.
//web_reg_save_param_ex is not only just find params from the server responding,it will find match items from LR functions, such as lr_save_string
/*web_reg_save_param ("OutBoundFlight","LB=<input type = radio name=outboundFlight value=","RB=>","ORD=ALL","Savelen=17",LAST);
web_reg_save_param ("ReturnFlight","LB=<input type = radio name=returnFlight value=","RB=>","ORD=ALL","Savelen=17",LAST);
you'd better use flight date as the RB , it would save your effot ,this function can't be used here
*/
web_reg_find("Search=Body",
"TextPfx=Flight departing from",
"TextSfx={DepartDate}",
LAST);
web_custom_request("reservations.pl",
"URL=http://127.0.0.1:1080/cgi-bin/reservations.pl",
"Method=POST",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t4.inf",
SubmitFlightCustomerRequest,
LAST);
return 0;
}
第七部分
SelectFlight
int ReturnFlightNum=0;
SelectFlight()
{
char OutBoundFlight[50]={NULL},
ReturnParam[50]={NULL},
SubmitSelectFlightData[500]={NULL};
lr_think_time(17);
srand( (unsigned)time(NULL) );
sprintf( OutBoundFlight,"{OutBoundFlightParam_%d}",rand() % 4 + 1 );
lr_save_string( lr_eval_string(OutBoundFlight),"OutBoundFlight" );
lr_output_message("Selected flight is :%s", lr_eval_string("{OutBoundFlight}"));
lr_output_message("Num of OutBoundFlight is :%d", lr_paramarr_len("OutBoundFlightParam"));
ReturnFlightNum=lr_paramarr_len( "ReturnFlightParam" );
if(ReturnFlightNum!=0)
{
sprintf( ReturnParam,"{ReturnFlightParam_%d}",rand() % 4 + 1);
lr_save_string( lr_eval_string(ReturnParam),"ReturnFlight" );
lr_output_message("%s", lr_eval_string("{ReturnFlight}"));
sprintf( SubmitSelectFlightData,
"Body=outboundFlight=%s&returnFlight=%s&numPassengers=%d&advanceDiscount=1&seatType=%s&seatPref=%s&reserveFlights.x=32&reserveFlights.y=8",
lr_eval_string("{OutBoundFlight}"),
lr_eval_string("{ReturnFlight}"),
NumPassengers,
lr_eval_string("{SeatType}"),
lr_eval_string("{SeatPref}") );
}
else
{
lr_save_string( lr_eval_string(ReturnParam),"ReturnFlight" );
lr_output_message("%s", lr_eval_string("{ReturnFlight}"));
sprintf( SubmitSelectFlightData,
"Body=outboundFlight=%s&numPassengers=%d&advanceDiscount=1&seatType=%s&seatPref=%s&reserveFlights.x=32&reserveFlights.y=8",
lr_eval_string("{OutBoundFlight}"),
NumPassengers,
lr_eval_string("{SeatType}"),
lr_eval_string("{SeatPref}") );
}
lr_output_message("SubmitSelectFlightData is :%s", lr_eval_string(SubmitSelectFlightData));
web_reg_find("Search=Body",
"Text=Payment Details",
LAST);
web_custom_request("reservations.pl_2",
"URL=http://127.0.0.1:1080/cgi-bin/reservations.pl",
"Method=POST",
"Resource=0",
"RecContentType=text/html",
"Referer=",
SubmitSelectFlightData,
"Snapshot=t5.inf",
LAST);
/* web_submit_data("reservations.pl_2",
"Action=http://127.0.0.1:1080/webtours/reservations.pl",
"Method=POST",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http://127.0.0.1:1080/webtours/reservations.pl",
"Snapshot=t5.inf",
"Mode=HTML",
ITEMDATA,
"Name=outboundFlight", "Value={OutBoundFlight}", ENDITEM,
"Name=returnFlight", "Value={Return}", ENDITEM,
"Name=numPassengers", "Value=1", ENDITEM,
"Name=advanceDiscount", "Value=1", ENDITEM,
"Name=seatType", "Value={SeatType}", ENDITEM,
"Name=seatPref", "Value={SeatPref}", ENDITEM,
"Name=reserveFlights.x", "Value=32", ENDITEM,
"Name=reserveFlights.y", "Value=8", ENDITEM,
LAST);
*/
return 0;
}
第八部分
SubmitPatmentDetails
SubmitPamentDetails()
{
char SubmitPamentDetails[800]={NULL},
SubmitPamentDetails1[500]={NULL},
SubmitPamentDetails2[200]={NULL};
int m=1;
lr_think_time(24);
lr_save_string( lr_eval_string(RandValue()),
"FirstName" );
lr_save_string( lr_eval_string(RandValue()),
"LastName" );
lr_save_string( lr_eval_string(RandValue()),
"StreetAddress" );
lr_save_string( lr_eval_string(RandValue()),
"City" );
lr_save_string( lr_eval_string(RandValue()),
"Address1" );
lr_output_message("the first name is :%s", lr_eval_string("{FirstName}"));
lr_output_message("the last name is :%s", lr_eval_string("{LastName}"));
lr_output_message("the street is :%s", lr_eval_string("{StreetAddress}"));
lr_output_message("the city is :%s", lr_eval_string("{City}"));
lr_output_message("the first passenger's address is :%s", lr_eval_string("{Address1}"));
web_reg_find("Search=Body",
"TextPfx={FirstName}{LastName}",
"TextSfx=Flight Invoice",
LAST);
if(ReturnFlightNum==0)
{
web_submit_data("reservations.pl_3",
"Action=http://127.0.0.1:1080/cgi-bin/reservations.pl",
"Method=POST",
"TargetFrame=",
"RecContentType=text/html",
"Referer=http://127.0.0.1:1080/cgi-bin/reservations.pl",
"Snapshot=t6.inf",
"Mode=HTML",
ITEMDATA,
"Name=firstName", "Value={FirstName}", ENDITEM,
"Name=lastName", "Value={LastName}", ENDITEM,
"Name=address1", "Value={StreetAddress}", ENDITEM,
"Name=address2", "Value={City}", ENDITEM,
"Name=pass1", "Value= {Address1}", ENDITEM,
"Name=creditCard", "Value={CreditCard}", ENDITEM,
"Name=expDate", "Value={ExpDate}", ENDITEM,
"Name=oldCCOption", "Value=", ENDITEM,
"Name=numPassengers", "Value=1", ENDITEM,
"Name=seatType", "Value={SeatType}", ENDITEM,
"Name=seatPref", "Value={SeatPref}", ENDITEM,
"Name=returnFlight", "Value=", ENDITEM,
"Name=advanceDiscount", "Value=1", ENDITEM,
"Name=JSFormSubmit", "Value=on", ENDITEM,
"Name=.cgifields", "Value=saveCC", ENDITEM,
"Name=buyFlights.x", "Value=46", ENDITEM,
"Name=buyFlights.y", "Value=7", ENDITEM,
LAST);
}
else
{
sprintf( SubmitPamentDetails,
"Body=firstName=%s&lastName=%s&address1=%s&address2=%s",
lr_eval_string("{FirstName}"),
lr_eval_string("{LastName}"),
lr_eval_string("{StreetAddress}"),
lr_eval_string("{City}"),
lr_eval_string("{Address1}") );
sprintf( SubmitPamentDetails1,
"&creditCard=%s&expDate=%s&oldCCOption=&numPassengers=%d&seatType=%s&seatPref=%s&outboundFlight=%s&advanceDiscount=1&returnFlight=%s&JSFormSubmit=on&.cgifields=saveCC&buyFlights.x=24&buyFlights.y=7",
lr_eval_string("{CreditCard}"),
lr_eval_string("{ExpDate}"),
NumPassengers,
lr_eval_string("{SeatType}"),
lr_eval_string("{SeatPref}"),
lr_eval_string("{OutBoundFlight}"),
lr_eval_string("{ReturnFlight}") );
srand( (unsigned)time(NULL) );
for(m;m<=NumPassengers;m++)
{
sprintf( SubmitPamentDetails2,
"&pass%d=%s",
m,
lr_eval_string(RandValue()) );
strcat( SubmitPamentDetails,
SubmitPamentDetails2 );
lr_output_message("SubmitPamentDetails is %s", lr_eval_string(SubmitPamentDetails));
}
strcat( SubmitPamentDetails,
SubmitPamentDetails1 );
lr_output_message("SubmitPamentDetails is %s", lr_eval_string(SubmitPamentDetails));
web_custom_request("reservations.pl_3",
"URL=http://127.0.0.1:1080/webtours/reservations.pl",
"Method=POST",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t6.inf",
SubmitPamentDetails,
LAST);
}
return 0;
}
第九部分
Invoice
Invoice()
{
lr_think_time(11);
web_submit_form("reservations.pl_4",
"Snapshot=t38.inf",
ITEMDATA,
"Name=Book Another.x", "Value=19", ENDITEM,
"Name=Book Another.y", "Value=10", ENDITEM,
EXTRARES,
"Url=../WebTours/classes/Calendar.class", "Referer=", ENDITEM,
LAST);
return 0;
}
第十部分
CancleFlights
CancleFlights()
{
int i=1,m=1;
char BookingRecord[1000]={NULL},
BookingRecord2[400]={NULL},
Temp1[100]={NULL},
Temp2[100]={NULL};
web_reg_save_param("Itinerary","LB=<input type=\"hidden\" name=\"flightID\" value=\"","RB=\"","ORD=ALL",LAST);
web_url("Itinerary Button",
"URL=http://127.0.0.1:1080/cgi-bin/welcome.pl?page=itinerary",
"TargetFrame=body",
"Resource=0",
"RecContentType=text/html",
"Referer=http://127.0.0.1:1080/cgi-bin/nav.pl?page=menu&in=flights",
"Snapshot=t8.inf",
"Mode=HTML",
LAST);
i=lr_paramarr_len("Itinerary");
srand((unsigned)time(NULL));
//cancle flights
if(i!=0)
{
strcat(BookingRecord,"Body=");
strcat(BookingRecord2,"iter=-1&dest=itinerary.pl&");
if(rand()%4==1) //random cancle one or all flights
{
for(m;m<=i;m++)
{
sprintf(Temp1,"flightID={Itinerary_%d}&",m);
sprintf(Temp2,".cgifields=%d&",m);
strcat(BookingRecord,lr_eval_string(Temp1));
strcat(BookingRecord2,lr_eval_string(Temp2));
}
strcat(BookingRecord,BookingRecord2);
strcat(BookingRecord,"removeAllFlights.x=21&removeAllFlights.y=9");//generate param to cancle all flights
}
else
{
for ( m;m<=i;m++ )
{
if(rand()%3==1)
{
sprintf(Temp1,"%d=on&flightID={Itinerary_%d}&",rand()%i,m);
}
else
{
sprintf(Temp1,"flightID={Itinerary_%d}&",m);
}
sprintf(Temp2,".cgifields=%d&",m);
strcat(BookingRecord,lr_eval_string(Temp1));
strcat(BookingRecord2,lr_eval_string(Temp2));
}
strcat(BookingRecord,BookingRecord2);
strcat(BookingRecord,"removeFlights.x=81&removeFlights.y=15"); //generate param to cancle random flights
}
lr_output_message("%s",BookingRecord);
web_reg_find("Search=Body",
"Text=Itinerary",
LAST);
web_custom_request("localredirect.pl",
"URL=http://127.0.0.1:1080/cgi-bin/itinerary.pl",
"Method=POST",
"Referer=http://127.0.0.1:1080/cgi-bin/itinerary.pl",
"RecContentType=text/html",
"Snapshot=t11.inf",
"Mode=HTML",
BookingRecord,
LAST);
}
else
{
lr_error_message("%s","No flight record found!");
}
return 0;
}
第十一部分
LogOut
LogOut()
{
lr_think_time(5);
web_url("welcome.pl_2",
"URL=http://127.0.0.1:1080/WebTours/home.html",
"TargetFrame=",
"Resource=0",
"RecContentType=text/html",
"Referer=http://127.0.0.1:1080/cgi-bin/welcome.pl?signOff=1",
"Snapshot=t10.inf",
"Mode=HTML",
LAST);
return 0;
}
第十二部分
Vuserend
vuser_end()
{
return 0;
}
因为这篇脚本太长了,相关的说明及注意事项将在下篇介绍。