Loadrunner随机删除1-2张机票

1、通过录制生成初步的脚本。录制时记得适时添加事务,方便辨认事务操作

2、对脚本进行登录session关联,机票订票信息关联等

3、切换到tree模式下,找到删除机票的requestbody内容

 


4、编写自定义函数,用于拼接body内容

Action()

{

  int count;

  int idx;

char * flight = (char *)malloc(1024);

 

    //通过关联获取已有订单号

web_reg_save_param("p_flighted",

"LB=name=\"flightID\" value=\"",

"RB=\"",

"Ord=All",

"NotFound=WARNING",

LAST);

 

lr_start_transaction("退票");

 

lr_think_time(17);

 

web_url("Itinerary Button",

"URL=http://127.0.0.1:1080/webtours/welcome.pl?page=itinerary",

"TargetFrame=body",

"Resource=0",

"RecContentType=text/html",

"Referer=http://127.0.0.1:1080/webtours/nav.pl?page=menu&in=home",

"Snapshot=t3.inf",

"Mode=HTML",

LAST);

 

lr_think_time(7);

 

flight = getBody("p_flighted");

 

//发起删除机票的请求

web_custom_request("itinerary.pl",

"Method=POST",

"URL=http://127.0.0.1:1080/webtours/itinerary.pl",

“flight”,

LAST);

 

lr_end_transaction("退票",LR_AUTO);

 

return 0;

}

 

char * getBody(char * p_paramName){

 

int count = lr_paramarr_len(p_paramName);//获取参数p_flighted的个数

int i,j,idx;

char * temp = (char *)malloc(10);

char * flight_body = (char *)malloc(1024);

* flight_body = '\0';//初始化字符串flighted

strcat(flight_body,"Body=");

 

if (count == 0) {

 

lr_output_message("-------没有订单,不用删除-------");

return 0;

}

else if (count == 1) {

 

lr_output_message("-------只有一个订单,退一张票--------");

i = 1;

j = null;

}

else

{

srand((int)time(0));//重置随机种子数,让每一次的随机数不一样

i = rand()%(count + 1);

do {

srand((int)time(0));

j = rand()%(count + 1);

} while ( i == j );

}

lr_output_message("第一张机票的下标:%d",i);

lr_output_message("第二张机票的下标:%d",j);

lr_output_message("总机票订单数:%d",count);

 

//以下用于判断确认删除的航班下标,并循环拼接所有flightID的值

for (idx=1; idx<=count; idx++) {

 

if (idx ==i || idx == j) {

 

itoa(idx,temp,10);

strcat(flight_body,temp);

strcat(flight_body,"=on&");

}

 

strcat(flight_body,"flightID=");

strcat(flight_body,lr_paramarr_idx(p_paramName,idx));

strcat(flight_body,"&");

}

 

for (idx=1; idx<=count; idx++) {

 

itoa(idx,temp,10);

strcat(flight_body,".cgifields=");

strcat(flight_body,temp);

strcat(flight_body,"&");

 

}

 

strcat(flight_body,"removeFlights.x=47&removeFlights.y=3");

 

lr_output_message("拼接的body内容: %s\n",flight_body);

 

return flight_body;

 

}

 

 

5、web_custom_request()函数发起删除机票的请求,用自定义函数返回的结果作为body

 

 

 

6、查看脚本回放结果,确定是否删除成功

删除前:

 

删除后: