Web page testing method and system
By constructing a neural network element positioning prediction model and base class subclass structure, the Web page element positioning feature information is automatically extracted, which solves the problem of inefficient testing in the existing technology and realizes efficient and stable automated testing.
Patent Information
- Application Number
- CN202510235834.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-28
- Publication Date
- 2025-07-04
AI Technical Summary
Existing web page testing methods cannot automatically extract element positioning feature information in the target web page, resulting in inefficient testing, especially when the page changes frequently, it requires a large amount of manual positioning of elements.
A neural network-based element positioning prediction model is constructed. By analyzing the URL address of the target web page, extracting element positioning feature information, and using a random forest model for training, generating element positioning prediction model, combining base class and subclass structure, encapsulating the calling function method of browser and page elements, and constructing a PO model to achieve automated testing.
Improves testing efficiency, reduces manual positioning element work when page changes, improves maintenance efficiency and test coverage, and enhances the stability and accuracy of tests.
Smart Images

Figure CN120256289A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of page testing, and particularly to a Web page testing method and system. Background Art
[0002] With the increasing scale of many current application systems, large functional requirements, and frequent system development, there is a problem that a large amount of human, time and other resources need to be invested in the system testing process. During the actual testing work, some performances that cannot be completed by manual testing, large data volume input testing, etc., and the core basic functions need to be continuously iteratively tested throughout the test life cycle. Based on these problems, software test automation has increasingly become the test solution chosen by enterprises.
[0003] A Web page testing method, device, equipment and storage medium with the publication number of CN110908907A obtains an XML file corresponding to the Web page to be tested from a storage unit, and the XML file contains page elements and positioning methods in the Web page; determines whether the page elements in the Web page have changed according to the XML file; if there are page elements in the Web page that have changed, determines the changed page elements and outputs a prompt message to prompt the tester to modify the test script; obtains the modified test script, and runs the modified test script to perform regression testing on the Web page. By determining the page elements that have changed in the Web page before running the test script, then modifying the test script according to the changed page elements, and then running the modified test script.
[0004] In the above-mentioned existing Web page testing method, the element positioning feature information in the target web page cannot be automatically extracted, and currently it is usually manually marked. Moreover, when the page is modified, it is necessary to re-extract the modified page, which increases the workload and thus reduces the test efficiency. Summary of the Invention
[0005] In view of this, the present invention proposes a Web page testing method and system, which constructs an element positioning prediction model based on a neural network, can accurately and automatically extract the element positioning feature information in the target web page, reduce the work of manually positioning elements when the page changes frequently, and improve the test efficiency.
[0006] The technical solution of the present invention is implemented as follows: In the first aspect, the present invention provides a Web page testing method, including the following steps:
[0007] S1, request and parse the URL address of the target web page, and extract different types of element positioning feature information in the target web page;
[0008] S2. Perform data annotation on the extracted element positioning feature information to obtain a training dataset;
[0009] S3. Input the training dataset into a machine model for training to obtain an element positioning prediction model, which is used to predict the element positioning information in a web page;
[0010] S4. Construct a base class that encapsulates the call function methods of browsers and page elements. Create a separate subclass for each page. All subclasses inherit from the base class, and define the test methods and private attributes for the corresponding pages in the subclasses. The private attributes are used to store the positioning information of the predicted elements on the corresponding pages;
[0011] S5. Construct a PO model, determine the jump relationships between pages according to the business logic, and write test cases to test the page jumps and interactions. The test data in the test cases is passed to the corresponding page test methods, and a test report is generated after the test is executed.
[0012] Based on the above technical solutions, preferably, in step S1, the URL address of the target web page is requested and parsed to extract the positioning feature information of different types of elements in the target web page. Among them,
[0013] According to the URL address of the target web page, use the Requests library to request the URL address of the target web page, obtain the HTML source code information of the target web page, and convert the HTML source code information into HTML document information;
[0014] Use the BeautifulSoup library to parse the HTML document information, extract different types of elements in the target web page, and obtain the attribute values of element positioning and the hierarchical structure positions of elements in the DOM tree as the element positioning feature information.
[0015] Based on the above technical solutions, preferably, in step S2, the data annotation is performed on the extracted element positioning feature information to obtain a training dataset. Among them,
[0016] Construct a training dataset, annotate the positioning information, type, text, class, ID of each element, and the hierarchical structure position feature data of the element in the DOM tree, and add the annotated data to the training dataset to obtain the training dataset. The hierarchical structure position of the element in the DOM tree is the level calculated from the root node.
[0017] Based on the above technical solutions, preferably, in step S3, the training dataset is input into a machine model for training to obtain an element positioning prediction model, which is used to predict the element positioning information in future web pages. Among them, the following steps are included:
[0018] S31. Obtain a training data set, preprocess the data in the training data set, convert the element positioning feature information into numerical features, and divide the training data set into a training set and a test set;
[0019] S32. Build an element positioning prediction model based on a random forest model, initialize the parameters of the random forest model, and use the training set data to train the random forest model to obtain a trained element positioning prediction model;
[0020] S33. Use the test set data to evaluate the trained element positioning prediction model to obtain an evaluation result;
[0021] S34. Adjust the parameters of the random forest according to the evaluation result, and iterate and optimize until the element positioning prediction model converges to obtain a final element positioning prediction model.
[0022] Based on the above technical solutions, preferably, the obtaining a training data set, preprocessing the data in the training data set, and converting the element positioning feature information into numerical features in step S31 includes:
[0023] Obtain a training data set, and convert the positioning information, type, text, class, ID of each element in the training data set, and the hierarchical structure position feature data of the element in the DOM tree into feature vectors;
[0024] Among them, perform data cleaning on the text feature data of the element, and perform word segmentation processing to obtain a text word segmentation set;
[0025] Calculate the frequency of each word appearing in the corresponding text word segmentation subset. The expression is:
[0026] P i,j = w i,j ·l i,j ·j i / d i
[0027] In the formula, P i,j is the frequency of the j-th word appearing in the i-th text word segmentation subset, w i,j is the weight value corresponding to the position where the j-th word appears in the document in the i-th text word segmentation subset, j i is the j-th word in the i-th text word segmentation subset, d i is the total number of words in the document in the i-th text word segmentation subset;
[0028] Calculate the frequency of each word in the text word segmentation set. The expression is:
[0029]
[0030] In the formula, Qi,j is the frequency of the j-th word in the i-th text token subset in the text token set, t is the number of text token subsets, c i,j is the number of the j-th word in the i-th text token subset included in the text token subset, λ is a constant, and b is the capacity size of the text token set;
[0031] According to the frequency of each word in the corresponding text token subset and the frequency of each word in the text token set, calculate the word vector of each word. The expression is:
[0032] A i,j = P i,j ·Q i,j
[0033] In the formula, A i,j is the word vector of the j-th word in the i-th text token subset;
[0034] Construct a text word vector based on the word vector of each word, and use the constructed text word vector as a feature to input into the element positioning prediction model for training.
[0035] Based on the above technical solutions, preferably, in step S4, the constructed base class encapsulates the call function methods of the browser and page elements. Create a separate subclass for each page. All subclasses inherit the base class, and define the test methods and private attributes of the corresponding page in the subclass. The private attributes are used to store the positioning information of the predicted elements of the corresponding page. Among them, the following sub-steps are included:
[0036] Define a base class to encapsulate the browser initialization and the call function methods of page elements. Define the function methods of initializing the browser, closing the browser, and element searching and clicking in the base class. The defined browser initialization method is used to start the browser and open the URL address of the specified page. The defined browser closing method is used to close the browser. The defined element searching and clicking method is used to search for and click on page elements;
[0037] Create a subclass for each page. All subclasses inherit the base class. Call the function methods of the base class in the subclass and pass the URL address of the page. Define the test methods and private attributes corresponding to the page in the subclass. The private attributes are used to store the positioning information of the predicted elements of the corresponding page.
[0038] Based on the above technical solutions, preferably, in step S5, the constructed PO model determines the jump relationship between pages according to the business logic, and writes test cases to test the page jump and interaction. The test data in the test cases is passed to the corresponding page test method, and a test report is generated after the test. Among them, the following sub-steps are included:
[0039] Construct a PO model, identify and define all pages in the test platform, determine the jump relationships between pages according to the business logic, and define jump methods in the corresponding subclasses of the pages;
[0040] Create test instances for each page, use the location information obtained from the element location prediction model to locate page elements in the test cases, and associate them with the private attributes of the corresponding subclasses of the pages. Based on the base class, call the test methods of the corresponding page objects of the subclasses to execute the tests and generate test reports.
[0041] In a second aspect, the present invention also provides a Web page test system implemented by using the above Web page test method. The system includes:
[0042] A data collection module for requesting and parsing the URL address of the target web page and extracting different types of element location feature information in the target web page;
[0043] A data annotation module for annotating the extracted element location feature information to obtain a training data set;
[0044] A data training module for inputting the training data set into a machine model for training to obtain an element location prediction model, and the element location prediction model is used to predict the element location information in the web page;
[0045] An encapsulation module for constructing a base class, which encapsulates test methods for browsers and page elements, creates separate subclasses for each page, all subclasses inherit from the base class, and defines test methods for the corresponding pages and element location information predicted based on the element location prediction model in the subclasses;
[0046] A test module for constructing a PO model, determining the jump relationships between pages according to the business logic, and writing test cases to test the jumps and interactions of the pages. The test data in the test cases is passed to the corresponding page test methods to execute the tests and generate test reports.
[0047] In a third aspect, the present invention also provides an electronic device, including at least one processor, at least one memory, a communication interface, and a bus; wherein, the processor, the memory, and the communication interface complete mutual communication through the bus; the memory stores a Web page test method program executable by the processor, and the Web page test method program is configured to implement the above Web page test method.
[0048] In a fourth aspect, the present invention also provides a computer-readable storage medium, on which a Web page test method program is stored, and when the Web page test method program is executed, it implements the above Web page test method.
[0049] A Web page testing method and system of the present invention have the following beneficial effects compared with the prior art:
[0050] (1) By parsing the URL address of the target web page, extracting element positioning feature information, and constructing an element positioning prediction model based on the random forest model, it can accurately and automatically extract the element positioning feature information in the target web page, reduce the manual positioning element work caused by frequent page changes, and improve the testing efficiency;
[0051] (2) By constructing a base class and a subclass structure, the base class encapsulates common browser operations and page element calling methods, and the subclass defines the testing method according to the specific page. When the page elements change, only the element positioning information needs to be modified in the corresponding subclass, without modifying the test cases or test steps, which improves the maintenance efficiency. By constructing the PO model, all pages and their jump relationships in the testing platform are identified and defined, and the test data in the test cases is passed to the corresponding page testing method, realizing data-driven testing, facilitating the testing of various input situations, and improving the testing coverage;
[0052] (3) Handle abnormal situations, optimize through explicit waits, reduce the test interruption problems caused by loading delays, and improve the testing stability. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for the description of the embodiments or the prior art. Obviously, the drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.
[0054] Figure 1 is a flowchart of the Web page testing method of the present invention;
[0055] Figure 2 is a schematic structural diagram of the element positioning prediction model of the Web page testing method of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0056] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, rather than all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present invention.
[0057] Such as Figure 1 AndFigure 2 As shown in the figure, the present invention provides a Web page testing method, including the following steps:
[0058] S1. Request and parse the URL address of the target web page, and extract the element positioning feature information of different types in the target web page.
[0059] Among them, step S1 includes:
[0060] According to the URL address of the target web page, use the Requests library to request the URL address of the target web page, obtain the HTML source code information of the target web page, and convert the HTML source code information into HTML document information;
[0061] Use the BeautifulSoup library to parse the HTML document information, extract different types of elements in the target web page, and obtain the attribute values for element positioning and the hierarchical structure position of the elements in the DOM tree as the element positioning feature information.
[0062] It should be noted that the Requests library is a Python library for sending HTTP requests, supporting various HTTP methods. By sending a request to the URL address of the target web page, the HTML source code information of the web page can be obtained, and this source code contains the structure and content of the web page. The BeautifulSoup library is a Python library for parsing HTML and XML documents, converting the obtained HTML source code information into a BeautifulSoup object, that is, HTML document information; extract different types of elements in the web page through the find_all method encapsulated in the BeautifulSoup library, and obtain the locatable attribute values such as text, class, id of the elements and the hierarchical structure of the elements in the DOM tree.
[0063] In this embodiment, by using the Requests library and the BeautifulSoup library, the request and parsing of the URL address of the target web page are realized, and the element positioning feature information of different types in the target web page is extracted, providing strong support for subsequent automated testing.
[0064] S2. Perform data annotation on the extracted element positioning feature information to obtain a training data set.
[0065] Among them, step S2 includes:
[0066] Construct a training dataset, annotate the positioning information, type, text, class, ID, and the hierarchical structure position feature data of each element in the DOM tree, add the annotated data to the training dataset to obtain the training dataset, where the hierarchical structure position of the element in the DOM tree is the level calculated from the root node.
[0067] It should be noted that, first, create an empty dataset to store the annotated data, annotate the positioning information of each element, such as XPath or CSS selectors, etc. These positioning information will help the machine learning model accurately find the elements on the web page in subsequent prediction tasks. Annotate the type of the element, such as buttons, input boxes, and links, etc. Annotate the text content of the element, annotate the class and ID attributes of the element. Attributes are the key parts for uniquely identifying elements on the web page. Annotate the hierarchical structure position of the element in the DOM tree, calculated from the root node. Each element has a unique hierarchical path, which provides the position information of the element in the web page structure. Add the annotation information of each element, including positioning information, type, text, class, ID, and hierarchical structure position, as a record to the training dataset to obtain the training dataset.
[0068] In this embodiment, through data annotation and training dataset construction, it provides high-quality data support for the learning of the machine learning model, which helps to achieve more accurate and flexible web page element positioning.
[0069] S3. Input the training dataset into the machine model for training to obtain an element positioning prediction model, which is used to predict the element positioning information on the web page.
[0070] Among them, step S3 includes the following steps:
[0071] S31. Obtain the training dataset, preprocess the data in the training dataset, convert the element positioning feature information into numerical features, and divide the training dataset into a training set and a test set;
[0072] Among them, step S31 includes: obtaining the training dataset, and converting the positioning information, type, text, class, ID, and the hierarchical structure position feature data of each element in the training dataset into feature vectors;
[0073] Perform data cleaning on the text feature data of the element, and perform word segmentation processing to obtain a text word segmentation set; calculate the frequency of each word appearing in the corresponding text word segmentation subset. The expression is:
[0074] P i,j =w i,j ·l i,j ·ji / d i
[0075] Wherein, P i,j is the frequency of the j-th word in the i-th text segmentation subset, w i,j is the weight value corresponding to the position where the j-th word in the i-th text segmentation subset appears in the document, j i is the j-th word in the i-th text segmentation subset, d i is the total number of words in the document in the i-th text segmentation subset;
[0076] Calculate the frequency of each word in the text segmentation subset, and the expression is:
[0077]
[0078] Wherein, Q i,j is the frequency of the j-th word in the i-th text segmentation subset in the text segmentation subset, t is the number of text segmentation subsets, c i,j is the number of text segmentation subsets containing the j-th word in the i-th text segmentation subset, λ is a constant, and b is the capacity size of the text segmentation set;
[0079] According to the frequency of each word in the corresponding text segmentation subset and the frequency of each word in the text segmentation subset, calculate the word vector of each word, and the expression is:
[0080] A i,j = P i,j ·Q i,j
[0081] Wherein, A i,j is the word vector of the j-th word in the i-th text segmentation subset;
[0082] Construct a text word vector according to the word vector of each word, and use the constructed text word vector as a feature to input into the element positioning prediction model for training.
[0083] It should be noted that through word segmentation processing and word frequency calculation, the key information in the text is effectively extracted, the text features are converted into numerical features, enabling the machine learning model to process and understand text data. Moreover, according to the position of the word in the text, different weights are assigned to it. The words at the beginning and end of the text contain more key information or summary content, so higher weights can be given. According to the length of the word, its weight can be adjusted. Shorter words may be more likely to express specific concepts or entities, while longer words may contain more context information or modifiers. By considering the position of the word in the text, the length of the word, and the corresponding weight adjustment, more expressive word vectors can be generated, which can improve the prediction performance and accuracy of the model.
[0084] S32. Build an element location prediction model based on the random forest model, initialize the parameters of the random forest model, and use the training set data to train the random forest model to obtain the trained element location prediction model;
[0085] S33. Use the test set data to evaluate the trained element location prediction model and obtain the evaluation result;
[0086] S34. Adjust the parameters of the random forest according to the evaluation result, and iterate and optimize until the element location prediction model converges to obtain the final element location prediction model.
[0087] It should be noted that preprocess the data in the training dataset, extract the location information of DOM elements from the web page, such as XPath or CSS selectors, types, text content, class attributes, ID attributes, and hierarchical structures. Use the improved TF-IDF algorithm to convert the text content into a numerical vector, use one-hot encoding to convert the class attribute into a numerical vector, convert the ID attribute into a hash value, and directly use the hierarchical structure as a numerical feature. Normalize the numerical features to ensure that they are on the same scale, convert all features into numerical forms, and combine them into a feature matrix. Use random sampling to divide the dataset into a training set and a test set, usually in a ratio of 70% for the training set and 30% for the test set. The training set is used to train the model, and the test set is used to evaluate the performance of the model. Build a random forest model, use the training set data to train the random forest model, use the mean squared error formula to calculate the performance of the evaluation model, adjust the parameters of the random forest according to the evaluation result, repeat the training model and evaluation steps until the model performance converges to obtain the best element location prediction model, automatically obtain the location information of page elements, and when the page changes each time, the trained element location prediction model can be used to predict the location information of the elements and generate the corresponding location information.
[0088] Specifically, convert the data into features available for the model. First, use the pandas library to read the dataset; create a LabelEncoder object and use the fit_transform() method to encode the location information of the elements; use the get_dummies() method in the pandas library to convert the element location feature information into numerical values; use the train_test_split() method to split the dataset into a training set and a test set. Initialize a random forest model object using the RandomForestClassifier class, call the fit() method to train the model using the training set, and use the score() method to evaluate the model.
[0089] S4. Construct a base class which encapsulates the calling function methods of the browser and page elements. Create a separate subclass for each page. All subclasses inherit from the base class, and define the test methods and private attributes for the corresponding pages in the subclasses. The private attributes are used to store the positioning information of the predicted elements on the corresponding pages.
[0090] Among them, step S4 includes the following sub-steps:
[0091] Define a base class for encapsulating the browser initialization and the calling function methods of page elements. Define the function methods of initializing the browser, closing the browser, and finding and clicking elements in the base class. The defined browser initialization method is used to start the browser and open the URL address of the specified page. The defined browser closing method is used to close the browser. The defined element finding and clicking method is used to find and click page elements.
[0092] Create a subclass for each page. All subclasses inherit from the base class. Call the function methods of the base class in the subclasses and pass the URL address of the page. Define the test methods and private attributes corresponding to the page in the subclasses. The private attributes are used to store the positioning information of the predicted elements on the corresponding pages.
[0093] It should be noted that when constructing the base class, the base class is named BasePage, which serves as the basis for all page classes. The base class BasePage encapsulates the basic methods for browser operations and page element operations. Methods for browser operations: For example, the initialization method instantiates a browser object driver for operating the browser, such as maximizing the window, locating page elements, etc.; there are also methods for exiting the browser, etc. Methods for page element operations: Such as obtaining page elements, and re-encapsulating the method find_element() in the selenium framework for finding elements.
[0094] Among them, the re-encapsulation strategy is to define the element positioning method and the corresponding value of the positioning method as the formal parameters of the method. Just pass the specific value of the element each time the method for finding elements is called, which greatly improves the code reuse rate; there are also methods for inputting text into input box type elements, performing explicit waits for page elements, etc. The naming of the base class and the methods in the class uses the camel case naming method.
[0095] First, define the page as a class that inherits from the BasePage class. Define the page elements as private attributes of the class, which cannot be directly accessed externally. Write the operation methods corresponding to the specific test steps in the page class. The methods call the basic methods for operating on page elements encapsulated in the base class. When calling these operation methods, since the positioning parameters of the corresponding page elements are directly passed to the base class methods, when the page elements change, only the attribute value defining the element needs to be modified once in the corresponding page class. The page methods corresponding to the test steps and the chained calls of the page methods in the test cases are not affected by the change of page elements.
[0096] S5. Construct the PO model, determine the jump relationships between pages according to the business logic, and write test cases to test the page jumps and interactions. The test data in the test cases is passed to the corresponding page test methods, and the test report is generated by executing the tests.
[0097] Among them, step S5 includes the following sub-steps:
[0098] Construct the PO model, identify and define all pages in the test platform, determine the jump relationships between pages according to the business logic, and define the jump methods in the subclasses corresponding to the pages;
[0099] Create test instances for each page, use the positioning information obtained from the element positioning prediction model to locate page elements in the test cases, and associate them with the private attributes of the subclasses corresponding to the pages. Based on the base class, call the test methods of the corresponding page objects in the subclasses to execute the tests and generate the test report.
[0100] Specifically, in this embodiment, the first step is to analyze the business, sort out the pages involved in the business, construct the classes for each page, and the page classes inherit from the base class. An initialization method is defined in the base class, and an instance variable base_driver is defined. Generally, Web system testing starts from logging in. Therefore, in the class of the general login page, WebDriver is initialized, a browser object is instantiated, the test address is accessed, and the login operation is performed. Then, the next page class is imported, and the next page is jumped to through return. When the next page class is instantiated, the WebDriver instance that has been initialized and used is passed to the instance variable of the next page class in the form of a variable. Then, in the next page class, this WebDriver object is used to perform element operations, and the method calls of the page classes required for business testing are carried out sequentially. The pytest framework is used to write test cases. In this test framework, pre- and post-requisite preparations can be carried out at the method level, class level, or module level, so that a WebDriver instance only needs to be initialized once and can be reused in the subsequent tests of the entire test class or the entire module. After all the test cases are executed, it is destroyed once. By separating the page operations and test cases, when testing the business logic, the test cases only need to make a chained call to the page methods encapsulated in the page classes involved in the business logic, and obtain the actual test results for assertion.
[0101] First, construct a test class. The name of the test class starts with Test, and the pre- and post-requisite operations are sorted out. In the pre-requisite process, a WebDriver instance object is initialized, and this WebDriver object can be used throughout the test class. Write test methods. Note that the method names must start with test_. According to the business process analysis, determine which methods in the page classes are required for this test case. The page methods are called in a chain to obtain the test results, and the test can be completed by making assertions on the test results. Multiple test methods can be written for multiple test cases. The pytest framework defaults to executing all methods starting with test_ in sequence, or some decorator methods can be used to specify the execution order of the test methods. The final post-requisite action is to recycle WebDriver and close the browser.
[0102] In addition, manage the test data in a documented manner, separate the test data that needs to be passed in during the implementation process of the test steps, use a common method to read the test data in the corresponding document, and pass the read data to the specific page methods for invocation. Use the parameter automation method for data-driven testing to avoid using multiple highly repetitive scripts to jointly complete the testing of an input field. Only need to manage the test data in a document. After these test data are passed in and the script is run, multiple test cases can be generated.
[0103] Specifically, for example, when testing an input field, it is necessary to cover text tests that conform to different types of input rules, boundary value tests, and input tests that do not conform to the input rules. The test steps for these test cases are basically the same, and the only differences are the different input contents and inconsistent test results. In this scenario, the parameter automation function can be used to avoid multiple highly repetitive test scripts. The specific use of parameterization is to use the built-in decorator method pytest.mark.parametrize() of the pytest framework to decorate the test method, and pass multiple sets of test data to the test method in the form of a parameter list. When the test method is executed, multiple sets of input tests will be executed independently, generating separate test cases.
[0104] In addition, it also includes the handling of abnormal situations. In actual automated testing, the situation where page elements cannot be found often occurs. Many times, it is because the waiting time is shorter than the loading response time, or the program reports an error and stops running before the element is found. The program reporting an error may make people think that the test case has not passed and that there is a bug in the system. Therefore, it is necessary to capture and handle exceptions in some places where running errors are likely to occur to reduce these errors so as not to affect the accuracy of the test. For example, write a custom method to find elements, use the try...except... method, set multiple traversals to find elements. If the element is not found for the first time, perform a page sliding operation, and then execute the element search again. If the element cannot be successfully found within the defined number of searches, raise and capture the corresponding exception prompt such as NoSuchElementException, so that the program will not be affected by the abnormal error of the execution itself and other issues and affect the test results.
[0105] This embodiment uses explicit waits for optimization. The explicit wait WebDriverWait defines the wait condition and calls the method in ExpectedCondition once every 500 milliseconds by default. This embodiment is used in conjunction with the until() method. It waits until the method in until returns true, which means the element is found; otherwise, it continues to wait. An exception is thrown only when the maximum waiting time set is exceeded, reducing the impact on the accuracy of the test results.
[0106] The present invention also provides a Web page testing system implemented by using the above Web page testing method. The system includes:
[0107] A data collection module, configured to request and parse the URL address of the target web page, and extract different types of element positioning feature information in the target web page;
[0108] A data annotation module, configured to perform data annotation on the extracted element positioning feature information to obtain a training data set;
[0109] A data training module for inputting a training data set into a machine model for training to obtain an element location prediction model, which is used to predict the element location information in a web page;
[0110] An encapsulation module for constructing a base class, which encapsulates the test methods of browsers and page elements, creating a separate subclass for each page. All subclasses inherit the base class, and define the test methods for the corresponding pages and the element location information predicted based on the element location prediction model;
[0111] A test module for constructing a PO model, determining the jump relationships between pages according to the business logic, and writing test cases to test the page jumps and interactions. The test data in the test cases is passed to the corresponding page test methods, and a test report is generated after the tests are executed.
[0112] It should be noted that this system corresponds to the above-mentioned Web page test method. All implementation manners in the above method embodiments are applicable to the embodiments of this system and can achieve the same technical effects.
[0113] Those of ordinary skill in the art can realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraint conditions of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the present invention.
[0114] Those skilled in the art can clearly understand that for the convenience and conciseness of description, the specific working processes of the above-described system and modules can refer to the corresponding processes in the foregoing method embodiments and will not be elaborated herein.
[0115] In the embodiments provided by the present invention, it should be understood that the disclosed system and method can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the units is only a logical function division, and there can be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces. The indirect couplings or communication connections of the devices or units can be in electrical, mechanical or other forms.
[0116] The unit described as a separation component may or may not be physically separated. The component shown as a unit may or may not be a physical unit, that is, it may be located in one place or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0117] In addition, in each embodiment of the present invention, each functional unit may be integrated in a processing unit, or each unit may exist physically alone, or two or more units may be integrated in one unit.
[0118] If the described function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to enable a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, ROM, RAM, magnetic disks, or optical discs that can store program codes.
[0119] In addition, it should be noted that in the system and method of the present invention, obviously, each component or each step can be decomposed and / or recombined. These decompositions and / or recombinations should be regarded as equivalent solutions of the present invention. And, the steps of performing the above series of processes can naturally be executed in chronological order according to the described order, but it is not necessary to execute them in chronological order. Some steps can be executed in parallel or independently of each other. For those of ordinary skill in the art, it can be understood that all or any steps or components of the method and device of the present invention can be implemented in any computing device (including a processor, a storage medium, etc.) or a network of computing devices in the form of hardware, firmware, software, or a combination thereof, which can be achieved by those of ordinary skill in the art using their basic programming skills after reading the description of the present invention.
[0120] Therefore, the object of the present invention can also be achieved by running a program or a set of programs on any computing system. The computing system can be a well-known general-purpose system. Therefore, the object of the present invention can also be achieved only by providing a program product containing program code for implementing the method or apparatus. That is to say, such a program product also constitutes the present invention, and a storage medium storing such a program product also constitutes the present invention. Obviously, the storage medium can be any well-known storage medium or any storage medium developed in the future. It should also be noted that in the apparatus and method of the present invention, obviously, each component or each step can be decomposed and / or recombined. These decompositions and / or recombinations should be regarded as equivalent solutions of the present invention. And, the steps of performing the above series of processes can naturally be executed in chronological order according to the described order, but it is not necessary to be executed in chronological order. Some steps can be executed in parallel or independently of each other.
[0121] The above are only the preferred embodiments of the present invention, and are not intended to limit the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A Web page testing method, characterized in that, It includes the following steps: S1. Request and parse the URL address of the target web page, and extract the positioning feature information of different types of elements in the target web page; S2. Perform data annotation on the extracted element positioning feature information to obtain a training data set; S3. Input the training data set into a machine model for training to obtain an element positioning prediction model, which is used to predict the element positioning information in a web page; S4. Construct a base class, which encapsulates the call function methods of the browser and page elements. Create a separate subclass for each page. All subclasses inherit the base class, and define the test methods and private attributes for the corresponding pages in the subclasses. The private attributes are used to store the positioning information of the predicted elements on the corresponding pages; S5. Construct a PO model, determine the jump relationship between pages according to the business logic, and write test cases to test the page jumps and interactions. The test data in the test cases is passed to the corresponding page test methods, and a test report is generated after the execution of the tests.
2. The Web page testing method according to claim 1, characterized in that: In step S1, when requesting and parsing the URL address of the target web page and extracting the positioning feature information of different types of elements in the target web page, According to the URL address of the target web page, use the Requests library to request the URL address of the target web page, obtain the HTML source code information of the target web page, and convert the HTML source code information into HTML document information; Use the BeautifulSoup library to parse the HTML document information, extract different types of elements in the target web page, and obtain the attribute values for element positioning and the hierarchical structure positions of the elements in the DOM tree as the element positioning feature information.
3. The Web page testing method according to claim 2, characterized in that: In step S2, when performing data annotation on the extracted element positioning feature information to obtain a training data set, Construct a training data set, annotate the positioning information, type, text, class, ID of each element, and the feature data of the hierarchical structure position of the element in the DOM tree, and add the annotated data to the training data set to obtain the training data set. The hierarchical structure position of the element in the DOM tree is the level calculated from the root node.
4. The Web page testing method according to claim 3, wherein: In step S3, when inputting the training data set into a machine model for training to obtain an element positioning prediction model, which is used to predict the element positioning information in a future web page, it includes the following steps: S31. Obtain the training data set, preprocess the data in the training data set, convert the element positioning feature information into numerical features, and divide the training data set into a training set and a test set; S32. Construct an element positioning prediction model based on the random forest model, initialize the parameters of the random forest model, and use the training set data to train the random forest model to obtain the trained element positioning prediction model; S33. Use the test set data to evaluate the trained element positioning prediction model to obtain an evaluation result; S34. Adjust the parameters of the random forest according to the evaluation result, and iterate and optimize until the element positioning prediction model reaches convergence to obtain the final element positioning prediction model.
5. The Web page testing method according to claim 4, wherein: In step S31, obtain the training data set, preprocess the data in the training data set, and convert the element positioning feature information into numerical features, including: Obtain the training data set, and convert the positioning information, type, text, class, ID of each element in the training data set, and the hierarchical structure position feature data of the element in the DOM tree into feature vectors; Among them, perform data cleaning on the text feature data of the element, and perform word segmentation processing to obtain a text word segmentation set; Calculate the frequency of each word appearing in the corresponding text word segmentation subset, and the expression is: P i,j = w i,j · l i,j · j i / d i Wherein, P i,j is the frequency of occurrence of the j-th word in the i-th text segmentation subset, w i,j is the weight value corresponding to the position where the j-th word in the i-th text segmentation subset appears in the document, j i is the j-th word in the i-th text segmentation subset, d i is the total number of words in the document in the i-th text segmentation subset; Calculate the frequency of each word in the text word segmentation set, and the expression is: Where Q i,j is the frequency of the j-th word in the i-th text token subset in the text token set, t is the number of text token subsets, c i,j is the number of text token subsets containing the j-th word in the i-th text token subset, λ is a constant, and b is the capacity of the text token set; According to the frequency of each word appearing in the corresponding text word segmentation subset and the frequency of each word in the text word segmentation set, calculate the word vector of each word, and the expression is: A i,j = P i,j ·Q i,j where A i,j is the word vector of the j-th word in the i-th text token subset; Construct a text word vector according to the word vector of each word, and use the constructed text word vector as a feature to input into the element positioning prediction model for training.
6. The Web page testing method according to claim 5, characterized in that: In step S4, construct the base class. The base class encapsulates the call function methods of the browser and page elements. Create a separate subclass for each page. All subclasses inherit from the base class, and define the test methods and private attributes of the corresponding page in the subclass. The private attributes are used to store the positioning information of the predicted elements on the corresponding page. Among them, the following sub-steps are included: Define the base class, which is used to encapsulate the browser initialization and the call function methods of page elements. Define the function methods of initializing the browser, closing the browser, and element searching and clicking in the base class. The defined browser initialization method is used to start the browser and open the URL address of the specified page. The defined browser closing method is used to close the browser. The defined element searching and clicking method is used to search for and click on page elements; Create a subclass for each page. All subclasses inherit from the base class. Call the function methods of the base class in the subclass and pass the URL address of the page. Define the test methods and private attributes corresponding to the page in the subclass. The private attributes are used to store the positioning information of the predicted elements on the corresponding page.
7. The Web page testing method according to claim 1, characterized in that: In step S5, construct the PO model, determine the jump relationship between pages according to the business logic, and write test cases to test the page jump and interaction. The test data in the test cases is passed to the corresponding page test method, and a test report is generated by executing the test. Among them, the following sub-steps are included: Construct the PO model, identify and define all pages in the test platform, determine the jump relationship between pages according to the business logic, and define the jump method in the subclass corresponding to the page; Create a test instance for each page. Use the positioning information obtained from the element positioning prediction model to locate page elements in the test case and associate them with the private attributes of the subclass corresponding to the page. Based on the base class, call the test method of the subclass corresponding page object to execute the test and generate a test report.
8. A Web page testing system implemented by using the Web page testing method according to any one of claims 1-7, characterized in that: The system includes: A data collection module, which is used to request and parse the URL address of the target web page, and extract the element positioning feature information of different types in the target web page; A data annotation module, which is used to perform data annotation on the extracted element positioning feature information to obtain a training data set; A data training module, configured to input a training data set into a machine model for training to obtain an element positioning prediction model, where the element positioning prediction model is used to predict the element positioning information in a web page; An encapsulation module, configured to construct a base class, where the base class encapsulates test methods for browsers and page elements, create a separate subclass for each page, all subclasses inherit from the base class, and define test methods for the corresponding pages and element positioning information predicted based on the element positioning prediction model in the subclasses; A testing module, configured to construct a PO model, determine the jump relationships between pages according to business logic, and write test cases to test the jumps and interactions of pages. The test data in the test cases is passed to the corresponding page test methods, and a test report is generated after the tests are executed.
9. An electronic device, characterized in that, It includes at least one processor, at least one memory, a communication interface, and a bus; wherein, the processor, the memory, and the communication interface complete communication with each other through the bus; the memory stores a Web page testing method program executable by the processor, and the Web page testing method program is configured to implement a Web page testing method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, A Web page testing method program is stored on the storage medium, and when the Web page testing method program is executed, it implements a Web page testing method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Web page testing method, apparatus and device, and storage medium
CN110908907A
Cited By
Webpage testing method, electronic equipment and computer readable medium
CN121233480A
Real-time collaborative Web test capture method and system based on multi-feature scoring
CN121958133A
Real-time collaborative web test capturing method and system based on multi-feature scoring
CN121958133B