Method and device for processing multiple common verification codes in automatic testing process
By combining Python, selenium and unittest frameworks, OCR technology is used to process multiple verification codes in automated testing, which solves the difficulty of logging in verification codes in automated testing and achieves efficient and safe automated inspections, suitable for multiple verification codes. type.
Patent Information
- Application Number
- CN202410940402.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-15
- Publication Date
- 2024-10-08
AI Technical Summary
During the automated testing process, existing technologies cannot effectively bypass the verification code login, requiring manual intervention, and setting up a universal verification code poses security risks, affecting the efficiency and security of automated inspections.
Use Python language combined with selenium and unittest frameworks, use OCR technology for image processing and verification code recognition, locate and process different types of verification codes (simple, slider, click), and introduce a failed rerun mechanism and image optimization technology. Complete verification code by simulating input and swiping operations.
It realizes the effective processing of multiple verification codes in automated testing, improves the pass rate of automatic login, reduces manual intervention, enhances security and efficiency, and is suitable for multiple verification code types.
Smart Images

Figure CN118747359A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of automated testing, and specifically provides a method and a device for processing a variety of common verification codes in an automated testing process. Background Art
[0002] The verification code mechanism is a public fully automatic program that distinguishes whether the user is a computer or a human. It can prevent the website from being maliciously cracked by passwords and causing server crashes due to large numbers of logins. Currently, logging in with a verification code is a common method used by many websites.
[0003] At the same time, with the popularization of informatization, a large number of systems are online, and daily inspections are required to ensure that their services are normal. Through automated testing, the workload of manual inspections can be greatly reduced. However, for systems that have been online, it is impossible to bypass login by blocking verification codes. The use of cookies also requires human intervention, and setting a universal verification code poses security risks. Summary of the invention
[0004] The present invention aims at the above-mentioned deficiencies of the prior art and provides a method for processing a variety of common verification codes in an automated testing process with strong practicability.
[0005] A further technical task of the present invention is to provide a device that is reasonably designed, safe and applicable for processing a variety of common verification codes in the process of automated testing.
[0006] The technical solution adopted by the present invention to solve its technical problem is:
[0007] A method for processing multiple common verification codes in an automated testing process comprises the following steps:
[0008] S1. Use Python as the main language, Selenium automated testing framework and Unittest unit testing framework, and use dddddocr, Python-tesseract or PIL encapsulation class for image processing;
[0009] S2. Use OCR technology to output image text through image noise reduction and image cutting;
[0010] S3. Use selenium automated testing to complete the location of the verification code;
[0011] S4. Process the verification code based on OCR technology;
[0012] S5. For a simple verification code, you only need to enter a combination of English or numbers;
[0013] S6. For the slider verification code, the sliding distance is calculated by comparing the sliding block with the missing image in the background image, and then the sliding operation is simulated with the help of selenium to complete the verification;
[0014] S7, click the verification code, that is, click the icons in a given order to pass the verification, and the verification code includes a picture and a Chinese character;
[0015] S8. Introduce a failure rerun mechanism with the help of unittestreport package;
[0016] S9. Optimize image processing.
[0017] Furthermore, in step S2, the image denoising is to remove some unnecessary information in the image. If the verification code is a colored background, each pixel is placed in a five-dimensional space, namely X, Y, R, G, B; X and Y are the two-dimensional plane coordinates of the pixel, and R, G, B represent the color corresponding to the pixel.
[0018] Furthermore, in step S3, selenium automated testing is used to complete the positioning of the verification code, the output of the position coordinates, the capture of the background image of the verification code, and after the image processing is completed, the output string or operation is simulated for input, thereby completing the processing of the verification code.
[0019] Furthermore, in step S4, a failure rerun mechanism is introduced, and the rerun in unittestreport in the unittest unit test framework is used to mark the login test. Specifically, two parameters count and interval are received, where count is the number of times the specified test case fails to be rerun, and interval is the time interval between each rerun;
[0020] At the same time, if the login verification fails, the execution of subsequent automated tests will be stopped.
[0021] Furthermore, in step S5, it further includes:
[0022] (1) Locate the verification code image and save it;
[0023] (2) Convert the color image into a grayscale image, convert RGB into HSI color space, and use the L component;
[0024] (3) Binarize the image, set the grayscale of pixels greater than a certain critical grayscale value to the maximum grayscale value, and set the grayscale of pixels less than this value to the minimum grayscale value. The value range is generally 0-1. Different binarization algorithms can be divided into fixed threshold and adaptive threshold.
[0025] (4) Enhance the image by using enhanced display or converting the image to black and white;
[0026] (5) Finally, perform text recognition on the processed image, obtain the output string, fill it into the input box and click login.
[0027] Furthermore, in step S6, it further includes:
[0028] (1) Locate the verification code and save the image;
[0029] (2) When saving an image, there may be the following situations: one is that the entire sliding verification code in the front end divides the image into several parts, and the order of the images is disrupted in the front end, and it needs to be restored according to the CSS offset;
[0030] The other is that the background image is a complete image. In either case, you need to download the complete background image and slider.
[0031] (3) Based on the complete background image and the incomplete image, use dddocr to process and obtain the gap distance;
[0032] (4) When using Selenium to simulate sliding, please note that the sliding block distance does not necessarily start from 0 before processing. It needs to be positioned again and the sliding distance is obtained after the difference is made.
[0033] Furthermore, in step S7, it further includes:
[0034] (1) Locate the verification code and save the image. First save the image that needs to be clicked, then save the background image.
[0035] (2) Cut the background image, locate the icon in the background image, and then cut it. The positioning method is processed with reference to the simple verification code to obtain the grayscale + binarization + enhanced icon;
[0036] (3) Compare the given icon with the cut icon for similarity and select the result with the highest similarity;
[0037] (4) Based on the results, use Selenium to simulate clicking and verify.
[0038] Furthermore, in step S9, it further includes:
[0039] (1) When processing images, use dynamic thresholding for binarization;
[0040] (2) When calculating the sliding distance, scale it according to the size of the verification code image;
[0041] (3) For Chinese character selection, model training is used.
[0042] A device for processing multiple common verification codes in an automated testing process, comprising: at least one memory and at least one processor;
[0043] The at least one memory is used to store a machine-readable program;
[0044] The at least one processor is used to call the machine-readable program to execute a method for processing multiple common verification codes in an automated testing process.
[0045] Compared with the prior art, the method and device for processing multiple common verification codes in the automated testing process of the present invention have the following outstanding beneficial effects:
[0046] The present invention can automatically log in to the information system on the Internet, solve the problem of manual login required during automated inspection, and save workload. It can support multiple verification codes, has a wide range of applications, and effectively improves the pass rate of verification codes. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0048] Attached Figure 1 The present invention is a flowchart of a method for processing various common verification codes in the process of automated testing. DETAILED DESCRIPTION
[0049] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0050] A best embodiment is given below:
[0051] like Figure 1 As shown, a method for processing multiple common verification codes in an automated test process in this embodiment has the following steps:
[0052] S1. Use Python as the main language, Selenium automated testing framework and Unittest unit testing framework, and use dddddocr, Python-tesseract or PIL encapsulation class for image processing;
[0053] S2. Use OCR technology to output image text through image noise reduction and image cutting;
[0054] Image noise reduction is to remove some unnecessary information in the image, such as background, interfering pixels, interfering lines, etc. If the verification code has a colored background, it actually means putting each pixel in a five-dimensional space, namely X, Y, R, G, B; X, Y are the two-dimensional plane coordinates of the pixel, and R, G, B represent the color corresponding to the pixel.
[0055] S3, supports selenium+unittest framework, uses selenium automated testing to complete the location of verification codes;
[0056] Use selenium automated testing to locate the verification code, output the location coordinates, capture the background image of the verification code, and after the image processing is completed, simulate the input of the output string or operation to complete the verification code processing.
[0057] S4. Process the verification code based on OCR technology;
[0058] There is a certain error, and wrong results may occur. For example, the English character "I" and the number "1" may be recognized incorrectly, so it is necessary to introduce a failed rerun mechanism. The rerun in unittestreport in the unittest unit test framework is used to mark the login test. Specifically, it receives two parameters count (used to specify the number of times the use case fails to be rerun) and interval (specify the time interval between each rerun).
[0059] At the same time, if the login verification fails, the execution of subsequent automated tests will be stopped.
[0060] S5. For a simple verification code, you only need to enter a combination of English or numbers;
[0061] The specific steps are:
[0062] (1) Locate the verification code image and save it;
[0063] (2) Convert the color image into a grayscale image, convert RGB into HSI color space, and use the L component;
[0064] (3) Binarize the image, set the grayscale of pixels greater than a certain critical grayscale value to the maximum grayscale value, and set the grayscale of pixels less than this value to the minimum grayscale value. The value range is generally 0-1. Different binarization algorithms can be divided into fixed threshold and adaptive threshold.
[0065] (4) In order to improve the correct recognition rate, image enhancement is performed, such as using enhanced image display or converting the image to black and white.
[0066] (5) Finally, perform text recognition on the processed image, obtain the output string, fill it into the input box and click login.
[0067] S6. For the slider verification code, the sliding distance is calculated by comparing the sliding block with the missing image in the background image, and then the sliding operation is simulated with the help of selenium to complete the verification;
[0068] The specific steps are:
[0069] (1) Locate the verification code and save the image;
[0070] (2) When saving an image, there may be the following situations: one is that the entire sliding verification code in the front end divides the image into several parts, and the order of the images is disrupted in the front end, and it needs to be restored according to the CSS offset;
[0071] The other is that the background image is a complete image. In either case, you need to download the complete background image and slider.
[0072] (3) Based on the complete background image and the incomplete image, use dddocr to process and obtain the gap distance;
[0073] (4) When using Selenium to simulate sliding, please note that the sliding block distance does not necessarily start from 0 before processing. It needs to be positioned again and the sliding distance is obtained after the difference is made.
[0074] S7, click the verification code, that is, click the icons in a given order to pass the verification, and the verification code includes a picture and a Chinese character;
[0075] The specific steps are:
[0076] (1) Locate the verification code and save the image. First save the image that needs to be clicked, then save the background image.
[0077] (2) Cut the background image, locate the icon in the background image, and then cut it. The positioning method is processed with reference to the simple verification code to obtain the grayscale + binarization + enhanced icon;
[0078] (3) Compare the given icon with the cut icon for similarity and select the result with the highest similarity;
[0079] (4) Based on the results, use selenium to simulate clicking and verify.
[0080] S8. Introduce a failed rerun mechanism with the help of unittestreport package to improve the pass rate;
[0081] S9, optimize image processing;
[0082] Further including:
[0083] (1) When processing images, use dynamic thresholding for binarization;
[0084] (2) When calculating the sliding distance, scale it according to the size of the verification code image to improve the sliding accuracy;
[0085] (3) For Chinese character selection, model training is used to improve accuracy.
[0086] Based on the above method, a device for processing multiple common verification codes in an automated test process in this embodiment includes: at least one memory and at least one processor;
[0087] The at least one memory is used to store a machine-readable program;
[0088] The at least one processor is used to call the machine-readable program to execute a method for processing multiple common verification codes in an automated testing process.
[0089] Embodiment 1:
[0090] When performing automated inspections on a system, because the login function uses a provincial unified identity authentication platform, a verification code needs to be entered when logging in. At this time, OCR technology is used to process the verification code. The specific implementation process is as follows:
[0091] S1. Automated inspection uses python+selenium+unittest framework;
[0092] S2. First, locate the verification code on the unified identity authentication login page and determine that it is a simple verification code, that is, only numbers and letters are required. Then download the verification code image and find that the image has a background grid. In order to ensure the accuracy of image recognition, the image is intercepted to obtain the verification code image without the background.
[0093] S3. Use OCR technology to process the image, perform grayscale processing, binarization processing and image enhancement, and perform text recognition on the processed image to obtain a character string;
[0094] S4. Finally, enter the obtained string into the verification code input box, click login, and complete the login operation;
[0095] S5. From the enhanced images, we can see that errors may occur sometimes, resulting in verification code recognition errors. Therefore, the unittestreport failure rerun mechanism is introduced to re-identify the code to improve the pass rate.
[0096] Embodiment 2:
[0097] When performing automated inspections on an Internet system, the login verification code is a slider verification code. In this case, OCR technology is used to process the verification code. The specific implementation process is as follows:
[0098] S1. Automated inspection uses python+selenium+unittest framework;
[0099] S2. First, locate the verification code on the login page and determine whether it is a slider verification code. Drag a small picture to fill the gap in the background image, then download the slider image and background. Note that the slider should not be obtained by screenshot, because there will be other images around the slider, which will interfere with the result.
[0100] S3. Use OCR technology to process the image, and then perform image matching after grayscale processing. Since the background image is obtained by screenshot, there are both sliders and gaps on the image. When matching the images, the best matching distance and the worst matching distance of the image will be obtained. The difference between the two distances is the moving distance of the slider.
[0101] S4. Finally, using selenium's automatic simulation operation, drag the slider a certain distance to fill the gap, log in successfully, and complete the login operation;
[0102] S5. During the implementation process, errors may sometimes occur in calculating the sliding distance, resulting in verification code recognition errors. Therefore, the failure re-run mechanism of unittestreport is introduced to re-identify again to improve the pass rate.
[0103] The above-mentioned specific implementations are only specific cases of the present invention. The patent protection scope of the present invention includes but is not limited to the above-mentioned specific implementations. Any technical solutions that comply with the claims of the present invention and any appropriate changes or substitutions made by ordinary technicians in the relevant technical field shall fall within the patent protection scope of the present invention.
[0104] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A method for processing multiple common verification codes in an automated testing process, characterized in that: The steps are as follows: S1. Use Python as the main language, Selenium automated testing framework and Unittest unit testing framework, and use dddddocr, Python-tesseract or PIL encapsulation class for image processing; S2. Use OCR technology to output image text through image noise reduction and image cutting; S3. Use selenium automated testing to complete the location of the verification code; S4. Process the verification code based on OCR technology; S5. For a simple verification code, you only need to enter a combination of English or numbers; S6. For the slider verification code, the sliding distance is calculated by comparing the sliding block with the missing image in the background image, and then the sliding operation is simulated with the help of selenium to complete the verification; S7, click the verification code, that is, click the icons in a given order to pass the verification, and the verification code includes a picture and a Chinese character; S8. Introduce a failure rerun mechanism with the help of unittestreport package; S9. Optimize image processing.
2. A method for processing multiple common verification codes in an automated test process according to claim 1, characterized in that: In step S2, the image denoising is to remove some unnecessary information in the image. If the verification code is a colored background, each pixel is placed in a five-dimensional space, namely X, Y, R, G, B; X and Y are the two-dimensional plane coordinates of the pixel, and R, G, B represent the color corresponding to the pixel.
3. A method for processing multiple common verification codes in an automated test process according to claim 2, characterized in that: In step S3, selenium automated testing is used to complete the positioning of the verification code, the output of the position coordinates, the capture of the background image of the verification code, and after the image processing is completed, the output string or operation is simulated input to complete the processing of the verification code.
4. A method for processing multiple common verification codes in an automated test process according to claim 3, characterized in that: In step S4, a failure rerun mechanism is introduced, and the login test is marked by using rerun in unittestreport in the unittest unit test framework. Specifically, two parameters count and interval are received, where count is the number of times the specified test case fails to be rerun, and interval is the time interval between each rerun; At the same time, if the login verification fails, the execution of subsequent automated tests will be stopped.
5. A method for processing multiple common verification codes in an automated test process according to claim 4, characterized in that: In step S5, further comprising: (1) Locate the verification code image and save it; (2) Convert the color image into a grayscale image, convert RGB into HSI color space, and use the L component; (3) Binarize the image, set the grayscale of pixels greater than a certain critical grayscale value to the maximum grayscale value, and set the grayscale of pixels less than this value to the minimum grayscale value. The value range is generally 0-1. Different binarization algorithms can be divided into fixed threshold and adaptive threshold. (4) Enhance the image by using enhanced display or converting the image to black and white; (5) Finally, perform text recognition on the processed image, obtain the output string, fill it into the input box and click login.
6. A method for processing multiple common verification codes in an automated test process according to claim 5, characterized in that: In step S6, further comprising: (1) Locate the verification code and save the image; (2) When saving an image, there may be the following situations: one is that the entire sliding verification code in the front end divides the image into several parts, and the order of the images is disrupted in the front end, and it needs to be restored according to the CSS offset; The other is that the background image is a complete image. In either case, you need to download the complete background image and slider. (3) Based on the complete background image and the incomplete image, use dddocr to process and obtain the gap distance; (4) When using Selenium to simulate sliding, please note that the sliding block distance does not necessarily start from 0 before processing. It needs to be positioned again and the sliding distance is obtained after the difference is made.
7. A method for processing multiple common verification codes in an automated test process according to claim 6, characterized in that: In step S7, further comprising: (1) Locate the verification code and save the pictures. First save the pictures that require you to click, then save the background picture. (2) Cut the background image, locate the icon in the background image, and then cut it. The positioning method is processed with reference to the simple verification code to obtain the grayscale + binarization + enhanced icon; (3) Compare the given icon with the cut icon for similarity and select the result with the highest similarity; (4) Based on the results, use selenium to simulate clicking and verify.
8. A method for processing multiple common verification codes in an automated test process according to claim 7, characterized in that: In step S9, further comprising: (1) When processing images, use dynamic thresholding for binarization; (2) When calculating the sliding distance, scale it according to the size of the verification code image; (3) For Chinese character selection, model training is used.
9. A device for processing multiple common verification codes in an automated test process, characterized in that: include: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is configured to call the machine-readable program to execute the method according to any one of claims 1 to 8.
