脚本开发-参数化之将内容保存为参数、参数数组及参数值获取 by:授客QQ:1033553122 在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) isunrecognized 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 dayof week, using locale"s abbreviated weekday names %A dayof week, using locale"s full weekday names %b month,using locale"s abbreviated month names %B month,using locale"s full month names %c dateand time as %x %X %d dayof month (01-31) %H hour(00-23) %I hour(00-12) %j numberof day in year (001-366) %m monthnumber (01-12) %M minute(00-59) %p locale"sequivalent of AM or PM, whichever is appropriate %S seconds(00-59) %U weeknumber of year (01-52), Sunday is the first day of the week. Weeknumber 01 is the first week with four or more January days init. %w dayof week; Sunday is day 0 %W weeknumber of year (01-52), Monday is the first day of the week. Weeknumber 01 is the first week with four or more January days init. %x date,using locale"s date format %X time,using locale"s time format %y yearwithin century (00-99) %Y year,including century (for example, 1988) %Z timezone abbreviation %% toinclude 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-2212:27:54 Action.c(10): 昨天的日期为:2014-10-21 Ending action Action. Ending iteration 1. 把内容保存为带格式的参数 lr_param_sprintf(param_name,format,var1,var2,…); 示例: Action2() { intindex = 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; } 运行结果: Startingaction Action2. Action2.c(24):The new file name is log_56.txt Endingaction Action2. 把内容保存到参数数组 这个概念lr9.x后才有 参数数组必须满足以下两个条件: 1.参数必须都是以相同的名字开头,后面接下划线加数字的方式顺序赋值。 2.参数数组必须有一个"参数名_count"的参数来记录数组的长度 相关函数: lr_paramarr_idx()//获取参数数组中指定编号的参数的值 lr_paramarr_len()//获取参数数组的长度 lr_paramarr_random()//从参数列表中随机获取一个参数 例子:要创建一个访问网站的参数数组,可以编写以下代码 说明:通过脚本创建了一个名为website的参数数组,并获取编号为2的参数的值, 运行结果: 此处:web_site= lr_paramarr_idx("website", 2),等同:lr_eval_string("{website_2}"); 获取参数数组长度 例子: Action2() { intarray_size = 0; char*arr_size_str = NULL; lr_save_string("www.google.com", "website_1"); lr_save_string("www.google.com", "website_2"); lr_save_string("www.google.com", "website_3"); lr_save_string("www.google.com", "website_4"); lr_save_string("4", "website_count"); array_size = lr_paramarr_len("website"); //输出整数 //1.把结果array_size保存为参数 lr_save_int(array_size, "arr_size"); //2.获取参数的字符串表示 arr_size_str = lr_eval_string("{arr_size}"); //输出结果 lr_output_message(arr_size_str); return 0; } 运行结果: 从参数列表中随机获取一个参数 例子: Action2() { char*web_site = NULL; lr_save_string("www.google.com", "website_1"); lr_save_string("www.google.com", "website_2"); lr_save_string("www.google.com", "website_3"); lr_save_string("www.google.com", "website_4"); lr_save_string("4", "website_count"); web_site = lr_paramarr_idx("website", 2); return 0; } 运行结果: 例子:按顺序输出每个参数 Action2() { int i= 0; lr_save_string("www.google.com", "website_1"); lr_save_string("www.google.com", "website_2"); lr_save_string("www.google.com", "website_3"); lr_save_string("www.google.com", "website_4"); lr_save_string("4", "website_count"); for (i=0; i { lr_output_message(lr_paramarr_idx("website", i)); } return 0; } 输出结果 用指针变量存放参数 Action2() { char*pt = NULL; lr_save_string("hello world", "param"); pt ="{param}"; lr_output_message(pt); lr_output_message(lr_eval_string(pt)); return 0; } 运行结果: