LR常用函数以及调用自定义函数
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);
}
}
LR常用函数以及调用自定义函数的更多相关文章
- 如何在sqlite3连接中创建并调用自定义函数
#!/user/bin/env python # @Time :2018/6/8 14:44 # @Author :PGIDYSQ #@File :CreateFunTest.py '''如何在sql ...
- PHP中call user func()和call_user_func_array()调用自定义函数小结
call_user_func() 和 call_user_func_array(),通过传入字符串函数,可以调用自定义函数,并且支持引用,都允许用户调用自定义函数并传入一定的参数: 1.mixed c ...
- Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数
Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数 目录 Pycharm使用技巧(转载) Python第一天 ...
- python27期day09:函数的初始、函数的定义、函数的调用、函数的返回值、函数的参数、作业题。
1.函数的作用:封装代码.大量的减少了重复的代码. 2.全局空间:顶行写的就是全局空间. 图解 : 3.函数的定义: def 是一个关键字.申明要定义一个函数 my_len 函数的名字.遵循变量命名的 ...
- 使用 {$INCLUDE} 或 {$I} 指令管理和调用自定义函数
这是一个简单.方便而又实用的小技巧. 譬如这段代码中有四个定义函数: MyAdd.MyDec.MyMul.MyDiv unit Unit1; interface uses Windows, Mes ...
- 微信小程序wxml文件中调用自定义函数
想在微信小程序的wxml文件里自如的像vue那样调用自定义的方法,发现并不成功,得利用WXS脚本语言. WXS脚本语言是 WeiXin Script 脚本语言的简称,是JavaScript.JSON. ...
- ThinkPHP 3.2 调用自定义函数库
ThinkPHP3.2 和3.1 区别还是有点大的 引入了命名空间,强化了驱动化和行为,增强了模块化的概念和对云平台的支持,并改进了诸多的细节.主要改进包括:模块化架构的全新设计全新命名空间和自动导入 ...
- Mysql5.7创建存储过程中调用自定义函数报错Not allowed to return a result set from a function
因为很多存储过程都会共用一段sql语句,所以我把共用的sql封装成一个自定义函数 AddCapital(); 然后通过存储过程调用,创建存储过程会报错1415,Not allowed to retur ...
- Python基础(函数,函数的定义,函数的调用,函数的参数,递归函数)
1.函数 我们知道圆的面积计算公式为: S = πr2 当我们知道半径r的值时,就可以根据公式计算出面积.假设我们需要计算3个不同大小的圆的面积: r1 = 12.34 r2 = 9.08 r3 = ...
随机推荐
- GBK
GBK是汉字编码标准之一,全称<汉字内码扩展规范>(GBK即“国标”.“扩展”汉语拼音的第一个字母,英文名称:Chinese Internal Code Specification) ,中 ...
- not子查询中有null值的时候 not in 会失效
not in子查询中有null值的时候 not in 会失效 但是 in 的子查询中有null的 不会失效
- randomAccess接口
http://www.blogjava.net/lzqdiy/archive/2007/04/22/112578.html
- 为什么使用 Redis及其产品定位
摘自:http://www.infoq.com/cn/articles/tq-why-choose-redis 传统MySQL+ Memcached架构遇到的问题 实际MySQL是适合进行海量数据存储 ...
- VS中的活动debug和活动cpu
“活动”的含义就是当前项目所用的模式和平台.项目中的”活动“继承于解决方案中的配置.
- vs 配置宏
Win_$(PROCESSOR_ARCHITECTURE)_$(PlatformArchitecture) <==> Win_x86_64 OR Win_x86_32$(Configura ...
- Android 蓝牙打印超时问题的处理
http://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on ...
- 使用Delphi收发GMail的邮件
GMAIL的端口和连接方式比较特殊:SMTP端口为:456POP3端口为:995都采用安全连接(SSL)这些通过Indy组件就可以实现参考代码如下: object IdConnectionInterc ...
- python 核心编程课后练习(chapter 6)
6-1 #6-1 #help(string) import string str = "helloworld" substr = "h1e" if string ...
- 所有古诗词的api
服务商:api.getlove.cn 分类:诗词 更新时间:2016-03 如果接口不满足您的要求,您可以联系qq:2265658022购买完整的数据库 免费apikey:56eab527a0facb ...