首页 > 代码库 > Loadrunner+Tesseract-OCR 识别验证码实现自动注册
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加入则只识别数字--------------
------------------在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="http://www.mamicode.com/, ENDITEM,
"Name=__EVENTARGUMENT", "Value="http://www.mamicode.com/, ENDITEM,
"Name=__VIEWSTATE", "Value=http://www.mamicode.com/{Siebel_Analytic_ViewState2}", ENDITEM,
"Name=txtMobileNumber2", "Value="http://www.mamicode.com/, ENDITEM,
"Name=txtValidateCode2", "Value=http://www.mamicode.com/输入短信验证码", ENDITEM,
"Name=txtEnterpriseName", "Value=http://www.mamicode.com/{NewParam}", ENDITEM,
"Name=rad", "Value=http://www.mamicode.com/on", ENDITEM,
"Name=txtWebSiteName", "Value=http://www.mamicode.com/{NewParam}", ENDITEM,
"Name=txtValidateCode", "Value=http://www.mamicode.com/{NewParam}", ENDITEM,
"Name=chkAcceptProtocol", "Value=http://www.mamicode.com/on", ENDITEM,
"Name=btnSubmit", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfImagePath", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfInageSelectedIndex", "Value=http://www.mamicode.com/2", ENDITEM,
"Name=hfIndustryTemplateID", "Value=http://www.mamicode.com/49903f80-7c0b-4b0f-8f46-c8822534bb5e", ENDITEM,
"Name=hfIndustryTemplateName", "Value=http://www.mamicode.com/通用/其他", ENDITEM,
"Name=hfEnterpriseName", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfDomainName", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfValidateCode", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfValidateCode2", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfMobileNumber", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfCurrentOrgID", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfMobileNumber2", "Value=http://www.mamicode.com/{NewParam}", ENDITEM,
"Name=hfMobileNumber3", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfAgentName", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfImgCheckCode", "Value=http://www.mamicode.com/{VC}", ENDITEM,
"Name=hfBusinessLicenseUrl", "Value="http://www.mamicode.com/, ENDITEM,
"Name=hfInviteNo", "Value=http://www.mamicode.com/afe31852-fba5-46f9-975a-84928824dddf", ENDITEM,
"Name=hfRandomKey", "Value=http://www.mamicode.com/a000e7fe-1e6b-431f-88c2-f51446fc04ee", ENDITEM,
"Name=hfObligateOrganizationID", "Value=http://www.mamicode.com/45669dda-c007-4812-9b60-b2e6e42f24ce", ENDITEM,
LAST);
return 0;
}
执行结果
登录页面http://18888888888.yunxuetang.cn/login.htm
换一个不存在的http://18888888890.yunxuetang.cn/login.htm,应该是跳到了一个固定的网页。
Loadrunner+Tesseract-OCR 识别验证码实现自动注册
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。