Automatic testing method based on image comparison
Through an automated test method based on image comparison, the problem that existing tools are difficult to compare complex image elements is solved, and the automatic comparison of the differences in front-end display of new and old pages is realized, detailed reports are generated, and the test accuracy and efficiency are improved. Reduced error rate.
Patent Information
- Application Number
- CN202411081798.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-08
- Publication Date
- 2024-12-06
- Estimated Expiration
- 2044-08-08
AI Technical Summary
Existing automated testing tools are difficult to effectively identify and compare complex image elements, resulting in limited test coverage and accuracy, which cannot meet the needs of diverse test scenarios.
Design an automated test method based on image comparison, access the target website through deep traversal, obtain and compare web page screenshots, calculate structural similarity index, identify differences and generate detailed reports, reduce manual operations, and improve test accuracy and efficiency.
It realizes automated comparison of the differences in front-end display of new and old pages, significantly reduces the error rate, saves manpower and material resources, improves test accuracy and efficiency, and provides more accurate and reliable test results.
Smart Images

Figure CN119088679A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the field of automated testing, and in particular to an automated testing method based on image comparison. Background Art
[0002] In today's rapidly developing testing field, automated testing has become a key means to improve testing efficiency and accuracy. However, existing automated testing solutions are mainly focused on interface testing, that is, verifying whether the interaction between systems meets expectations through defined interface specifications. In contrast, there are relatively few automated solutions for image comparison testing, and the development of this field is obviously lagging behind.
[0003] Existing automated testing tools often have single functions and cannot meet the diverse testing scenarios faced by testers. These tools often cannot effectively identify and compare complex image elements, resulting in limited test coverage and accuracy. With the continuous enrichment of software applications and the improvement of user experience requirements, testing teams urgently need more intelligent, flexible and comprehensive automated testing tools to meet the growing testing needs. These tools should be able to handle various complex image comparison tasks, reduce manual intervention, and provide more accurate and reliable test results, thereby improving software quality and shortening time to market. Summary of the invention
[0004] The purpose of the present invention is to overcome the shortcomings of the prior art and provide an automated testing method based on image comparison, which can realize the comparison of the differences between the front-end display of new and old pages through an automated testing method, automatically generate detailed reports, reduce manual operations, significantly reduce the error rate, and save manpower and material resources. Users can conveniently analyze the comparison results, optimize the test process, and improve accuracy and efficiency.
[0005] In order to achieve the above purpose, an automated testing method based on image comparison is designed. The steps of the method are as follows: S1 generates a test case: accesses the target website through deep traversal, and obtains the target website through the front end. Use the tag method or regular matching method to extract the sub-links and external links contained in the website, select the depth to be traversed according to business needs, and repeat the above operations to obtain the deeper sub-links of the web page; S2 matching comparison parameters: organize the parameter data required for image comparison, and maintain them in two Excel files of the specified template in a one-to-one correspondence order; S3 collect comparison data: access the web page links maintained in the table, adjust the browser width and height to the maximum web page screenshot Figure 1 to obtain two sets of web page screenshot images, and save the images in the specified path; S4 processing comparison data: according to the specified path in step S3, two pictures to be compared are obtained and the pictures are converted into grayscale images, the structural similarity index between the two grayscale images is calculated, the contours of the different points between the two grayscale images are identified and marked with rectangles, and the images marked with differences are saved in HTML files; S5 report generation: the relevant details of the image comparison are written into an Excel file, and compressed together with the difference file finally generated in step S4 to generate an image comparison report file; S6 report sending: configure the recipient's mailbox, and send the image comparison report file of step S5 to relevant business personnel.
[0006] Preferably, the automated testing method based on image comparison provided by the present invention has other technical features, wherein the step S1 specifically includes: S1.1 accessing the target website through selenium, taking out the complete page source code of the website, and obtaining the source code through regular matching or obtaining All sub-links and external links contained in the website are obtained by tagging; S1.2 removes files in pdf, ppt, rar, zip, doc and xls formats from the sub-links and external links of the website, and performs deduplication. After deduplication, the sub-links and external links of the website are written into a list; S1.3 selects the depth to be traversed according to business needs, loops through the sub-links obtained in step S1.2, visits these sub-links in turn, obtains the complete page source code of the sub-links, and obtains the sub-links through regular matching or acquisition The tag placement method is used to obtain all sub-links and external links contained in the website; S1.4 removes files in pdf, ppt, rar, zip, doc and xls formats from the sub-links and external links of the website, and puts all the obtained sub-links and external links in the same list to remove duplication; S1.5 writes the sub-links and external links in the list into an Excel file.
[0007] Preferably, the automated testing method based on image comparison provided by the present invention also has other technical features, wherein the step S2 specifically includes: S2.1 filling in a set of data task number ID, task name and corresponding website column URL corresponding to a test case in the first Excel file of the specified template; when network fluctuations cause the final generated image to be incompletely displayed, fill in the waiting time in the table, the time is in seconds, and modify the comparison path in the configuration file.
[0008] Preferably, the automated testing method based on image comparison provided by the present invention also has other technical features, wherein the step S3 specifically includes: S3.1. Open the Excel file of the second specified template, and fill in the relevant data of another set of test cases required to be compared with the Excel file of the first specified template in the same way as step S2.1, and when maintaining the two sets of data, ensure that the website column URL serial numbers required to be compared are applied one by one to reduce errors occurring during the comparison; S3.2. Loop through the corresponding website column URLs in the Excel file of the specified template, access them one by one with Google Chrome through the Selenium framework, and at the same time calculate the maximum web page screenshot of Google Chrome, set the width and height of the browser to the width and height of the maximum web page screenshot, and save the image after taking the screenshot.
[0009] Preferably, the automated testing method based on image comparison provided by the present invention also has other technical features, wherein the step S4 specifically includes: S4.1 reading two images to be compared, the read image format is BGR, and converting the two BGR format images into grayscale images; S4.2 calculating the structural similarity index between the two grayscale images, and dividing the comparison results according to the structural similarity and the size, and the division results include: consistent comparison, inconsistency, comparison failure-size difference and comparison failure-unknown abnormality; S4.3 If the comparison result is: inconsistency, then identify the contours of the different points between the two grayscale images and mark them with rectangles, and store the inconsistent comparison results, the identified images and other comparison results in the report together; S4.4 save the comparison images marked with differences into an HTML file.
[0010] Preferably, the automated testing method based on image comparison provided by the present invention also has other technical features, wherein the step S5 specifically includes: S5.1 saving all comparison files in a specified path, creating a new Excel table in the path to write the comparison name and comparison result of each use case after comparison for review by business personnel, and setting a jump link for each use case. After clicking the link, it jumps to the HTML page that marks the image difference corresponding to the use case.
[0011] Preferably, the automated testing method based on image comparison provided by the present invention has other technical features, wherein the step S6 specifically includes: S6.1 configuring the recipient mailbox, compressing the above-mentioned comparison report file and sending it to relevant business personnel.
[0012] Compared with the prior art, the present invention has the following advantages: A. Compared with the existing manual comparison, it eliminates the complicated process, saves time and manpower, and is easy to manage and maintain.
[0013] B. The present invention uses the automated testing tool Selenium to write scripts to traverse and generate sub-links, then write the sub-links into a use case table, capture a webpage screenshot of each sub-link, compare and analyze the two sets of images, generate a comparison report, and send it to relevant business personnel. On the one hand, traversing website links through scripts greatly reduces the cost of manual use case maintenance.
[0014] C. Accurate image comparison ensures the reliability of the use case execution results and solves the problem that defects cannot be exposed due to single interface comparison. Clear comparison reports also make it easier for business personnel to understand and analyze. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 , is the flow chart of the present invention. DETAILED DESCRIPTION
[0016] In order to make the purpose, principle and structure of the present invention more clear, it is further described below in conjunction with the drawings and specific embodiments.
[0017] In response to the increasingly diverse testing needs, the present invention discloses a set of automated testing methods based on image comparison. Specifically, the following steps are included: after deeply traversing the sub-links of the target website, the web page links required for comparison are maintained in an Excel file of a specified template; two sets of images are obtained according to the links maintained in the table; the two sets of images are compared one by one; the parts of the two sets of images with differences are marked; a comparison report is generated and the report is sent via email. The present application can complete image comparison more quickly, and the automatic and accurate difference comparison and report generation saves manpower and material resources, greatly reduces the occurrence rate of errors, and can more conveniently analyze the comparison results.
[0018] See also Figure 1 In order to achieve the above purpose, an automated testing method based on image comparison is designed, and the method is specifically as follows: A. Visit the target website through the front-end Use tags or regular matching to extract the sub-links and external links contained in the website, select the depth to be traversed according to business needs, and repeat the above operations to obtain deeper sub-links of the web page; B. Arrange the web page links, task names, waiting times and other data to be compared, and maintain them in two Excel files of the specified template in a one-to-one correspondence order; C. Access the web page link maintained in the table and set the browser width and height to match the maximum web page screenshot. Figure 1 Thus, two sets of webpage screenshot images are obtained and the two sets of images are placed in the specified path; D. Get the two images to be compared according to the specified path in step 3, convert them to grayscale, calculate the structural similarity index between the two grayscale images, find the contours of the different points so that we can place rectangles around the areas marked as "different", and finally save the images with difference marks into an HTML file; E. Write the relevant details of the comparison into an Excel file, and compress it together with the difference file finally generated in step 4 to generate a comparison report file; F. Configure the recipient's mailbox and send the comparison report file in step 5 to relevant business personnel.
[0019] <Example 1>: According to this embodiment, an embodiment of an automated testing method based on image comparison is provided. This method is used to compare the data and style differences actually displayed on the front end of the new and old pages, and generate a comparison report for testers to analyze after the comparison is completed.
[0020] The method includes: S1: Generate test cases; Configure the website to be tested in the deep traversal script, set the traversal depth as required, and run the deep traversal script. The script will generate the sub-links and external links obtained after the traversal into a table.
[0021] The deep traversal script will use the Selenium automated testing tool to access the target website, obtain the page source code of the website, and then use regular matching or acquisition Get all the links contained in the current page in a tag-based way, delete all file links in pdf, ppt, rar, zip, doc and xls formats, and deduplicate the remaining links, and finally store them in a table according to the sub-links and external links. If you need to traverse multiple layers, repeat the above operation to generate sub-links based on the existing sub-links.
[0022] When maintaining the use case table, write the sub-links and external links obtained in the previous step into the template file of the comparison use case as needed, and write the task name, waiting time and other data of each use case. Make a copy and replace its domain name with the domain name of the web page to be compared with it, modify the file name of the comparison use case file, and place the two files in the specified location of the script.
[0023] S2: Image comparison; Modify the required parameters of the image comparison script and run the shell file of the image comparison script.
[0024] The internal logic of the image comparison script is: loop through the corresponding website column URLs in the Excel file, open the Google Chrome through the Selenium automation testing tool and access them one by one, and at the same time calculate the maximum web page screenshot of the Google Chrome, set the width and height of the browser to the width and height of the maximum web page screenshot, and save the image after taking the screenshot.
[0025] Use cv2.imread() interface to read two images to be compared. The image format read by this method is BGR. Use cv2.cvtColor() interface to convert the two BGR format images into grayscale images. Use compare_ssim interface to calculate the structural similarity index between the two grayscale images. According to the structural similarity and size, the comparison results are divided into four types: "Comparison is consistent", "Inconsistent", "Comparison failed (size difference)" and "Comparison failed (unknown exception)". If the comparison result is "Inconsistent", find the outline of the different points so that we can place a rectangle around the area marked as "different". Other situations will also be presented in the final report. Finally, save the comparison image marked with differences to an HTML file.
[0026] S3: Generate and send report; Save all comparison files in the specified path, create a new Excel table in this path to write the comparison name and comparison results of each use case after comparison for business personnel to review, set a jump link for each use case, click the link to jump to the HTML page marked with the image difference corresponding to the use case; configure the recipient's mailbox, compress the above comparison report file and send it to the relevant business personnel.
[0027] The above description is only a specific implementation mode of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can make equivalent substitutions or changes according to the technical solutions and novel concepts of the present invention within the technical scope disclosed by the present invention, which should be covered by the protection scope of the present invention.
Claims
1. An automated testing method based on image comparison, characterized in that: The steps of the method are as follows S1 generates test cases: access the target website through deep traversal, and obtain the target website through the front end Use the tag method or regular matching method to extract the sub-links and external links contained in the website, select the depth to be traversed according to business needs, and repeat the above operation to obtain the deeper sub-links of the web page; S2 matching comparison parameters: organize the parameter data required for image comparison and maintain them in two Excel files of the specified template in a one-to-one correspondence order; S3 collects and compares data: accesses the web page link maintained in the table, adjusts the browser width and height to the maximum web page screenshot to obtain two sets of web page screenshot images, and saves the images in the specified path; S4 processes the comparison data: according to the specified path in step S3, two pictures to be compared are obtained and the pictures are converted into grayscale images, the structural similarity index between the two grayscale images is calculated, the contours of different points between the two grayscale images are identified and marked with rectangles, and the images marked with differences are saved in an HTML file; S5 Report generation: Write the relevant details of the image comparison into an Excel file, and compress it together with the difference file finally generated in step S4 to generate an image comparison report file; S6 Report sending: Configure the recipient's mailbox and send the image comparison report file in step S5 to relevant business personnel.
2. The automated testing method based on image comparison according to claim 1, characterized in that: The step S1 specifically includes: S1.1 Access the target website through selenium, retrieve the complete page source code of the website, and obtain it through regular matching or Use tags to get all sub-links and external links contained in the website; S1.2 Remove files in pdf, ppt, rar, zip, doc and xls formats from the website's sub-links and external links, and perform deduplication. After deduplication is completed, write the website's sub-links and external links into a list; S1.3 Select the depth to be traversed according to business needs, loop through the sub-links obtained in step S1.2, visit these sub-links in turn, take out the complete page source code of the sub-links, and obtain the sub-links through regular matching or The tag placement method obtains all sub-links and external links contained in the website; S1.4 Remove files in pdf, ppt, rar, zip, doc and xls formats from the website's sub-links and external links, and put all the obtained sub-links and external links in the same list to remove duplication; S1.5 Write the sub-links and external links in the list into an Excel file.
3. The automated testing method based on image comparison according to claim 1, characterized in that: The step S2 specifically includes: S2.1 Fill in a set of data task number ID, task name and corresponding website column URL corresponding to a set of test cases in the first Excel file of the specified template. When network fluctuations cause the final generated image to be incomplete, fill in the waiting time in seconds in the table and modify the comparison path in the configuration file.
4. The automated testing method based on image comparison according to claim 1, characterized in that: The step S3 specifically includes: S3.1 Open the second Excel file of the specified template, and fill in the relevant data of another set of test cases to be compared with the Excel file of the first specified template in the same way as step S2.
1. When maintaining the two sets of data, ensure that the URL serial numbers of the website columns to be compared are applied one by one to reduce errors in the comparison; S3.
2. Loop through the corresponding website column URLs in the Excel file of the specified template, access them one by one using Google Chrome through the Selenium framework, calculate the maximum web page screenshot of Google Chrome, set the width and height of the browser to the width and height of the maximum web page screenshot, and save the image after taking the screenshot.
5. The automated testing method based on image comparison according to claim 1, characterized in that: The step S4 specifically includes: S4.1 reads two images to be compared, the image format is BGR, and converts the two BGR format images into grayscale images; S4.2 calculates the structural similarity index between the two grayscale images, and divides the comparison results according to the structural similarity and the size, and the division results include: comparison consistency, inconsistency, comparison failure-size difference and comparison failure-unknown anomaly; S4.3 If the comparison result is: there is an inconsistency, identify the contours of the different points between the two grayscale images and mark them with rectangles, and store the inconsistency comparison result, the identified image and other comparison results together in the report; S4.4 saves the comparison images with marked differences into an HTML file.
6. The automated testing method based on image comparison according to claim 1, characterized in that: The step S5 specifically includes: S5.1 Save all comparison files in the specified path, create a new Excel table in this path to write the comparison name and comparison results of each use case after comparison for review by business personnel, set a jump link for each use case, click the link to jump to the HTML page marked with the image difference corresponding to the use case.
7. The automated testing method based on image comparison according to claim 1, characterized in that: The step S6 specifically includes: S6.1 Configure the recipient's mailbox, compress the above comparison report file and send it to relevant business personnel.
Citation Information
Patent Citations
Automatic difference detection method and device based on crawler
CN103365967A
Website vulnerability test method, server and system
CN108667770A
Method, device and equipment for detecting abnormal hyperlink of portal website and medium
CN114861101A
Test result visualizing file generation device, test result visualizing file generation method, and test result visualizing file generation program
JP2024000386A
Apparatus and method for automatically extractinghyperlink information and automatically testing websites using the automatically extracted hyperlinkinformation
KR1020020072028A
Cited By
Screenshot method and device and electronic equipment
CN121542521A
