【原创】Loadrunner+Tesseract-OCR 识别验证码实现自动注册
发现一个网站注册时发送给手机的验证码可以直接在网站上展出出来,考虑可以使用LR与OCR结合尝试自动注册
http://elearning.yunxuetang.cn/portal/enterpriseregister.htm
Tesseract-OCR 下载
在code.google上下载tesseract-ocr-setup-3.02.02.exe
安装后实验
http://elearning.yunxuetang.cn/portal/enterpriseregister.htm
填写手机号后等待30秒获得带有验证码的图片
图片另存为test.png
tesseract test.png out
loadrunner使用tesseract-ocr时需要通过bat实现
bat内容如下
----------------------------------------------------------------------------------------
c:
cd\
tesseract.exe test.png out digits
cd\
tesseract.exe test.png out digits
----------------------------------------------------------------------------------------
------------------在config中修改digits文件,将1234567890加入则只识别数字--------------![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmoAAAA5CAIAAAAJNZBYAAAR+ElEQVR4nO2d+18TV97H549K7W5e3WfX7na33ZdV12W7fZ6aqttXo8jWS1VERY2pVWt1Re4QJZFLgCTkwiVcBAsKEtjILQiiIpdwmXCpoICAYp4fBiYnc8tJZgIBvu/X54dk5uScM585M585MwkQlW1kRStZ3jpibxnu87zetjvm5avF/uHZ/uHZuTfv5t68o16DQCAQCASiRZQ2DxQ39dua+myO3ucjU599qZx6tTgwMjswMjv35l3vi0GT9S71FgQCgUAgECXC/OCJ6X63sfax4RdXz+D4J7v2Tr1aHBiZo2SyVpus1fRbEAgEAoFAAyNzREF1W35VS16FM9fe3NU3+ofPv7JXO0EgEAgEAgmIyC5r1JU0ZNke3DbXuZ65f/fpP6YAAACATYx3EyDeJSLLUqsx1WQaqtPyK9uf9P/2TzvFVwoAAACsU7xer3sTEB0dLdIoIsNwN62gIjmvLDGnuLWrd8vWbQwfARpwBh/wSlrATzGAe0HhdrsXDR9teOl0OpEJSqQXVKTmlSVlF9/UWVseP//g939Fx9laXx9EFrTX4ExAwCtpAT/FAO7hA/EZRHym5dtTcksT79gStOaWx89k//MZOs5qAQTKbnAGB/BKWsBPMYB7+EB8BhGf6QWVqXp7Uk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zzDNPutz6NSsz1GpcuohPgEAAIB1z8rsk7p5Oxvem7edpYkQnwAAAMAGgOj0vKfk8oQ7PutzQv13K0MQnwAAAEAk8f/YVwg1KnzHbAAAAABJRU5ErkJggg==)
------------------在config中修改digits文件,将1234567890加入则只识别数字--------------
参数设置
action脚本
Action() { int flen; //定义一个整型变量保存获得文件的大小 long filein; long filedinn; //保存文件 char filepath[256]="c:\\test1.jpg"; //保存文件路径及文件名 char result[20];//从文件中读取验证码 web_set_max_html_param_len("2000000"); web_reg_save_param("pic", "LB=", "RB=", "SEARCH=BODY", //获取响应的全部内容,作为文件内容保存 (图片路径在body里面) LAST); web_url("index.html", "URL=http://test.qianwang365.com/usercenter/index.html", //含有验证码的注册页面 "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t3.inf", "Mode=HTML", EXTRARES, "Url=http://elearning.yunxuetang.cn/Services/DrawNewRegisterValidateCode.ashx", //验证码图片的地址 ENDITEM, LAST); flen = web_get_int_property(HTTP_INFO_DOWNLOAD_SIZE); //获得下载图片的文件大小 if(flen > 0) { if((filedes = fopen(file,"wb")) == NULL) { lr_output_message("Open Failed!"); return -1; } fwrite( lr_eval_string("{pic}"),flen,1, filedin); fclose( filedin ); } system("c:\\test.bat"); //在CMD命令行中运行OCR命令 lr_output_message("result: %s", filedin); //打印出读取后的数据 /* 在C盘根目录下创建批处理文件test1.bat: #进入到含有验证码图片的目录 c: cd\ tesseract.exe test.png out #执行完成后,自动退出命令行 exit */ if((filedess = fopen("c:\\out.txt","rt")) == NULL) { lr_output_message("Open txt Failed!"); return -1; } fread( result,6,1,filedinn);//从文件中读取验证码 fclose( filedinn ); lr_output_message("result: %s", result); //打印出读取后的数据 lr_error_message("result: %s",lr_eval_string(result)); lr_save_string(result,""); web_custom_request("ExistsRegisterMobile", "URL=http://elearning.yunxuetang.cn/Services/CommonService.svc/ExistsRegisterMobile", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=http://elearning.yunxuetang.cn/portal/enterpriseregister.htm", "Snapshot=t6.inf", "Mode=HTML", "EncType=text/json; charset=UTF-8", "Body={\"key\":\"{NewParam}\"}", LAST); web_custom_request("ExistsRegisterMobile_2", "URL=http://elearning.yunxuetang.cn/Services/CommonService.svc/ExistsRegisterMobile", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=http://elearning.yunxuetang.cn/portal/enterpriseregister.htm", "Snapshot=t7.inf", "Mode=HTML", "EncType=text/json; charset=UTF-8", "Body={\"key\":\"{NewParam}\"}", LAST); web_custom_request("SendValidateCodeToMobile", "URL=http://elearning.yunxuetang.cn/Services/CommonService.svc/SendValidateCodeToMobile", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=http://elearning.yunxuetang.cn/portal/enterpriseregister.htm", "Snapshot=t8.inf", "Mode=HTML", "EncType=text/json; charset=UTF-8", "Body={\"mobile\":\"{NewParam}\"}", LAST); lr_think_time(22); web_custom_request("ExistsByOrganizationName", "URL=http://elearning.yunxuetang.cn/Services/CommonService.svc/ExistsByOrganizationName", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=http://elearning.yunxuetang.cn/portal/enterpriseregister.htm", "Snapshot=t9.inf", "Mode=HTML", "EncType=text/json; charset=UTF-8", "Body={\"organizationName\":\"{NewParam}\"}", EXTRARES, "Url=http://i.firefoxchina.cn/cclv/v2/patterns.json", "Referer=", ENDITEM, "Url=http://i.firefoxchina.cn/skipsb/v1/urls.json", "Referer=", ENDITEM, LAST); web_custom_request("ExistsByDomainName", "URL=http://elearning.yunxuetang.cn/Services/CommonService.svc/ExistsByDomainName", "Method=POST", "Resource=0", "RecContentType=application/json", "Referer=http://elearning.yunxuetang.cn/portal/enterpriseregister.htm", "Snapshot=t10.inf", "Mode=HTML", "EncType=text/json; charset=UTF-8", "Body={\"domainName\":\"{NewParam}.yunxuetang.cn\"}", LAST); lr_think_time(50); web_submit_data("enterpriseregister.htm_2", "Action=http://elearning.yunxuetang.cn/portal/enterpriseregister.htm", "Method=POST", "RecContentType=text/html", "Referer=http://elearning.yunxuetang.cn/portal/enterpriseregister.htm", "Snapshot=t11.inf", "Mode=HTML", ITEMDATA, "Name=__EVENTTARGET", "Value=", ENDITEM, "Name=__EVENTARGUMENT", "Value=", ENDITEM, "Name=__VIEWSTATE", "Value={Siebel_Analytic_ViewState2}", ENDITEM, "Name=txtMobileNumber2", "Value=", ENDITEM, "Name=txtValidateCode2", "Value=输入短信验证码", ENDITEM, "Name=txtEnterpriseName", "Value={NewParam}", ENDITEM, "Name=rad", "Value=on", ENDITEM, "Name=txtWebSiteName", "Value={NewParam}", ENDITEM, "Name=txtValidateCode", "Value={NewParam}", ENDITEM, "Name=chkAcceptProtocol", "Value=on", ENDITEM, "Name=btnSubmit", "Value=", ENDITEM, "Name=hfImagePath", "Value=", ENDITEM, "Name=hfInageSelectedIndex", "Value=2", ENDITEM, "Name=hfIndustryTemplateID", "Value=49903f80-7c0b-4b0f-8f46-c8822534bb5e", ENDITEM, "Name=hfIndustryTemplateName", "Value=通用/其他", ENDITEM, "Name=hfEnterpriseName", "Value=", ENDITEM, "Name=hfDomainName", "Value=", ENDITEM, "Name=hfValidateCode", "Value=", ENDITEM, "Name=hfValidateCode2", "Value=", ENDITEM, "Name=hfMobileNumber", "Value=", ENDITEM, "Name=hfCurrentOrgID", "Value=", ENDITEM, "Name=hfMobileNumber2", "Value={NewParam}", ENDITEM, "Name=hfMobileNumber3", "Value=", ENDITEM, "Name=hfAgentName", "Value=", ENDITEM, "Name=hfImgCheckCode", "Value={VC}", ENDITEM, "Name=hfBusinessLicenseUrl", "Value=", ENDITEM, "Name=hfInviteNo", "Value=afe31852-fba5-46f9-975a-84928824dddf", ENDITEM, "Name=hfRandomKey", "Value=a000e7fe-1e6b-431f-88c2-f51446fc04ee", ENDITEM, "Name=hfObligateOrganizationID", "Value=45669dda-c007-4812-9b60-b2e6e42f24ce", ENDITEM, LAST); return 0; }
执行结果
登录页面http://18888888888.yunxuetang.cn/login.htm
换一个不存在的http://18888888890.yunxuetang.cn/login.htm,应该是跳到了一个固定的网页。