LoadRunner脚本编写学习

1、Loadrunner自带了一个小飞机订票系统

LoadRunner脚本编写学习_第1张图片
image.png

2、启动 Start Web server 服务,若启动成功,桌面右下角会有一个绿色的X ;
3、打开浏览器访问 http://127.0.0.1:1080/WebTours/
4、使用loadrunner录制一个登录过程。

Action()
{ 
    web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t1.inf", 
        "Mode=HTML", 
        LAST);

    lr_rendezvous("集合点");

    lr_start_transaction("登陆时间");
    web_submit_form("login.pl", 
        "Snapshot=t2.inf", 
        ITEMDATA, 
        "Name=username", "Value=test1", ENDITEM, 
        "Name=password", "Value=123456", ENDITEM, 
        "Name=login.x", "Value=59", ENDITEM, 
        "Name=login.y", "Value=5", ENDITEM, 
        LAST);
    lr_end_sub_transaction("登陆时间",LR_ABORT);

    return 0;
}  

5、使用乘法运算,求员工工资的总合

#define COUNT 100
#define SALARY 4000

Action()
{
    int total;
    total = COUNT * SALARY;
    lr_output_message("100人合计薪资支出为:%d",total);

    return 0 ;
}

#define COUNT 100
#define SALARY 4000     定义一个全局常量。
lr_output_message    输出语句,这个和我们所学的C 语言不一样,在C中我们会用println 来输入结果。

运行结果

..........
Starting iteration 1.
Starting action Action.
Action.c(9): 100人合计薪资支出为:400000    ----这里将运行结果打印输出
Ending action Action.
Ending iteration 1.
..........

6、通过随即数和分支(选择)语句switch来完成一个小程序

Action()
{
    int randomnumber;   
    randomnumber = rand() % 3+1;

    switch (randomnumber) {
    
    case 1:
        {
            lr_rendezvous("访问百度集合点");

            web_url("www.baidu.com", 
        "URL=http://www.baidu.com/", 
                "Resource=0", 
                    "RecContentType=text/html", 
                    "Referer=", 
                    "Snapshot=t12.inf", 
                    "Mode=HTML", 
                    EXTRARES, 
                    "Url=http://s1.bdstatic.com/r/www/img/i-1.0.0.png", ENDITEM, 
                    "Url=/favicon.ico", "Referer=", ENDITEM, 
                    "Url=http://s1.bdstatic.com/r/www/img/bg-1.0.0.gif", ENDITEM, 
                    LAST);

            return 0;
        }
    case 2:
        {
            lr_rendezvous("访问谷歌集合点");

            web_url("www.google.com.hk", 
                    "URL=http://www.google.com.hk/", 
                    "Resource=0", 
                    "RecContentType=text/html", 
                    "Referer=", 
                    "Snapshot=t10.inf", 
                    "Mode=HTML", 
                    EXTRARES, 
                    "Url=http://ssl.gstatic.com/gb/images/b_8d5afc09.png", ENDITEM, 
                    "Url=/images/srpr/logo3w.png", ENDITEM, 
                    "Url=/extern_chrome/749e1ce3c3e5f171.js", ENDITEM, 
                    "Url=/images/swxa.gif", ENDITEM, 
                    "Url=/favicon.ico", "Referer=", ENDITEM, 
                    "Url=http://ssl.gstatic.com/gb/js/sem_0e1289051da7e9e3697c2025d9490acd.js", ENDITEM, 
                    "Url=http://www.google.com/textinputassistant/tia.png", ENDITEM, 
                    LAST);

            return 0;
        }
    case 3:
        {
            lr_rendezvous("访问有道集合点");

            web_url("www.youdao.com", 
                    "URL=http://www.youdao.com/", 
                    "Resource=0", 
                    "RecContentType=text/html", 
                    "Referer=", 
                    "Snapshot=t7.inf", 
                    "Mode=HTML", 
                    EXTRARES, 
                    "Url=http://shared.ydstatic.com/oxo/p/pic.gif", ENDITEM, 
                    "Url=http://shared.ydstatic.com/oxo/p/logo.png?1", ENDITEM, 
                    "Url=http://shared.ydstatic.com/oxo/p/nv_line.gif", ENDITEM, 
                    "Url=http://shared.ydstatic.com/r/2.0/p/pic.gif", ENDITEM, 
                    LAST);

            return 0;
        }     
    }   
}  

上面的程序,分别录制了百度、谷歌、有道访问三个网的首页的代码,若在运行脚本时,随机的去访问其中一个网站,如何做呢?通过随机函数,随机出1~3之间的整数,根据随机来的结果,然后执行switch语句中的代码。

运行结果
为了查看脚本结果更清晰,我在每一段脚本前面加了一个集合点“lr_rendezvous”函数。

Starting iteration 1.
Starting action Action.
Action.c(57): Rendezvous 访问有道集合点
Action.c(59): Downloading resource "http://shared.ydstatic.com/oxo/p/pic.gif" (specified by argument number 9)      [MsgId: MMSG-26577]
Action.c(59): Downloading resource "http://shared.ydstatic.com/oxo/p/logo.png?1" (specified by argument number 11)      [MsgId: MMSG-26577]
Action.c(59): Downloading resource "http://shared.ydstatic.com/oxo/p/nv_line.gif" (specified by argument number 13)      [MsgId: MMSG-26577]
Action.c(59): Downloading resource "http://shared.ydstatic.com/r/2.0/p/pic.gif" (specified by argument number 15)      [MsgId: MMSG-26577]
Action.c(59): Found resource "http://shared.ydstatic.com/dao/search/outweb/js/yd.js?201207131" in HTML "http://www.youdao.com/"      [MsgId: MMSG-26659]
Action.c(59): Found resource "http://shared.ydstatic.com/dao/search/outweb/js/nav.js?201207131" in HTML "http://www.youdao.com/"      [MsgId: MMSG-26659]
Action.c(59): Found resource "http://shared.ydstatic.com/dao/search/outweb/js/suggest.js?201207131" in HTML "http://www.youdao.com/"      [MsgId: MMSG-26659]
Action.c(59): web_url("www.youdao.com") was successful, 30006 body bytes, 3347 header bytes, 39 chunking overhead bytes      [MsgId: MMSG-26385]
Ending action Action.
Ending iteration 1.

7、通过脚本读取文件
文件test.txt ,里面随便输入些内容!

Action()
{
    int count,total=0;
    char buffer [50];
    long file_stream;
    char * filename = "C:\\test.txt";  //读取文件的存放位置

    //判断是否可以读取文件
    if((file_stream =fopen(filename,"r"))==NULL)   
    {
        lr_error_message("不能打开%s文件!",filename);

        return -1;
    }

    while(!feof(file_stream))
    {
        count=fread(buffer,sizeof(char),50,file_stream);  //从文件中读取50个字符
        total=total+count;   //字符个数计数
        if(total>=50)
        {
            fclose(file_stream);  //关闭文件
            lr_output_message("文件的前50字符:%s",buffer);
            break;  //退出循环
        }

    }
    return 0;
}

运行结果

Starting iteration 1.
Starting action Action.
Action.c(24): 文件的前50字符:01234567890123456789012345678901234567890123456789
Ending action Action.
Ending iteration 1.

你可能感兴趣的:(LoadRunner脚本编写学习)