【原创】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
----------------------------------------------------------------------------------------
 
------------------在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=", 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,应该是跳到了一个固定的网页。
 
 
 

 

posted @ 2014-11-26 17:16  从小不打伞  阅读(945)  评论(0编辑  收藏  举报