2.LR常用函数以及调用自定义函数
2.1.LR常用函数以及对信息的判断
2.1.1. LR内部自定义函数
在LR脚本中定义变量和编写自定义函数,需将变量的声明放在脚本其他内容的上方,否则会提示【illegal statement termination】
1.编写简单函数
int sum(int a,int b)//简单的求和函数 { return a+b; } AdvSearch() { lr_message("sum = 10+2=%d",sum(9,2)); return 0; }
变量的声明要写在Action的前面,否则会提示【illegal statement termination】
图表12
解决方法:将int x,int ret的声明,紧跟char*showString之后。
2.随机字母串
int itera_num,rand_num,rand_num1,rand_case,i; char StrTable[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%&*()_+|}{:><,./;[]=-"; //大小写字母52个+数字10个+特殊字符26个=88个;~!@#$%&*()_+|}{:><,./;[]=- char i_Alpa[36]=""; web_set_max_html_param_len("12400"); itera_num=rand()%36; for (i=0;i<=itera_num;i++){ rand_num=rand()%88; strncat(i_Alpa,StrTable+rand_num,1); } lr_save_string(i_Alpa,"autoAlpaValue");
2.1.2.常用LR请求函数
使用快捷键Alt+Insert调出函数参数快捷编辑工具,如图:
图表13
- 请求URL中传参
web_url("Login", "URL=https://secure.computing.com/scripts/login.asp?user=(username)&session={ssid}", "RecContentType=text/html", //Expected content–type LAST );
- 键值对传参
web_submit_data("default.aspx", "Action=http://lazarus/flightnet/default.aspx", "Method=POST", "TargetFrame=", "RecContentType=text/html", "Referer=http://lazarus/flightnet/", "Snapshot=t7.inf", "Mode=HTML", //提交数据表单,键值对。 ITEMDATA, "Name=grpType", "Value=radRoundtrip", ENDITEM, "Name=lstDepartingCity", "Value=DEN", ENDITEM, //或者提交文件 ITEMDATA, "Name=userFile0", "Value=E:\\sense_sensibility\\Elinor.htm", "File=yes", "ContentType=text/html", // .txt :"text/plain" FilePath=Elinor.txt", "ContentTransferEncoding=html/text", ENDITEM, LAST );
- body体传参
web_custom_request("post_query.exe", "Method=POST", "URL=http://lazarus/cgi–bin/post_query.exe", "Body={\"action\":\"{actionName}\",\"name\":\"{userName}\",\"secret\":\"{password}\"}", "TargetFrame=", LAST );
以上方式是传递的信息请求体中,若存在信息需要在请求头Header中传递的,需要在请求前添加web_add_header,比如E:\项目资料\工作总结\接口测试\33.OKMS日志服务接口测试、Knowlege Management System 接口测试脚本中。
- web_add_heade传参
/* *用户名密码方式登陆 * */ web_add_header("action","name_pass_login"); web_add_header("name","{userName}"); web_add_header("secret","{secretValue}"); web_add_header("sid",""); web_add_header("uid",""); web_add_header("app_id","odata_us3"); web_add_header("timestamp","{tStamp}"); web_add_header("sign","{signValue}"); web_add_header("ip",""); web_add_header("did","\{123456\}"); web_add_header("mobile",""); web_add_header("location",""); lr_start_transaction("name_pass_Login"); web_reg_save_param("json","LB=\r\n{", "RB=", LAST); web_custom_request("login", "URL=http://192.168.100.198:8010/api/login", "Method=GET", "TargetFrame=", "RecContentType=text/json", "Referer=", "Mode=HTML", "EncType=text/json", //"Body={\"action\":\"{actionName}\",\"name\":\"{userName}\",\"secret\":\"{secretValue}\"}", LAST );
2.1.3.常用函数
- //设置保存参数最大长度;
web_set_max_html_param_len("10240");
- //保存系统当前时间距离1970年的秒数
web_save_timestamp_param("tStamp",LAST);
输出:Notify: Saving Parameter "tStamp = 1470030946475".//取第0-10位保存;
- //取第0-10位保存
lr_save_var(lr_eval_string("{tStamp}"),10,0,"tStamp");
输出:Notify: Saving Parameter "tStamp = 1470030946".
- //将参数subStamp的值复制给变量pre中。
strcpy(pre,lr_eval_string("{subStamp}"));
- //A+B的
strcat(A, B);
- // A+(B的前n位);
strncat(A,B,n);
- //比较A=B返回 =0,A>B返回 >0;
strcmp(A,B) == 0
// char * string = "His Excellency the Duke of Exeter"; char * first_x, * last_x;
- //查找在字符串中第一次出现x的位置;
first_x = (char *)strchr(string, 'x');
- //查找在字符串中最后一次出现x的位置;
last_x = (char *)strrchr(string, 'x');
备注:Returns a pointer to the first occurrence of string2 in string1. If string2 is not found in string1 the function returns NULL.
- //atoi将字符串型变为整型;passKey="123456", pre="147003"
iKey = atoi(passKey)+atoi(pre);
输出:Notify: Saving Parameter "KeyValue = 270459".
- //将整数转化为字符串,10表示按照十进制int itoa( int value, char *str, int radix);
itoa(id,strid,10); //int id = 56;
输出:strid = 56;若将10修改为2,二进制输出strid=111000;
- //将整型做为字符串保存到参数中。
lr_save_int(iKey,"KeyValue");
- //被检索字符串,被检索字符串长度,取检索到的第几次的值,检索词,从检索词后偏移几位开始,截取长度,保存参数名。
lr_save_searched_string(lr_eval_string("{json}"),strlen(lr_eval_string("{json}")), 0,"\"SID\":\"",0,32,"SIDValue");
- //保存返回值;
web_reg_save_param("p", "LB/BIN=\\x3F\\xDD", "RB/BIN=\\xCCb", LAST ); //二进制
- //将值写入参数filename中,int index = 56; char filename[64], * suffix = "txt";
sprintf(filename, "log_%d.%s", index, suffix);
输出:Notify: Saving Parameter " filename = log_56.txt".
2.1.4.信息判断
web_reg_save_param("json ", "LB =< ", "RB = >", LAST ); ………… //获取返回信息 lr_save_var(lr_eval_string("{json}"),5,0,"StartWith"); //判断返回信息与预期值 if(strcmp(lr_eval_string("{StartWith}"),"\"SID\"") == 0) { // lr_message("The SIDValue : %s",lr_eval_string("{SIDValue}")); lr_end_transaction("name_pass_Login", LR_PASS); }else (strcmp(lr_eval_string("{StartWith}")," \"err") == 0){ lr_save_var(lr_eval_string("{json}"),13,0,"StartStr"); if(strcmp(lr_eval_string("{StartStr}")," \"error\": \"-1") == 0) { lr_message("The json : %s",lr_eval_string("{json}")); lr_end_transaction("name_pass_Login", LR_FAIL); }else{ lr_end_transaction("name_pass_Login", LR_PASS); } }