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常用函数以及调用自定义函数的更多相关文章

  1. 如何在sqlite3连接中创建并调用自定义函数

    #!/user/bin/env python # @Time :2018/6/8 14:44 # @Author :PGIDYSQ #@File :CreateFunTest.py '''如何在sql ...

  2. PHP中call user func()和call_user_func_array()调用自定义函数小结

    call_user_func() 和 call_user_func_array(),通过传入字符串函数,可以调用自定义函数,并且支持引用,都允许用户调用自定义函数并传入一定的参数: 1.mixed c ...

  3. Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数

    Python第七天   函数  函数参数   函数里的变量   函数返回值  多类型传值     函数递归调用   匿名函数   内置函数 目录 Pycharm使用技巧(转载) Python第一天   ...

  4. python27期day09:函数的初始、函数的定义、函数的调用、函数的返回值、函数的参数、作业题。

    1.函数的作用:封装代码.大量的减少了重复的代码. 2.全局空间:顶行写的就是全局空间. 图解 : 3.函数的定义: def 是一个关键字.申明要定义一个函数 my_len 函数的名字.遵循变量命名的 ...

  5. 使用 {$INCLUDE} 或 {$I} 指令管理和调用自定义函数

    这是一个简单.方便而又实用的小技巧. 譬如这段代码中有四个定义函数: MyAdd.MyDec.MyMul.MyDiv unit Unit1; interface uses   Windows, Mes ...

  6. 微信小程序wxml文件中调用自定义函数

    想在微信小程序的wxml文件里自如的像vue那样调用自定义的方法,发现并不成功,得利用WXS脚本语言. WXS脚本语言是 WeiXin Script 脚本语言的简称,是JavaScript.JSON. ...

  7. ThinkPHP 3.2 调用自定义函数库

    ThinkPHP3.2 和3.1 区别还是有点大的 引入了命名空间,强化了驱动化和行为,增强了模块化的概念和对云平台的支持,并改进了诸多的细节.主要改进包括:模块化架构的全新设计全新命名空间和自动导入 ...

  8. Mysql5.7创建存储过程中调用自定义函数报错Not allowed to return a result set from a function

    因为很多存储过程都会共用一段sql语句,所以我把共用的sql封装成一个自定义函数 AddCapital(); 然后通过存储过程调用,创建存储过程会报错1415,Not allowed to retur ...

  9. Python基础(函数,函数的定义,函数的调用,函数的参数,递归函数)

    1.函数 我们知道圆的面积计算公式为: S = πr2 当我们知道半径r的值时,就可以根据公式计算出面积.假设我们需要计算3个不同大小的圆的面积: r1 = 12.34 r2 = 9.08 r3 = ...

随机推荐

  1. log4net 配置

    1.是直接在代码中通过调用XmlConfigurator.Configure()来解析配置文件,配置日志环境. log4net.Config.XmlConfigurator.Configure(); ...

  2. OAF_开发系列20_实现OAF打印功能

    ddddd 添加一个页面级的button区域:pagebuttonBar,在之下添加button item ,这里主要设置的参数有:采用默认的oaf的打印按钮的id名称: IcxPrintablePa ...

  3. DevExpress TreeList 全选和反选 z

    /// <summary> /// 全选树 /// </summary> /// <param name="tree">树控件</para ...

  4. dubbo初识(一)Dubbo架构设计详解

    参见http://shiyanjun.cn/archives/325.html Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合( ...

  5. 基于Material Design(转载)

    SeeNewsV2新闻Android客户端 基于Material Design http://www.codesocang.com/gn/xiangmu/33630.html 直接拿来用!十大Mate ...

  6. super getClass()

    首先看一段代码: import java.util.Date;public class Test extends Date{ public static void main(String[] args ...

  7. d3安装异常

    使用npm安装D3,发现其工程名和依赖名重复,导致安装异常 http://thisdavej.com/node-newbie-error-npm-refusing-to-install-package ...

  8. Dimmer: 通过移动鼠标来改变 LED 的亮度

    原文地址 - https://www.arduino.cc/en/Tutorial/Dimmer 调光器 本例展示了如何通过个人电脑发送数据到 Arduino / Genuino 开发板来控制一个LE ...

  9. Python 配置日志

    Python 2.6+ def cfgLogging(): from logging.handlers import RotatingFileHandler console = logging.Str ...

  10. nginx日志分割脚本

    [root@localhost nginx]# cat logs/nginx.pid 5118[root@localhost nginx]# kill -QUIT 5118-QUIT : 关闭进程-H ...