1. 程式人生 > >loadrunner編寫飛機訂票系統(web tours)指令碼

loadrunner編寫飛機訂票系統(web tours)指令碼

Exit_Flight()
{




int Flight_count1,Flight_count2;//Flight_count1,Flight_count2 分別用來儲存2次隨機值
int count=0;// count儲存飛機票數量
char buffer[1024]={0};
char buffer1[1024]={0};
char buffer2[1024]={0};
char buffer3[1024]={0};




int i;
char temp[1024]={0};
char paras[1024]={0};


web_set_max_html_param_len("1024");




/**
* 用來關聯飛機票的Name值
*/
web_reg_save_param("FlightName",
   "LB=type=\"checkbox\" name=\"",
   "RB=\"",
   "Notfound=warning",
   "Search=body",
   "ORD=all",
   LAST); 


/**
* 用來關聯飛機票的Value值
*/
web_reg_save_param("FlightValue",
   "LB=name=\"flightID\" value=\"",
   "RB=\"",
   "Notfound=warning",
   "Search=body",
   "ORD=all",
   LAST);




/**
* 獲取有多少張票 以及每張票的value
*/
web_url("退票介面",
"URL={ip}/WebTours/itinerary.pl",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
LAST);


lr_output_message("--------------------------------有%s張票!!!",lr_eval_string("{FlightValue_count}"));


count=atoi(lr_eval_string("{FlightValue_count}"));  // atoi把字串的數字轉換為整形的數字 
// FlightValue_count為飛機票的數量












/**
     * 如果頁面存在2條以上的票務資訊就退票2張
     * 如果頁面存在2條以下包括2條票務資訊就全退
     * 如果沒有票務資訊就退出
     */
if(count>0){
if(count<=2){




   /**
            *拼接表單資訊
*/
for (i=1;i<=count;i++) {

sprintf(temp,"{FlightValue_%d}", i);
sprintf(temp,"flightID=%s&",lr_eval_string(temp));
strcat(paras,temp);
sprintf(temp,"{FlightName_%d}", i);
sprintf(temp,".cgifields=%s&",lr_eval_string(temp));
strcat(paras,temp);
//sprintf(temp,"{FlightName_%d}",i);
//sprintf(temp,"%s=on&",lr_eval_string(temp));
//strcat(paras,temp);

}
strcat(paras,"removeAllFlights.x=77&removeAllFlights.y=3");
lr_output_message("------------------------引數拼接最後形態:%s",lr_eval_string(paras));
lr_save_string(paras,"paras");
/**
             * 退票操作(退全張)
* paras為拼接的字串
*/
web_custom_request("退票",
"URL=http://127.0.0.1:1080/WebTours/itinerary.pl",
"Method=POST",
"TargetFrame=",
"Resource=0",
"Mode=HTML",
"Referer=http://127.0.0.1:1080/WebTours/itinerary.pl",
"Body={paras}",
LAST);


lr_output_message("-----------------------全部退票");




}else{



Flight_count1=rand()%(count-1+1)+1;//隨機在飛機票數量中取值 
Flight_count2=rand()%(count-1+1)+1;//隨機在飛機票數量中取值 

/**
*將int儲存為引數
*/
lr_save_int(Flight_count1, "param1");
lr_save_int(Flight_count2, "param2");
/**
* 對比2次取值是否相同
* 相同的話給Flight_count2重新隨機
*/
while (strcmp(lr_eval_string("{param1}"),lr_eval_string("{param2}"))==0) {

lr_error_message("Flight_count1取值為%s",lr_eval_string("{param1}"));
lr_error_message("Flight_count2取值為%s",lr_eval_string("{param2}"));
Flight_count2=rand()%(count-1+1)+1;//重新取第二次隨機值
lr_save_int(Flight_count2, "param2");//重新將int儲存為引數param2
}
lr_output_message("第一次隨機值為%s",lr_eval_string("{param1}"));
lr_output_message("第二次隨機值為%s",lr_eval_string("{param2}"));


/**
* 拼接表單提交內容
*/
for (i=1;i<=count;i++) {

sprintf(temp,"{FlightValue_%d}", i);
sprintf(temp,"flightID=%s&",lr_eval_string(temp));
strcat(paras,temp);
sprintf(temp,"{FlightName_%d}", i);
sprintf(temp,".cgifields=%s&",lr_eval_string(temp));
strcat(paras,temp);



}

sprintf(temp,"{FlightName_%s}", lr_eval_string("{param1}"));
sprintf(temp,"%s=on&",lr_eval_string(temp));
strcat(paras,temp);
sprintf(temp,"{FlightName_%s}", lr_eval_string("{param2}"));
sprintf(temp,"%s=on&",lr_eval_string(temp));
strcat(paras,temp);
strcat(paras,"removeFlights.x=64&removeFlights.y=8");

lr_output_message("------------------------引數拼接最後形態:%s",lr_eval_string(paras));

lr_save_string(paras,"paras");





/**
             * 退票操作(退2張票)
* paras為拼接的字串
*/
web_custom_request("退票",
"URL=http://127.0.0.1:1080/WebTours/itinerary.pl",
"Method=POST",
"TargetFrame=",
"Resource=0",
"Mode=HTML",
"Referer=http://127.0.0.1:1080/WebTours/itinerary.pl",
"Body={paras}",
LAST);








}
}else{
web_url("退出",
  "URL={ip}/WebTours/welcome.pl?signOff=1",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
LAST);
lr_output_message("-----------------------沒有票,退出");


}










/**
     * 將隨機值寫入FlightValue中
     * 用來取出向對應的飛機票
     * 將隨機值寫入FlightName中
     * 用來取出飛機票向對應的號碼
     
sprintf(buffer, "{FlightValue_%s}", lr_eval_string("{param1}"));
sprintf(buffer1,"{FlightValue_%s}", lr_eval_string("{param2}"));


sprintf(buffer2, "{FlightName_%s}", lr_eval_string("{param1}"));
sprintf(buffer3, "{FlightName_%s}", lr_eval_string("{param2}"));


lr_save_string(lr_eval_string(buffer),"flightID1");
lr_save_string(lr_eval_string(buffer1),"flightID2");


lr_save_string(lr_eval_string(buffer2),"number1");
lr_save_string(lr_eval_string(buffer3),"number2");


lr_output_message("-----------------第一張飛機票為%s",lr_eval_string("{flightID1}"));
lr_output_message("-----------------第二張飛機票為%s",lr_eval_string("{flightID2}"));
lr_output_message("-----------------第一張飛機票編號為%s",lr_eval_string("{number1}"));
lr_output_message("-----------------第二張飛機票編號為%s",lr_eval_string("{number2}"));
*/








/**
     * 退票2張
     * flightID1第一張票value
     * flightID2第二張票value
     * number1第一張票編號name
     * number2第二張票編號name
     
web_submit_data("退票",
"Action=http://127.0.0.1:1080/WebTours/itinerary.pl",
"Method=POST", 
"RecContentType=text/html", 
"Referer=http://127.0.0.1:1080/WebTours/itinerary.pl", 
"Snapshot=", 
"Mode=HTML",
ITEMDATA,
"name={number1}","Value=on",ENDITEM,
"name=flightID","Value={flightID1}",ENDITEM,
"name={number2}","Value=on",ENDITEM,
"name=flightID","Value={flightID2}",ENDITEM,
"name=removeFlights.x","Value=59",ENDITEM,
"name=removeFlights.y","Value=9",ENDITEM,
"name=.cgifields","Value={number1}",ENDITEM,
"name=.cgifields","Value={number2}",ENDITEM,
LAST);*/




return 0;
}
附城市全集:
DenverFrankfurtLondonLos AngelesParisPortlandSan FranciscoSeattleSydneyZurich