來至:gotesting軟件測試聯盟論壇 http://www.78test.com
原創vuser腳本片段:
lr_start_transaction("trans_login");
//設置檢查的內容,在savecount變量裏面記錄查找到的次數
web_reg_find("Text=歡迎您","SaveCount=loginss_count",LAST);
//登錄(登錄錄製的自動生成代碼)
web_submit_data("loginAction.do;jsessionid=16D156A310B3804B354123F7CDEE5211",
"Action=http://****/loginAction.do;jsessionid={JSESSIONID2}",
"Method=POST",
............
LAST);
//檢查登錄是否成功
// if(strcmp(lr_eval_string("{loginss_count}"),"0")==0) 這兩個if等效
if(atoi(lr_eval_string("{loginss_count}"))==0)
// lr_output_message("login failed登錄失敗!");
lr_error_message("login failed登錄失敗!username:%s,pwd:%s",lr_eval_string("{username}"),lr_eval_string("{pwd})"));//打印出錯誤信息,同時把登錄錯誤時的用戶名、密碼打印出來。
else
lr_output_message("login successful登錄成功!");
lr_end_transaction("trans_login", LR_AUTO);
網上摘錄的web_reg_find用法:
web_reg_find
該函數的作用是“在緩存中查找相應的內容”,常用參數及含義如下:
web_reg_find("Search=Body", //定義查找範圍
"SaveCount=ddd", //定義查找計數變量名稱
"Text=aaaa", //定義查找內容
LAST);
使用該函數注意以下事項:
1、 位置
該函數寫在要查找內容的請求之前,通常情況下寫在如下六個函數之前:
Web_castom_request();web_image();web_link();web_submit_data();web_submit_form();web_url()
2、 使用技巧
在該函數的參數中有個“SaveCount”,該參數可以記錄在緩存中查找內容出現的次數,我們可以使用該值,來判斷要查找的內容是否被找到,下面舉個例子來說明:(引用LR的幫助中的例子)
// Run the Web Tours sample
web_url("MercuryWebTours",
"URL=http://localhost/MercuryWebTours/",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
LAST);
// Set up check for successful login by looking for "Welcome"
web_reg_find("Text=Welcome",
"SaveCount=Welcome_Count",
LAST);
// Now log in
web_submit_form("login.pl",
"Snapshot=t2.inf",
..........
LAST);
// Check result
if (atoi(lr__string("{Welcome_Count}")) > 0){ //判斷如果Welcome字符串出現次數大於0
lr_output_message("Log on successful."); }//在日誌中輸出Log on successful
else{ //如果出現次數小於等於
lr_error_message("Log on failed"); //在日誌中輸出Log on failed
return(0); }
http://blog.163.com/tech_qa/blog/static/1301763492009101724535999/