A complete guide to identifying and processing verification codes in the web automation framework, making testing more convenient!

Foreword:

With the continuous development of web applications, automated testing has become an essential part of project development. However, the presence of CAPTCHAs often makes automated testing more challenging. In order to solve this problem, we need a method to automatically identify and process verification codes, thereby improving the efficiency and accuracy of automated testing. This article will introduce how to encapsulate a web automation framework and integrate verification code recognition and processing functions.

1. Construction of Web automated testing framework

1. Choose an automated testing tool

There are many automated testing tools to choose from, such as Selenium, Appium, etc. We chose Selenium as our automated testing tool because Selenium has many useful features such as powerful browser support and Active Element Detection.

2. Installation environment

- Python3.x: Selenium is written in Python, and Python3.x needs to be installed before use.

- Selenium: Using Selenium in a Python environment requires installing the Selenium package. It can be installed using the pip command.

pip install selenium

3. Initialize a Selenium driver

After installing the Selenium module, you can get a WebDriver object. We use WebDriver to connect to a browser instance and load the web page we want to test.

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://tempuri.org')

At this time, we can see that the Chrome browser automatically opens and opens the website "http://tempuri.org".

2. Add automated test cases

1. Write test scripts

Write a simple Selenium test script to add a new user and verify whether the user can log in successfully:

def test_add_user():

# 查找用户登录界面的"用户名"和"密码"输入框
username_input = driver.find_element_by_name('username')
password_input = driver.find_element_by_name('password')

# 输入用户名和密码
username_input.send_keys('username')
password_input.send_keys('password')

# 记录当前 URL
login_url = driver.current_url

# 点击登录按钮
driver.find_element_by_id('submit').click()

# 获取当前 URL
current_url = driver.current_url

# 验证登录成功与否
assert current_url != login_url

Through the find_element_by_* series of methods provided by Selenium, we can locate elements in the page and perform operations.

2. Run the test script

After adding the test script, you only need to run the test script to carry out automated testing.

test_add_user()
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:691998057【暗号:csdn999】

3. Verification code identification and processing

CAPTCHAs are generally used to prevent malicious machine attacks, which is a big problem in automated testing. Through some common verification code forms and corresponding processing methods, we can improve the recognition rate and efficiency of automated testing.

1. Type of verification code

Currently, common types of verification codes include graphic verification codes, Chinese verification codes, calculation verification codes, sliding verification codes, etc., but the most common one is the graphic verification code.

2. Verification code processing method

We can use the PIL library to preprocess the recognized verification codes to improve the recognition rate. For verification codes that cannot be recognized in some cases, manual coding is usually required. The following are the basic steps for graphic verification code preprocessing:

- Convert to grayscale: Convert the colored verification code into a single grayscale.

- Binarization: Use the Sobel algorithm to perform edge detection on grayscale images and perform binarization.

- Noise reduction: Remove noise points and other interference information in the binarized image.

- Cutting: Cut the verification code into single characters, and then identify the single characters.

Let's take a look at an automated test script that implements verification code recognition processing. Assuming that the website we want to test contains a graphical verification code, we can preprocess the verification code first and then enter the verification code.

import requests
from PIL import Image
import pytesseract

def get_verify_code_img(url):
"""
获取验证码图片
"""
response = requests.get(url=url)
img = Image.open(BytesIO(response.content))
return img

def preprocessing_verify_code(img):
"""
预处理验证码图片
"""
# 转换成灰度图
img = img.convert('L')
# 二值化
threshold = 160
img = img.point(lambda x: 255 if x > threshold else 0)
# 去除噪声
img = img.filter(ImageFilter.MedianFilter(size=3))
return img

def recognize_verify_code(img):
"""
识别验证码
"""
text = pytesseract.image_to_string(img, lang='eng')
return text

def test_with_verify_code():
# 验证码图片URL
img_url = 'http://captcha.com/verify_code'
# 获取验证码图片
img = get_verify_code_img(img_url)
# 预处理验证码图片
img = preprocessing_verify_code(img)
# 识别验证码
verify_code = recognize_verify_code(img)
# 输入验证码并登录
driver.find_element_by_id('verify_code_input').send_keys(verify_code)
driver.find_element_by_id('submit').click()

test_with_verify_code()

In this example, we use the requests library to obtain the verification code image, use the PIL library to preprocess the verification code image, and then use the pytesseract library for identification. Finally, we enter the identified verification code into the web page to complete the automated test.

4. Conclusion

This article describes how to encapsulate a Web automation framework and integrate verification code recognition and processing functions. It should be noted that for different verification code types, the preprocessing and identification methods may be different and need to be adjusted and optimized according to the actual situation. In this way, we can improve the efficiency and accuracy of automated testing and achieve higher quality testing.

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/135034998