腳本開發(fā)-參數(shù)化之將內(nèi)容保存為參數(shù)、參數(shù)數(shù)組及參數(shù)值獲取
by:授客QQ:1033553122
在VuGen中默認(rèn)使用{}的字符串稱為參數(shù)
注意:參數(shù)必須在雙引號(hào)中才能用
將字符串保存為參數(shù)
lr_save_string("string you want to save", "arg_name");
舉例:用參數(shù)來替換需要打開的url鏈接
Action2()
{
}
運(yùn)行報(bào)錯(cuò):
Action2.c(6): Error -27226: The "URL =http://172.25.75.2:1080/WebTours/" argument (number 2) isunrecognized or misplaced
Action2.c(6): web_url("WebTours") highest severity level was"ERROR", 0 body bytes, 0 header bytes
解決方法:
"URL = {web_site}",URL和等號(hào)”=”之間多出了一個(gè)空格,去掉該空格即可。
所以使用lr_eval_string()函數(shù)的時(shí)候也是使用雙引號(hào)來調(diào)用的。
還可以如下方式
Action2()
{
}
獲取參數(shù)值的字符串表識(shí)
可用lr_eval_string函數(shù)獲取參數(shù)值的字符串標(biāo)表示,然后用lr_output_message()函數(shù)輸出結(jié)果
Action2()
{
}
注:如果想獲取參數(shù)字符串的第一個(gè)字母,同c,可以這樣:lr_eval_string(“{param}”)[0];
將int型數(shù)字保存為參數(shù)
lr_save_int(int_number, “param_name”)
例如:
Action2()
{
//
}
把時(shí)間保存為參數(shù)
通過lr_save_datetime函數(shù)來實(shí)現(xiàn)。
函數(shù)原型:
void lr_save_datetime(const char *format, int offset, const char*name);
format:期望輸出的日期格式,如:%Y、%m、%d、%X等
offset:類似與表示時(shí)間的一些關(guān)鍵字常量:
DATE_NOW-> 現(xiàn)在的日期
TIME_NOW-> 現(xiàn)在的時(shí)間
ONE_DAY-> 一天的時(shí)間
ONE_HOUR-> 一小時(shí)的時(shí)間
ONE_MIN-> 一分鐘的時(shí)間
需要注意的是,他們可以單獨(dú)使用,也可以聯(lián)合使用
DATE_NOW + TIME_NOW -> 當(dāng)前時(shí)間
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個(gè)小時(shí)后的時(shí)間
TIME_NOW+2*(ONE_HOUR)
TIME_NOW+2*60*(ONE_MIN)
name:期望將時(shí)間保存到的那個(gè)參數(shù)的名稱
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()
{
}
運(yùn)行結(jié)果:
Starting iteration 1.
Starting action Action.
Action.c(7): 系統(tǒng)的當(dāng)前時(shí)間為:12:27:54
Action.c(8): 系統(tǒng)的當(dāng)前日期為:2014-10-22
Action.c(9): 系統(tǒng)的當(dāng)前日期,當(dāng)前時(shí)間:2014-10-2212:27:54
Action.c(10): 昨天的日期為:2014-10-21
Ending action Action.
Ending iteration 1.
把內(nèi)容保存為帶格式的參數(shù)
lr_param_sprintf(param_name,format,var1,var2,…);
示例:
Action2()
{
}
運(yùn)行結(jié)果:
Startingaction Action2.
Action2.c(24):The new file name is log_56.txt
Endingaction Action2.
把內(nèi)容保存到參數(shù)數(shù)組
這個(gè)概念lr9.x后才有
參數(shù)數(shù)組必須滿足以下兩個(gè)條件:
1.參數(shù)必須都是以相同的名字開頭,后面接下劃線加數(shù)字的方式順序賦值。
2.參數(shù)數(shù)組必須有一個(gè)“參數(shù)名_count”的參數(shù)來記錄數(shù)組的長(zhǎng)度
相關(guān)函數(shù):
lr_paramarr_idx()
lr_paramarr_len()
lr_paramarr_random()
例子:要?jiǎng)?chuàng)建一個(gè)訪問網(wǎng)站的參數(shù)數(shù)組,可以編寫以下代碼
說明:通過腳本創(chuàng)建了一個(gè)名為website的參數(shù)數(shù)組,并獲取編號(hào)為2的參數(shù)的值,
運(yùn)行結(jié)果:
此處:web_site= lr_paramarr_idx("website", 2),等同:lr_eval_string(“{website_2}”);
獲取參數(shù)數(shù)組長(zhǎng)度
例子:
Action2()
{
}
運(yùn)行結(jié)果:
從參數(shù)列表中隨機(jī)獲取一個(gè)參數(shù)
例子:
Action2()
{
}
運(yùn)行結(jié)果:
例子:按順序輸出每個(gè)參數(shù)
Action2()
{
}
輸出結(jié)果
用指針變量存放參數(shù)
Action2()
{
}
運(yùn)行結(jié)果:
聯(lián)系客服