LoadRunner脚本编写

2023-02-02 15:56:19 浏览数 (1)

引言:针对接口进行脚本编写,解决录制脚本的局限。

第一步:打开LR,选择协议Web Services。

第二步:在action里进行脚本编写。

登陆:

代码语言:javascript复制
 1     int HttpRetCode_login,HttpRetCode_add;//定义两个变量,必须放在最前面。
 2 
 3     //*******************用例1*******************
 4     
 5     web_reg_save_param("result_login","LB=","RB=","Search=headers",LAST);  //从响应数据获取指定内容
 6 
 7     //post请求使用web_submit_data
 8     web_submit_data("web_submit_data",
 9         "Action=http://*******/login",
10         "Method=POST",
11         "TargetFrame=",
12         "Referer=",
13         "Mode=HTTP",
14          ITEMDATA,
15         "Name=username", "Value=**", ENDITEM,
16         "Name=password", "Value=****", ENDITEM,
17         LAST);
18 
19     //字符串编码转换
20     //lr_convert_string_encoding(lr_eval_string("{result_login}"),LR_ENC_UTF8,LR_ENC_SYSTEM_LOCALE,"resultutf8");
21     lr_convert_string_encoding(lr_eval_string("{result_login}"),"utf-8",NULL,"resultutf8");
22 
23     //获取参数resultutf8的值,并打印出来。
24     lr_output_message("# 响应体的内容:n %s",lr_eval_string("{resultutf8}"));
25 
26     HttpRetCode_login=web_get_int_property(HTTP_INFO_RETURN_CODE); //获取http响应代码
27 
28     //判断
29     if(HttpRetCode_login==200)
30 
31     {    
32         lr_output_message("n");
33          lr_output_message("******** 成功登陆会员系统! ********");
34 
35     }
36 
37     else
38 
39     {    
40 
41          lr_output_message("登陆失败!");
42 
43     }
44     

提交数据:

代码语言:javascript复制
 1 //等待时间
 2     lr_think_time(3);
 3 
 4 
 5     //*******************用例2*******************
 6 
 7     web_reg_save_param("result_addcard","LB=","RB=","Search=Body",LAST);
 8     //生成随机参数
 9     lr_output_message("",lr_save_string(lr_eval_string("{cardNo}"),"name"));
10     lr_output_message("*****参数:%s********",lr_eval_string("{name}"));
11 
12     //请求中文参数转码
13     lr_convert_string_encoding(lr_eval_string("储值卡"),LR_ENC_SYSTEM_LOCALE,LR_ENC_UTF8,"categoryName_value");
14     lr_save_string(lr_eval_string("{categoryName_value}"),"categoryName_value");
15     
16     web_submit_data("web_submit_data",
17         "Action=http://*******/crm/card/add",
18         "Method=POST",
19         "TargetFrame=",
20         "Referer=",
21         "Mode=HTTP",
22 
23         ITEMDATA,
24         "Name=operate", "Value=single", ENDITEM,
25         "Name=categoryName", "Value={categoryName_value}", ENDITEM,
26         "Name=cardDiscountFloor2", "Value=", ENDITEM,
27         "Name=cardDiscountFloor3", "Value=0", ENDITEM,
28         "Name=isGroup", "Value=0", ENDITEM,
29         "Name=cardNo", "Value={name}", ENDITEM,
30         "Name=cardAttr", "Value=1", ENDITEM,
31         "Name=categoryId", "Value=4", ENDITEM,
32         "Name=originalPrice", "Value=30000.00", ENDITEM,
33         "Name=realPrice", "Value=30000.00", ENDITEM,
34         "Name=itemTimes", "Value=1", ENDITEM,
35         LAST);
36     lr_convert_string_encoding(lr_eval_string("{result_addcard}"),LR_ENC_UTF8,LR_ENC_SYSTEM_LOCALE,"result_addcardtutf8");
37     lr_output_message("# 响应体的内容:n %s",lr_eval_string("{result_addcardtutf8}"));
38 
39     lr_output_message("*******新增的会员卡卡号是 = %s ", lr_eval_string("{name}"));
40 
41     HttpRetCode_add=web_get_int_property(HTTP_INFO_RETURN_CODE);
42 
43     if(HttpRetCode_add==200)
44         {    
45 
46         lr_output_message("新增会员卡成功!");
47 
48     }
49 
50     else
51         {    
52 
53         lr_output_message("新增会员卡失败!");
54 
55     }

查询提交的数据:

代码语言:javascript复制
 1     //*******************用例3*******************
 2 
 3     web_reg_save_param("result_search","LB=","RB=","Search=Body",LAST);
 4 
 5     web_submit_data("web_submit_data",
 6        "Action=http://********/api/crm/v1/getCrmCardList",
 7        "Method=POST",
 8        "TargetFrame=",
 9        "Referer=",
10        "Mode=HTTP",
11        ITEMDATA,
12        "Name=cardNo", "Value={name}", ENDITEM,
13        "Name=cardAttr", "Value=", ENDITEM,
14         "Name=cardStatus", "Value=", ENDITEM,
15         "Name=isPresent", "Value=", ENDITEM,
16         "Name=phone", "Value=", ENDITEM,
17         "Name=name", "Value=", ENDITEM,
18         "Name=order","Value=desc",ENDITEM,
19        LAST);
20     lr_convert_string_encoding(lr_eval_string("{result_search}"),LR_ENC_UTF8,LR_ENC_SYSTEM_LOCALE,"result_search");
21     lr_output_message("# 响应体的内容:n %s",lr_eval_string("{result_search}"));

0 人点赞