Identification Verification Code image in Ruby + watir Automated Testing

Source: Internet
Author: User

A loadrunner was developed a few days ago to use tesseract-ocr to identify the verification code. Although the recognition accuracy is not too high, some verification code images cannot be recognized, however, the correct verification code can also be obtained through the loop method. This method is not recommended in performance testing because of high concurrency pressure. However, I am 100% in favor of functional automation testing. Functional automation is not as concurrent as performance automation for the same operation (LOGIN.

The automated framework we use is a set of UI framework built based on ruby + watir. By identifying objects in the UI, we can perform automated functional tests to detect the health status of system functions.

There are also gem packages for processing images in ruby, for example, Runtime (which needs to be installed). I am using the version of the ruby1.9.2 platform. I am not doing it myself. after a long time of configuration, I still cannot debug it, the methods in require "rtesseract" are not correct. It seems to be related to the ruby version. In the end, discard this method and use the following method to implement it. You do not need to install any gem package, it is easier.

1、install tesseract-ocr-setup-3.02.02.exe and download
2. configure a batch of processing files to the root directory of the C drive, and use this batch to execute tesseract.exe.
Batch content:

c:cd C:\Program Files\Tesseract-OCRtesseract.exe c:\CheckImg.jpg c:\CheckCode -l

3. ruby script:
A. First, obtain the image of the verification code;

B. Call the batch files under drive C;

C. Obtain the verification code in txt;
4. Make this script into a def. Call this def in the login test case and return a CheckCode.

Def CheckCode_ OK (url, gifurl, CheckCode) # obtain the verification code image require 'net/http' net: http. start (url) do | http | resp = http. get (gifurl) File. open ("C: \ CheckImg.jpg", "wb") do | file. write (resp. body) file. closeendend # execute the batch processing File system ("c: \ CheckBat. bat ") # obtain the verification code in txt if File. exists? ("C: \ CheckCode.txt") = trueFile. open ("c: \ CheckCode.txt", "r") do | line | CheckCode = line. readlineline. closeendendputs CheckCodereturn CheckCodeend

The method is as follows:

CheckCode (url, gifurl, CheckCode)
Url: the url that contains the verification code image, such as: https://memberprod.alipay.com/account/reg/index.htm

Gifurl: the url of the Verification Code image, such as: https://omeo.alipay.com/service/checkcode? SessionID = fb70db3212e3c44bb5e909b51841adb5 & r = 0.48708463088160914

CheckCode: ruturn verification code for return

Optimized the Script: reduced the parameter passing in the method to two

Def CheckCode_ OK (gifurl, CheckCode) # obtain the verification code image require 'net/http' # net: http. start (url) do | http | # resp = http. get (gifurl) resp = Net: HTTP. get_response (URI (gifurl) File. open ("C: \ CheckImg.jpg", "wb") do | file. write (resp. body) file. closeend # end # execute the batch processing File system ("c: \ CheckBat. bat ") # obtain the verification code in txt if File. exists? ("C: \ CheckCode.txt") = trueFile. open ("c: \ CheckCode.txt", "r") do | line | CheckCode = line. readlineline. closeendendputs CheckCodereturn CheckCodeend

Call:

Def xxx_www_login (user, pwd) LoadObject (".. /.. /testcase/xxx. yaml ") times = 0 loop dotimes + = 1 code = CheckCode_ OK (" http://xxx.xxxx.com/CheckImg.aspx/CheckImg.gif ", code) puts codesleep 10 # after obtaining the latest verification code, add a sleep wait time, it seems that IE needs to cache the latest Verification code; otherwise, the @ B object fails to obtain the latest verification code and the login fails if @ B. text. include? (ExpectData ("expect1") = true # Is there a "Merchant login" xxx_login (user, pwd, code) elsif @ B. text. include? (ExpectData ("expect2") = true # Is there "exit" xxx_www_logoutxxx_login (user, pwd, code) endbreak if times> = 5 or @ B. text. include? (ExpectData ("expect2") = true # Whether "exit" exists"
 end #loop end 
end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.