loadrunner之脚本篇——将内容保存为参数

在VuGen中默认使用{}的字符串称为参数

注意:参数必须在双引号中才能用

 

将字符串保存为参数

lr_save_string("string you want to save", "arg_name");

 

举例:用参数来替换需要打开的url链接

Action2()

{  

    lr_save_string("http://172.25.75.2:1080/WebTours/", "web_site");

 

    //打开登录页面

    web_url("WebTours",

        "URL = {web_site}",   //运行出错//改成"URL= {web_site}"即可

        "Resource=0",

        "RecContentType=text/html",

        "Referer=",

        "Snapshot=t1.inf",

        "Mode=HTML",

        LAST);

 

    return 0;

}

运行报错:

Action2.c(6): Error -27226: The "URL = http://172.25.75.2:1080/WebTours/" argument (number 2) is unrecognized or misplaced    [MsgId: MERR-27226]

Action2.c(6): web_url("WebTours") highest severity level was "ERROR", 0 body bytes, 0 header bytes  [MsgId: MMSG-26388]

 

解决方法:

"URL = {web_site}",URL和等号”=”之间多出了一个空格,去掉该空格即可。

所以使用lr_eval_string()函数的时候也是使用双引号来调用的。

 

还可以如下方式

Action2()

 

{  

    lr_save_string("http://172.25.75.2:1080/", "web_site");

    lr_save_string("WebTours/", "web_name");

 

    //打开登录页面

    web_url("WebTours",

        "URL={web_site}{web_name}",

        "Resource=0",

        "RecContentType=text/html",

        "Referer=",

        "Snapshot=t1.inf",

        "Mode=HTML",

        LAST);

 

    return 0;

}

 

获取参数值的字符串表识

可用lr_eval_string函数获取参数值的字符串标表示,然后用lr_output_message()函数输出结果

Action2()

{  

    lr_save_string("http://172.25.75.2:1080/", "web_site");

    lr_save_string("WebTours/", "web_name");

 

    lr_output_message(lr_eval_string("获取参数值的字符串表示:{web_site}{web_name}"));

 

    //打开登录页面

    web_url("WebTours",

        "URL= {web_site}{web_name}",

        "Resource=0",

        "RecContentType=text/html",

        "Referer=",

        "Snapshot=t1.inf",

        "Mode=HTML",

        LAST);

 

     return 0;

}

 

注:如果想获取参数字符串的第一个字母,同c,可以这样:lr_eval_string(“{param}”)[0];

 

int型数字保存为参数

lr_save_int(int_number, “param_name”)

例如:

Action2()

    lr_save_int(0, "int_param");

 

    //打开登录页面

    web_url("WebTours",

        "URL=http://172.25.75.2:1080/WebTours/",

//      "Resource=0",

        "Resource={int_parma}",

        "RecContentType=text/html",

        "Referer=",

        "Snapshot=t1.inf",

        "Mode=HTML",

        LAST);

 

    return 0;

}

 

把时间保存为参数

通过lr_save_datetime函数来实现。

函数原型:

void lr_save_datetime(const char *format, int offset, const char *name);

format:期望输出的日期格式,如:%Y、%m、%d、%X等

offset:类似与表示时间的一些关键字常量:

DATE_NOW -> 现在的日期

TIME_NOW -> 现在的时间

ONE_DAY -> 一天的时间

ONE_HOUR -> 一小时的时间

ONE_MIN -> 一分钟的时间

需要注意的是,他们可以单独使用,也可以联合使用

DATE_NOW + TIME_NOW -> 当前时间

DATE_NOW-ONE_DAY -> 昨天

DATE_NOW+ONE_DAY -> 明天

两天前的日期

 

 

DATE_NOW-2*(ONE_DAY)、 DATE_NOW-2*24*(ONE_HOUR)、 DATE_NOW-2*24*60*(ONE_MIN)

2个小时后的时间

TIME_NOW+2*(ONE_HOUR)

TIME_NOW+2*60*(ONE_MIN)

 

name:期望将时间保存到的那个参数的名称

 

format格式参照表:

 

Code

Description

%a

day of week, using locale's abbreviated weekday names

%A

day of week, using locale's full weekday names

%b

month, using locale's abbreviated month names

%B

month, using locale's full month names

%c

date and time as %x %X

%d

day of month (01-31)

%H

hour (00-23)

%I

hour (00-12)

%j

number of day in year (001-366)

%m

month number (01-12)

%M

minute (00-59)

%p

locale's equivalent of AM or PM, whichever is appropriate

%S

seconds (00-59)

%U

week number of year (01-52), Sunday is the first day of the week. Week number 01 is the first week with four or more January days in it.

%w

day of week; Sunday is day 0

%W

week number of year (01-52), Monday is the first day of the week. Week number 01 is the first week with four or more January days in it.

%x

date, using locale's date format

%X

time, using locale's time format

%y

year within century (00-99)

%Y

year, including century (for example, 1988)

%Z

time zone abbreviation

%%

to include the "%" character in your output string

 

 
 
举例:

Action()

{

    lr_save_datetime("%X",TIME_NOW,"time");

    lr_save_datetime("%Y-%m-%d",DATE_NOW,"date");

    lr_save_datetime("%Y-%m-%d %X",DATE_NOW+TIME_NOW,"datetime");

    lr_save_datetime("%Y-%m-%d",DATE_NOW-ONE_DAY,"yesterday");

    lr_output_message(lr_eval_string("系统的当前时间为:{time}"));

    lr_output_message(lr_eval_string("系统的当前日期为:{date}"));  

    lr_output_message(lr_eval_string("系统的当前日期,当前时间:{datetime}"));

    lr_output_message(lr_eval_string("昨天的日期为:{yesterday}"));

 

    return 0;

}

 

运行结果:

Starting iteration 1.

Starting action Action.

Action.c(7): 系统的当前时间为:12:27:54

Action.c(8): 系统的当前日期为:2014-10-22

Action.c(9): 系统的当前日期,当前时间:2014-10-22 12:27:54

Action.c(10): 昨天的日期为:2014-10-21

Ending action Action.

Ending iteration 1.

 

把内容保存为带格式的参数

lr_param_sprintf(param_name,format,var1,var2,…);

示例:

Action2()

{

    int index = 56;

    char *suffix = "txt";

    lr_param_sprintf("NewParam","log_%d.%s",index,suffix);

    lr_output_message("The new file name is %s",lr_eval_string("{NewParam}"));

    return 0;

}

 

运行结果:

Starting action Action2.

Action2.c(24): The new file name is log_56.txt

Ending action Action2.

转载于:https://www.cnblogs.com/langhuagungun/p/8352281.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值