loadrunner 用for迴圈寫入資料
阿新 • • 發佈:2019-01-10
lr中for迴圈寫入資料 引數化指令碼
“ Lr_save_string()函式和Itoa()函式的使用
/*
目的:使用for迴圈新增多條有規律的資料,比如說:a1、a2、a3......a1000
方法:從上面看1-1000是一個有序序列,並且前面的a都是固定的,所以只需要用for迴圈將1-1000迴圈出來就可以了。
但是loadrunner使用的是C語言的語法,所以不能向在java中一樣,將兩個變數直接相加(+)(+:連字元)
所以這個時候就需要將首先將int變數轉換成string,然後再用lr_save_string()函式將該變數用loadrunner可以識別的引數化方式儲存起來,再應用到錄製的指令碼中就可以了
函式簡介:
int itoa ( int value, char *str, int radix );
函式目的:將int型別轉換成string
引數介紹:value: 要轉換的int型的值
str: 目標字串,即將轉換成的string值儲存到str中
radix:轉換數字時所用的基數 10:十進位制;2:二進位制……
int lr_save_string (const char *param_value, const char *param_name);
函式目的:將param_value值儲存到param_name變數中
引數介紹:param_value:要儲存的值
param_name: 變數名稱
問題:為什麼要用itoa()和lr_save_string()兩個函式相結合使用呢?
因為lr_save_string()中的兩個引數都是char型別的指標,int型別的值必須經過轉換才能在lr_save_string()函式中使用,所以此處就要將itoa()和lr_save_string()兩個函式相結合使用。
*/
Action()
{
int i;
char str[100]; //定義一個數組用來儲存int型別轉換後的值
for(i=0;i<=9;i++){
itoa(i,str,10);
lr_save_string(str,"ID");
lr_output_message("==str:=%s==ID:=%s====",str,lr_eval_string("{ID}"));
}
/*
用lr_save_string()將陣列儲存到變數中以後就可以應用到Loadrunner錄製的指令碼中了
{ID}:是loardrunner識別引數的方式
web_url("login",
"URL=http://server0.im.sonoro.cn:18001/login?username=a{ID}&password=test&autoLogin=false&1211433835878&1211433835878",
"TargetFrame=",
"Resource=0",
"RecContentType=text/html",
"Referer=http://server0.im.sonoro.cn:18001/proxy.html?1211433821547",
"Snapshot=t6.inf",
"Mode=HTML",
LAST);
*/
return 0;
}
”
錄製的一個範例指令碼如下:
Action()
{
int i;
char str[100]; //定義一個數組用來儲存int型別轉換後的值
web_url("sms",
"URL=http://127.0.0.1:7001/sms",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
LAST);
web_add_cookie("VERSION=ZHUJI; DOMAIN=127.0.0.1");
lr_think_time(11);
web_submit_form("com.dvnchina.fw.svt.BfMain",
"Snapshot=t2.inf",
ITEMDATA,
"Name=XT_LOGIN_AREAID", "Value=057502", ENDITEM,
"Name=XT_LOGIN_NETID", "Value=057502ck", ENDITEM,
"Name=XT_LOGIN_ID", "Value=szf", ENDITEM,
"Name=XT_LOGIN_PASSWORD", "Value=1", ENDITEM,
"Name=B3", "Value=登 入", ENDITEM,
EXTRARES,
"Url=../jsp/images/arrow.gif", "Referer=http://127.0.0.1:7001/sms/jsp/login/welcome.jsp", ENDITEM,
LAST);
web_url("submenu.jsp",
"URL=http://127.0.0.1:7001/sms/jsp/submenu.jsp?subMenuID=21",
"Resource=0",
"RecContentType=text/html",
"Referer=http://127.0.0.1:7001/sms/jsp/login/welcome.jsp",
"Snapshot=t3.inf",
"Mode=HTML",
LAST);
web_url("com.dvnchina.fw.svt.BfMain_2",
"URL=http://127.0.0.1:7001/sms/servlet/com.dvnchina.fw.svt.BfMain?ctrlBeanName=com.dvnchina.sms.warehouse.ctrl.Jump2STBBaseCtrl",
"Resource=0",
"RecContentType=text/html",
"Referer=http://127.0.0.1:7001/sms/jsp/submenu.jsp?subMenuID=21",
"Snapshot=t4.inf",
"Mode=HTML",
EXTRARES,
"Url=../jsp/images/arrow.gif", "Referer=http://127.0.0.1:7001/sms/servlet/com.dvnchina.fw.svt.BfMain?ctrlBeanName=com.dvnchina.sms.warehouse.ctrl.Jump2STBBaseCtrl", ENDITEM,
LAST);
lr_think_time(7);
for(i=0;i<=9;i++){
itoa(i,str,10);
lr_output_message("==str:=%s==ID:=%s====",str,lr_eval_string("{ID}"));
lr_save_string(str,"ID");
web_submit_data("com.dvnchina.fw.svt.BfMain_3",
"Action=http://127.0.0.1:7001/sms/servlet/com.dvnchina.fw.svt.BfMain",
"Method=POST",
"RecContentType=text/html",
"Referer=http://127.0.0.1:7001/sms/servlet/com.dvnchina.fw.svt.BfMain?ctrlBeanName=com.dvnchina.sms.warehouse.ctrl.Jump2STBBaseCtrl",
"Snapshot=t5.inf",
"Mode=HTML",
ITEMDATA,
"Name=ctrlBeanName", "Value=com.dvnchina.sms.warehouse.ctrl.InitAddItemsCtrl", ENDITEM,
"Name=AUTOVIRTUALPAIR", "Value=FALSE", ENDITEM,
"Name=ICTYPE_H", "Value=", ENDITEM,
"Name=STBID", "Value=hh{ID}", ENDITEM,
"Name=ICID", "Value=", ENDITEM,
"Name=UnitAddress", "Value=", ENDITEM,
"Name=SENDCMD", "Value=ON", ENDITEM,
"Name=Location", "Value=057502ck", ENDITEM,
"Name=Performance", "Value=1", ENDITEM,
"Name=STBType", "Value=STB04", ENDITEM,
"Name=ICType", "Value=TF", ENDITEM,
"Name=STBProducer", "Value=", ENDITEM,
"Name=ICProducer", "Value=", ENDITEM,
"Name=STBProduceDate", "Value=2010-7-8", ENDITEM,
"Name=CardProduceDate", "Value=2010-7-8", ENDITEM,
"Name=STBPrice", "Value=", ENDITEM,
"Name=CardPrice", "Value=", ENDITEM,
"Name=STBGoodsNo", "Value=", ENDITEM,
"Name=ICGoodsNo", "Value=", ENDITEM,
"Name=STBProvider", "Value=", ENDITEM,
"Name=ICProvider", "Value=", ENDITEM,
"Name=STBInDate", "Value=2010-7-8", ENDITEM,
"Name=ICInDate", "Value=2010-7-8", ENDITEM,
"Name=STBRemark", "Value=", ENDITEM,
"Name=ICRemark", "Value=", ENDITEM,
EXTRARES,
"Url=../jsp/images/arrow.gif", ENDITEM,
LAST);
}
return 0;
}
“ Lr_save_string()函式和Itoa()函式的使用
/*
目的:使用for迴圈新增多條有規律的資料,比如說:a1、a2、a3......a1000
方法:從上面看1-1000是一個有序序列,並且前面的a都是固定的,所以只需要用for迴圈將1-1000迴圈出來就可以了。
但是loadrunner使用的是C語言的語法,所以不能向在java中一樣,將兩個變數直接相加(+)(+:連字元)
所以這個時候就需要將首先將int變數轉換成string,然後再用lr_save_string()函式將該變數用loadrunner可以識別的引數化方式儲存起來,再應用到錄製的指令碼中就可以了
函式簡介:
int itoa ( int value, char *str, int radix );
函式目的:將int型別轉換成string
引數介紹:value: 要轉換的int型的值
str: 目標字串,即將轉換成的string值儲存到str中
radix:轉換數字時所用的基數 10:十進位制;2:二進位制……
int lr_save_string (const char *param_value, const char *param_name);
函式目的:將param_value值儲存到param_name變數中
引數介紹:param_value:要儲存的值
param_name: 變數名稱
問題:為什麼要用itoa()和lr_save_string()兩個函式相結合使用呢?
因為lr_save_string()中的兩個引數都是char型別的指標,int型別的值必須經過轉換才能在lr_save_string()函式中使用,所以此處就要將itoa()和lr_save_string()兩個函式相結合使用。
*/
Action()
{
int i;
char str[100]; //定義一個數組用來儲存int型別轉換後的值
for(i=0;i<=9;i++){
itoa(i,str,10);
lr_save_string(str,"ID");
lr_output_message("==str:=%s==ID:=%s====",str,lr_eval_string("{ID}"));
}
/*
用lr_save_string()將陣列儲存到變數中以後就可以應用到Loadrunner錄製的指令碼中了
{ID}:是loardrunner識別引數的方式
web_url("login",
"URL=http://server0.im.sonoro.cn:18001/login?username=a{ID}&password=test&autoLogin=false&1211433835878&1211433835878",
"TargetFrame=",
"Resource=0",
"RecContentType=text/html",
"Referer=http://server0.im.sonoro.cn:18001/proxy.html?1211433821547",
"Snapshot=t6.inf",
"Mode=HTML",
LAST);
*/
return 0;
}
”
錄製的一個範例指令碼如下:
Action()
{
int i;
char str[100]; //定義一個數組用來儲存int型別轉換後的值
web_url("sms",
"URL=http://127.0.0.1:7001/sms",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
LAST);
web_add_cookie("VERSION=ZHUJI; DOMAIN=127.0.0.1");
lr_think_time(11);
web_submit_form("com.dvnchina.fw.svt.BfMain",
"Snapshot=t2.inf",
ITEMDATA,
"Name=XT_LOGIN_AREAID", "Value=057502", ENDITEM,
"Name=XT_LOGIN_NETID", "Value=057502ck", ENDITEM,
"Name=XT_LOGIN_ID", "Value=szf", ENDITEM,
"Name=XT_LOGIN_PASSWORD", "Value=1", ENDITEM,
"Name=B3", "Value=登 入", ENDITEM,
EXTRARES,
"Url=../jsp/images/arrow.gif", "Referer=http://127.0.0.1:7001/sms/jsp/login/welcome.jsp", ENDITEM,
LAST);
web_url("submenu.jsp",
"URL=http://127.0.0.1:7001/sms/jsp/submenu.jsp?subMenuID=21",
"Resource=0",
"RecContentType=text/html",
"Referer=http://127.0.0.1:7001/sms/jsp/login/welcome.jsp",
"Snapshot=t3.inf",
"Mode=HTML",
LAST);
web_url("com.dvnchina.fw.svt.BfMain_2",
"URL=http://127.0.0.1:7001/sms/servlet/com.dvnchina.fw.svt.BfMain?ctrlBeanName=com.dvnchina.sms.warehouse.ctrl.Jump2STBBaseCtrl",
"Resource=0",
"RecContentType=text/html",
"Referer=http://127.0.0.1:7001/sms/jsp/submenu.jsp?subMenuID=21",
"Snapshot=t4.inf",
"Mode=HTML",
EXTRARES,
"Url=../jsp/images/arrow.gif", "Referer=http://127.0.0.1:7001/sms/servlet/com.dvnchina.fw.svt.BfMain?ctrlBeanName=com.dvnchina.sms.warehouse.ctrl.Jump2STBBaseCtrl", ENDITEM,
LAST);
lr_think_time(7);
for(i=0;i<=9;i++){
itoa(i,str,10);
lr_output_message("==str:=%s==ID:=%s====",str,lr_eval_string("{ID}"));
lr_save_string(str,"ID");
web_submit_data("com.dvnchina.fw.svt.BfMain_3",
"Action=http://127.0.0.1:7001/sms/servlet/com.dvnchina.fw.svt.BfMain",
"Method=POST",
"RecContentType=text/html",
"Referer=http://127.0.0.1:7001/sms/servlet/com.dvnchina.fw.svt.BfMain?ctrlBeanName=com.dvnchina.sms.warehouse.ctrl.Jump2STBBaseCtrl",
"Snapshot=t5.inf",
"Mode=HTML",
ITEMDATA,
"Name=ctrlBeanName", "Value=com.dvnchina.sms.warehouse.ctrl.InitAddItemsCtrl", ENDITEM,
"Name=AUTOVIRTUALPAIR", "Value=FALSE", ENDITEM,
"Name=ICTYPE_H", "Value=", ENDITEM,
"Name=STBID", "Value=hh{ID}", ENDITEM,
"Name=ICID", "Value=", ENDITEM,
"Name=UnitAddress", "Value=", ENDITEM,
"Name=SENDCMD", "Value=ON", ENDITEM,
"Name=Location", "Value=057502ck", ENDITEM,
"Name=Performance", "Value=1", ENDITEM,
"Name=STBType", "Value=STB04", ENDITEM,
"Name=ICType", "Value=TF", ENDITEM,
"Name=STBProducer", "Value=", ENDITEM,
"Name=ICProducer", "Value=", ENDITEM,
"Name=STBProduceDate", "Value=2010-7-8", ENDITEM,
"Name=CardProduceDate", "Value=2010-7-8", ENDITEM,
"Name=STBPrice", "Value=", ENDITEM,
"Name=CardPrice", "Value=", ENDITEM,
"Name=STBGoodsNo", "Value=", ENDITEM,
"Name=ICGoodsNo", "Value=", ENDITEM,
"Name=STBProvider", "Value=", ENDITEM,
"Name=ICProvider", "Value=", ENDITEM,
"Name=STBInDate", "Value=2010-7-8", ENDITEM,
"Name=ICInDate", "Value=2010-7-8", ENDITEM,
"Name=STBRemark", "Value=", ENDITEM,
"Name=ICRemark", "Value=", ENDITEM,
EXTRARES,
"Url=../jsp/images/arrow.gif", ENDITEM,
LAST);
}
return 0;
}