• LoadRunner 脚本开发和常用函数


    开发性能测试脚本原则.简单 正确 高效

    在lr中,脚本运行是解释执行的。所以在运行时,需要先编译。

    局部变量和全局变量

    1、在init、action、end中定义的变量就是局部变量

    2、在globals.h中定义的变量是全局变量

    3、什么时候定义全局变量 ? 整个过程中固定不变的,例如URL地址、KEY、其他

    int a=100;//这个a是文件级别的。除了globals.h 都可访问
    Action()
    {
        int a =10;
        int b =6; //在lr要使用变量,必须放在最上面。
        printf("%d",a);//c语言自带的打印函数不能用
        lr_output_message("%d",a);
        //int c =6;不支持其他位置定义变量
        return 0;
    }
    vuser_init()
    {
        p=(char*)malloc(1024*sizeof(char)); //给p分配内存
        return 0;
    }
    Action()
    {
        lr_output_message("hello");
        return 0;
    }
    vuser_end()
    {
        free(p);
        return 0;
    }
    
    globals.h
    
    #ifndef _GLOBALS_H
    #define _GLOBALS_H
    
    //--------------------------------------------------------------------
    // Include Files
    #include "lrun.h"
    #include "web_api.h"
    #include "lrw_custom_body.h"
    
    //--------------------------------------------------------------------
    // Global Variables
    
    #endif // _GLOBALS_H
    char *p ; //定义变量p
    Action()
    {
        //lr对指针的支持;
        char var ='A';//定义字符
        char *s =&var;//定义指向字符的指针,取字符的地址
        char *name ="LoadRunner";//定义指针,将字符串赋值给指针。
        lr_output_message("var=%c",*s);//以字符的形式输出指针
        lr_output_message("name=%s",name);//以字符串的形式输出指针
        return 0;
    }
    Action2()
    {
        char p[]={'A','B',''};//这种方式赋值,需要手动加上/0。因为输出的时候,只有遇到才结束.不加的话 会输出ABLoad Runner!!!。尽量不要用这种方式赋值
        char s[]="Load Runner!!!";//这种方式赋值,会自动加上
        lr_output_message("p=%s",p);
        lr_output_message("s=%s",s);

          lr_output_message("%d",sizeof(s)); //返回15.多了一个看不到的。这个不建议用
          lr_output_message("%d",strlen(s)); //返回14。用这个。

    return 0;
    }
        char a[5] ="ABCDE";//因为定义的是5,所以自动加上的被丢了。所以还要往后打。应该把长度改成>5的数值。保证数据都能装下
        char b ='a';
        lr_output_message("a数组的输出是:%s",a);
    //运行结果:a数组的输出是:ABCDEa
    //LR 对判断循环语句的支持
            int m=0;
            int n=5;
            int a[5]={1,2,3,4,5};
            int i=0;
            if(m==0){
                lr_output_message("m=0");
            }
            else
                { lr_output_message("m<>0");
            }
             lr_output_message("m++是:%d",m++);
             lr_output_message("++n是:%d",++n);
             for(i=0;i<5;i++){
                 lr_output_message("a[%d]=%d",i,a[1]);
             }
             lr_output_message("*********************");
             while(n>0){
                 lr_output_message("in while");
                n--;
             }

     需要注意的事项:

    1、注意中文的分号。注意全角半角

    2、变量使用前尽量初始化。如果不初始化,可能是0,可能是空指针等。

    3、字符数组尽量少用。尽量用字符串

    -----------------------------------------------------------------------------------------------------

    LoadRunner中的三种类型的函数:

    a、通用函数 lr开头的(例如日志函数,思考时间函数...)

    b、与编程语言相关的函数

    c、与协议相关的函数

    自定义函数

    Action()
    {
        vuser_init(); //action init end 都是函数。可以互相调用。
           lr_output_message("action");
        return 0;
    }
    Action()
    {
        int a=1;
        int b=2;
        lr_output_message("a+b=%d",sum(a,b));
        return 0;
    }
    sum(int a,int b){
        return a+b;
    }

    将sum函数放入c:/a.h 文件中,在globals.h中加上#include "c:/a.h" 也可以正常调用。

    或者 file-addfilesscript将a.h引用。并在action最上方引入#include "a.h" 这里不需要加路径,不需要加分号。

    查某一个函数的帮助。F1

    通用函数解析

    lr_think_time()

    lr_get_host_name( )

        char * my_host;
        my_host =lr_get_host_name();
        lr_output_message("%s",my_host);

    lr_whoami()

    int id, scid;
    char *vuser_group;
    lr_whoami(&id, &vuser_group, &scid);
    lr_message( "Group: %s, vuser id: %d, scenario id %d",vuser_group, id, scid);

    lr_get_attrib_string函数的使用

    1)通过运行时设置使用

    2)通过命令行

    第一种使用方法:获取全局变量的值。

        char *p =lr_get_attrib_string("hello");
        lr_output_message("%s",p);  
            //返回结果
            Action.c(13): (null)

    设置hello的值

    继续运行返回:Action.c(13): abcd

    第二种使用方法:命令行传入。

    LoadRunner错误机制解析

    1、 LoadRunner错误处理机制 2、lr_continue_on _error函数解析

    运行时,是mmdrv.ext进程执行脚本.

    lr_continue_on _error 会覆盖运行时设置的设置。

    lr_continue_on_error(1); 后面发生错误继续执行

    lr_continue_on_error(0);后面发生错误结束执行

    日志函数解析

    lr_output_message 不仅在本地写,还会上传到主机、 lr_log_message 只在本地写 、 lr_message 、 lr_error_message区别

    调试代码

    1) F10 一步一步的执行 F9断点

    2) 右键代码-- 可以选择快速打开脚本目录

    3) 右键代码-- 可以选择快速定位到回放日志的地方

    与编程语言相关的函数(可在文档中查到。)

    1) strcpy 拷贝一个字符串到另一个字符串 与 strcat 连接2个字符串

    Action()
    {
        char fullpath[1024],*filename ="logfile.txt";
        strcpy(fullpath,"c:\tmp"); //拷贝一个字符串到另一个字符串中。在头进行添加
        lr_output_message("fullpath after strcpy:%s",fullpath);
        strcat(fullpath,"\"); //连接2个字符串,在尾进行添加
        strcat(fullpath,filename);
        lr_output_message("Full path of file is %s",fullpath);
        return 0;
    }
    //运行结果
    Action.c(5): fullpath after strcpy:c:	mp
    Action.c(8): Full path of file is c:	mplogfile.txt

    2) strcmp函数

    3) atoi函数解析

    atoi 类型转换 前面是数字进行转换,后面不是的进行丢弃。

    Action()
    {
        int i=0;
        char *s="7元";
        i = atoi(s); // 类型转换 前面是数字进行转换,后面不是的进行丢弃。
        lr_output_message("price $%d",i);
        return 0;
    }
    //运行结果
    Action.c(6): price $7

    4)sprinf 将字符串组合成特定的格式

    Action()
    {
        int index =56;
        char filename[64],*suffix="txt";
        sprintf(filename,"log_%d_%s",index,suffix);
        lr_output_message("the new file name is %s",filename);
        return 0;
    }
    //执行结果
    Action.c(6): the new file name is log_56_txt

    5) time  以秒的形式,返回197001010000到现在的时间差。

    Action()
    {
        typedef long time_t; 
        time_t t; 
        lr_message ("Time in seconds since 1/1/70: %ld
    ", time(&t)); 
        lr_message ("Formatted time and date: %s", ctime(&t)); 
        return 0;
    }
    运行结果:
    Time in seconds since 1/1/70: 1498489129 Formatted time and date: Mon Jun 26 22:58:49 2017

    6) 文件操作.

    与协议相关的函数

    1)web_link 与 web_url (都是get)

    2)web_submit_form 与 web_submit_data (都是POST) web_submit_form中的hidden自动发送

    3)web_custom_request 自定义请求,可上传文件。

    z4)web_add_header 添加指定的头给下一个http请求。web_add_auto_header 下面所有的请求都加上

    Action()
    {
        web_add_header("ggg", "myLoadRunner"); 
        web_url("WebTours", 
            "URL=http://127.0.0.1:1080/WebTours/", 
            "Resource=0", 
            "RecContentType=text/html", 
            "Referer=", 
            "Snapshot=t10.inf", 
            "Mode=HTTP", 
            LAST);
        return 0;
    }
    //运行结果
    Action.c(4):     ggg: myLoadRunner
    

    5)web_get_int_property 拿到HTTP request的返回信息

    int HttpRetCode; 
    web_url("my_home", 
        "URL=http://my_home", 
        "TargetFrame=_TOP", 
        LAST ); 
    HttpRetCode = web_get_int_property(HTTP_INFO_RETURN_CODE); 
    if (HttpRetCode == 200) 
        lr_log_message("The script successfully accessed the My_home home page"); 
        else 
        lr_log_message("The script failed to access the My_home home page "); 
    } 

    需要注意的

    在脚本的任何系统函数中,都不能使用C语言函数。在系统函数之间可以任意使用C元素。如果在函数中药使用C语言的变量,LR提供参数化的功能。

    DLL解析

    lr_load_dll 加载一个外部的dll

    vuser_init()
    {
       lr_load_dll("c:\md5.dll"); //md5.dll,自己准备。有一个方法Calculate,将字符串用MD5加密
        return 0;
    }
    Action()
    {
        char *p="myLoadRunner";
        int len =strlen(p);
        char *result=(char *)Calculate(p,len);
        lr_output_message("MD5的结果是:%s",result);
        return 0;
    }
    //返回MD5加密的字符串
  • 相关阅读:
    2020软件工程个人作业06——软件工程实践总结作业
    git上传文件夹内容
    git常用命令(部分)
    java命令行输入参数
    2020软件工程作业05
    软件工程——问题清单
    2020软件工程作业04
    2020软件工程作业03
    2020软件工程作业02
    问题清单
  • 原文地址:https://www.cnblogs.com/milanmi/p/7081168.html
Copyright © 2020-2023  润新知