transferred from: http://www.cnblogs.com/fnng/p/3606934.html
Note: Automated testing in this title includes performance testing and UI-level automated testing
Often asked how to solve the problem of verification code, here to record a few of the ways I know.
For Web applications, most systems require users to enter a verification code when they log in, many of the types of verification codes, alphanumeric, Chinese characters, and even user input of an arithmetic question, for the system to use the verification code can have an effect to prevent using machine guessing method of the password spying, Increased security to some extent. But for testers, whether it's performance testing or automated testing is a tricky issue.
Here are a few ways to deal with verification codes.
Remove the verification code
This is the simplest way, for developers, just to comment out the code codes, if it is in the test environment, this can save the testers a lot of trouble, if the automation script is to run in the formal environment, so that the system poses a certain risk.
set the Universal code
Remove the verification code is primarily a security issue, in order to deal with the security threats of online systems, you can modify the program without canceling the verification code, but the program to leave a "backdoor"---set a "Universal Verification code", as long as the user input this "Universal Verification Code", the program is considered to pass the verification, Otherwise, it is verified by the original verification method.
#coding =utf-8
Import random
#生成0到10之间的随机数
#d = Random.uniform (0,10)
#print D
# Generates a random integer between 1000 and 9999
d = random.randint (1000,9999)
print U "generated random number:%d"%d
i = input (u "Please enter random number:")
print I
if i = = d:
print U "login successful!!"
elif i = = 1111:
print u "login successful!!"
else:
print U "Please reenter the verification code." "
Operation Result:
>>> ================================ RESTART ================================
>>>
Generated random number: 3764
Please enter random number: 1111
1111
login successful!!
>>> ================================ RESTART ================================
>>>
Generated random number: 3763
Please enter random number: 3763
3763
login successful!!
>>> ================================ RESTART ================================
>>>
Generated random number: 1928
Enter a random number: 1354646
1354646 Please reenter the
verification code.
Random
Random is used to generate stochastic numbers
Randint ()
The Randint () method is used to generate a random integer, and the two arguments passed are the range of the random number, and the second parameter of Randint (1000,9999) is greater than the first argument.
We ask the user to enter a random number, and to determine the user input, if equal to the generated random number, then, the login succeeds, if equal to 1111 also counted the login success, otherwise failed. Then the judgment equals 1111 is a universal code.
Verification Code Identification Technology
For example, the python-tesseract can be used to identify the image verification code, Python-tesseract is the optical character recognition Tesseract OCR engine Python wrapper class. Ability to read any regular picture files (JPG, GIF, PNG, TIFF, etc.). However, there are many types of verification codes on the market at present, any verification code identification technology, the recognition rate is not 100%.
Record Cookies
(For UI Automation testing, the user name password currently in the large app is not recorded in a cookie or encrypted processing.) )
Adding cookies to your browser bypasses the sign-in verification code, which is a more interesting solution. We can use the Add_cookie () method to write the user name password to the browser cookie before the user logs in, and the system login link will be automatically logged in again. For example, the following way:
....
#访问xxxx网站
driver.get ("http://www.xxxx.cn/")
#将用户名密码写入浏览器cookie
driver.add_cookie ({' name ': ' Login_ Usernumber ', ' value ': ' username '})
Driver.add_cookie ({' name ': ' login_passwd ', ' value ': ' Password '})
# Visit the XXXX website again, will automatically login
driver.get ("http://www.xxxx.cn/")
time.sleep (3) ...
.
Driver.quit ()
The biggest difficulty in using cookies to log in is how to get the name of the username and password, and if the name cannot be found, it is not possible to lose the user name and password information to value.
I suggest that you can get the name of the name object of the user name and password by using the Get_cookies () method to obtain all the cookie information of the login, and of course, the simplest way is to ask the front-end developer.
Reference: Automated Testing-Skip login verification codes with cookies
Summary:
The simplest safe, effective way is to set the universal code, a little and development communication is OK. If you are willing to "work hard to self-reliance" can also study verification code identification technology.