LoadRunner通過驗證碼并實現成功登錄的方法
需要安裝的軟件
1、安裝ImageMagick安裝完成后,將其安裝路徑添加到環境變量path中
2、安裝Tesseract-OCR
#define MAX_NAME_LEN 4 //定義驗證碼字符串的長度,這里是4位 int flen; //定義一個整型變量保存獲得文件的大小 long filedes; long filedess; //保存文件句柄 char file[256]="c:\\test1.jpg"; //保存文件路徑及文件名 char result[4];//定義用于從文件中讀取驗證碼的數組 web_set_max_html_param_len("2000000"); //設置頁面接收最大的字節數,該設置應大于下載文件的大小 //下載含有驗證碼的網頁元素,并將其保存到參數中 web_reg_save_param_ex( "ParamName=pic", "LB=", "RB=", SEARCH_FILTERS, "Scope=Body", "RequestUrl=*/action/ValidateCodeServlet*", LAST); //關聯登錄時與服務器產生的會話ID web_reg_save_param_ex( "ParamName=CorrelationParameter_1", "LB=Set-Cookie: JSESSIONID=", "RB=; Path", SEARCH_FILTERS, "Scope=All", "RequestUrl=*/192.168.12.1:8080/*", LAST); //訪問測試網站 web_url("192.168.12.1:8080", "URL=http://192.168.12.1:8080/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t1.inf", "Mode=HTML", EXTRARES, "Url=/images/login_screenbg.jpg", ENDITEM, "Url=/images/login_midbg.jpg",ENDITEM, "Url=/images/login_contentbg.jpg", ENDITEM, "Url=/images/login_btn.png", ENDITEM, "Url=/images/login_input1.png",ENDITEM, "Url=/images/login_input2.png",ENDITEM, "Url=/images/reset_btn.png",ENDITEM, LAST); flen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE); |
//獲得下載圖片的文件大小 if(flen > 0) { if((filedes = fopen(file,"wb")) == NULL) { lr_output_message("oh cloud your Open File Failed!"); return -1; } fwrite( lr_eval_string("{pic}"),flen,1, filedes ); fclose( filedes ); } system( "c:\\test1.bat" ); //在CMD命令行中運行OCR命令 /* 在C盤根目錄下創建批處理文件test1.bat: #進入到含有驗證碼圖片的目錄 c: cd\ #轉換下載的JPG文件為TIF convert.exe -compress none -depth 8 -alpha offtest1.jpg test1.tif #調用tesseract轉換圖片中的文件并保存到test1.txt文件中 tesseract.exe test1.tif test1 #執行完成后,自動退出命令行 exit */ if((filedess = fopen("c:\\test1.txt","rt")) == NULL) { lr_output_message("oh,cloud your Open File Failed!"); return -1; } fread( result,4,1,filedess );//從文件中讀取驗證碼 fclose( filedess ); lr_output_message("result: %s", result); //打印出讀取后的數據 lr_save_var(result,MAX_NAME_LEN,0,"VC" ); //保存指定位數的數據到變量中 web_submit_data("UserAction", "Action=http://192.168.12.1:8080/action/UserAction", "Method=POST", "RecContentType=text/html", "Referer=http://192.168.12.1:8080/", "Snapshot=t2.inf", "Mode=HTML", ITEMDATA, "Name=method","Value=login", ENDITEM, "Name=goPage","Value=/index.html", ENDITEM, "Name=currentPage","Value=/", ENDITEM, "Name=username","Value=admin", ENDITEM, "Name=password","Value=123456", ENDITEM, "Name=ValidateCode","Value={VC}", ENDITEM, //輸入獲取的驗證碼 "Name=loginSubmit", "Value=登錄", ENDITEM, EXTRARES, ... LAST); |
posted on 2013-12-19 11:10 順其自然EVO 閱讀(2322) 評論(0) 編輯 收藏 所屬分類: loadrunner