A web application-oriented test case intelligent positioning method and system
By analyzing the fuzziness and atomic properties of the ancestor nodes of target elements in web pages, a locator is constructed and matched layer by layer. This solves the problem of the difficulty of locating traditional locators on complex web pages, achieves accurate positioning of target elements, and improves the stability of automated testing.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- GUANGDONG POWER GRID CO LTD
- Filing Date
- 2022-11-29
- Publication Date
- 2026-06-12
AI Technical Summary
In existing web application test cases, traditional locators are difficult to reliably locate target elements in complex web page frames with variability, random arrangement, and many similar elements, causing test cases to fail during recording and replay.
By obtaining the absolute XPath of the target element of the web page, fuzzy calculations are performed on its ancestor nodes, and the atomic attributes of the fuzzy ancestor nodes and the target element are recorded. A locator is constructed, and fuzzy matching is performed layer by layer based on the multi-atomic attributes to find the child node with the highest similarity to determine the target element.
It achieves accurate and reliable automatic location of target elements in web pages, reduces test case failures during recording and replay, and improves the usability of automated testing tools.
Smart Images

Figure CN115712575B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of automated testing, and more specifically to a method and system for intelligent location of test cases for web applications. Background Technology
[0002] With the development of computer and network technologies, the scale and number of JavaScript-based web applications have grown rapidly, permeating various application areas of society and becoming the mainstream of current and future software system development. However, due to their highly dynamic and event-driven characteristics, various problems often arise in their applications. Recording / replay technology is now widely used for test case generation and execution in web applications. For example, Selenium IDE and UIRecorder inject listeners into web application pages when they are opened, capturing user test actions on the page during recording and creating locators for page operation objects. After recording is complete, test cases can be generated and executed by calling the test case execution engine.
[0003] From the perspective of event capture, current work can be divided into two categories: instrumentation at the application layer and instrumentation in native browsers. For example, Selenium IDE uses browser extensions to inject code into web applications during runtime, thereby monitoring web application events. However, due to the continuous upgrading of internet technology and the increasing volume of information services, various frameworks and complex, variable pages present a challenge for test cases in reliably locating target elements. Traditional locators often use CSS selectors and XPath to describe the location of target elements, but this approach is increasingly unable to meet the needs of many variable scenarios. For traditional CSS and XPath descriptions, even with weighted averaging of multiple locators, this scenario is generally unsolvable; if image locators are used, they rely heavily on the texture around the clicked element and are unstable in the aforementioned situations; manual locating generally requires writing multi-layered traversal and judgment logic, which is time-consuming and labor-intensive. Therefore, how to automatically and accurately locate target elements has become an urgent problem to be solved in automated testing. Summary of the Invention
[0004] To address the aforementioned technical problems, this invention provides a method and system for intelligently locating test cases for Web applications.
[0005] The technical solution of this invention is: a method for intelligent location of test cases for web applications, comprising:
[0006] Step S1: Obtain the absolute XPath corresponding to the target element on the web page; traverse each node on the absolute XPath to obtain all ancestor nodes of the target element, perform fuzzy calculation on the target element and the ancestor nodes, and determine whether the ancestor node has fuzziness; record the various atomic properties of the inner and outer elements of all ancestor nodes with fuzziness, and combine them with the various atomic properties of the target element to construct a locator;
[0007] Step S2: According to the locator, traverse the ancestor nodes; from the outer layer to the inner layer, perform fuzzy matching on the child nodes based on the multi-atomic attributes, find the child node with the highest similarity, and compare it with the target element of the locator. When the similarity is greater than the threshold, the child node is determined to be the target element.
[0008] Compared with the prior art, the present invention has the following advantages:
[0009] This invention discloses an intelligent test case location method for web applications. Addressing the characteristics of complex web page frames—variability, random arrangement, and numerous similar elements—this method analyzes key nodes with "fuzziness," proceeds layer by layer, and uses fuzzy matching to achieve accurate and reliable automatic location of target elements on web pages. This reduces the number of failed test cases during recording and replay, significantly increasing the usability of automated testing tools. Attached Figure Description
[0010] Figure 1 This is a flowchart of a test case intelligent location method for web applications according to an embodiment of the present invention;
[0011] Figure 2 This is a schematic diagram of target elements on a webpage in an embodiment of the present invention;
[0012] Figure 3 This is a schematic diagram of the process of recording the locator in an embodiment of the present invention;
[0013] Figure 4 This is a schematic diagram illustrating the change in the position of a target element on a webpage in an embodiment of the present invention;
[0014] Figure 5 This is a schematic diagram of the process of executing the locator according to an embodiment of the present invention;
[0015] Figure 6 This is a structural block diagram of a test case intelligent location system for web applications according to an embodiment of the present invention. Detailed Implementation
[0016] This invention provides an intelligent test case location method for web applications, which achieves accurate and reliable automatic location of target elements on web pages by analyzing key nodes with "fuzziness".
[0017] This invention is built upon the Playwright API. The Playwright API is a browser control tool library that provides a rich set of interface functions. It relies on the JS-handler interface provided by Playwright to inject scripts and extract and locate relevant element information.
[0018] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below through specific implementations and in conjunction with the accompanying drawings.
[0019] Example 1
[0020] like Figure 1 As shown in the figure, an embodiment of the present invention provides a method for intelligent location of test cases for web applications, comprising the following steps:
[0021] Step S1: Obtain the absolute XPath corresponding to the target element on the web page; traverse each node on the absolute XPath to obtain all ancestor nodes of the target element, perform fuzzy calculation on the target element and ancestor nodes, and determine whether the ancestor node has fuzziness; record the various atomic properties of the inner and outer elements of all ancestor nodes with fuzziness, and combine them with the various atomic properties of the target element to construct a locator.
[0022] Step S2: Based on the locator, traverse the ancestor nodes; from the outer layer to the inner layer, perform fuzzy matching on the child nodes based on the multi-atomic properties, find the child node with the highest similarity, and compare it with the target element of the locator. When the similarity is greater than the threshold, the child node is determined to be the target element.
[0023] In one embodiment, step S1 above involves: obtaining the absolute XPath corresponding to the target element on the webpage; traversing each node on the absolute XPath to obtain all ancestor nodes of the target element; performing fuzzy calculation on the target element and ancestor nodes to determine whether the ancestor node is fuzzy; recording the various atomic attributes of the inner and outer elements of all fuzzy ancestor nodes, and combining them with the various atomic attributes of the target element to construct a locator, specifically including:
[0024] Step S11: Obtain the absolute XPath corresponding to the target element on the webpage;
[0025] like Figure 2 As shown, users can like comments on a sample essay on the website www.example.com. Part of the website's source code is shown below:
[0026]
[0027] Among them, the target element to be located is the like button of person A, as shown in the circle, and its xpath " / html[1] / body[1] / div[6] / div[1] / div[3]" is obtained;
[0028] Step S12: Traverse each ancestor node in the absolute XPath; for each ancestor node, first ignore the position information of the ancestor node in the XPath, then calculate the weighted similarity of each atomic attribute of all elements located by this XPath and the target element. When the number of elements with a weighted similarity greater than the threshold γ1 is greater than 1, the ancestor node is ambiguous. Save the atomic attributes of the ancestor node and its parent node to inner and outer respectively, and add them to the ancestors list.
[0029] For example, for the 4th level node div[1] of the like button of person A, after ignoring the node position information of div[1] (i.e. the number 1 in []), the xpath is " / html[1] / body[1] / div[6] / div / div[3]". At this time, this will cause the like button in person A and the like button in "person B" to be easily confused. In order to avoid this ambiguity, the metadata of comment A and the atomic properties of its direct parent element are added to the ancestor part of the locator.
[0030] The ancestor node is determined to be ambiguous by calculating the weighted similarity between all elements corresponding to the xpath " / html[1] / body[1] / div[6] / div / div[3]" and the target element;
[0031] This invention employs a similarity calculation method based on atomic features. Specifically, it calculates the differences between various atomic features p (such as id, className, src, innerText, width and height coordinates, etc.) of two elements (e1, e2), and then calculates a weighted average of these results according to their respective weights to represent the similarity between the two elements. The weighted average value ranges from 0 to 1, with values closer to 1 indicating greater similarity. The calculation formula is shown below:
[0032]
[0033] In this context, PROPS is the set of atomic attributes selected from HTML elements, WEIGHT is the preset weight, and Distance represents the method for calculating the difference between two atomic attributes. This invention employs, but is not limited to, the following three calculation methods:
[0034] "Edit distance" refers to calculating the Levenshtein distance (0 to 1) of a string;
[0035] "Strict equality" means that only a complete match returns 1 (1 or 0);
[0036] "Calculation percentage" indicates the calculation ratio (0 to 1).
[0037] The atomic properties of the HTML elements used in this embodiment of the invention are shown in Table 1:
[0038] Table 1. Atomic properties of HTML elements and their Distance calculation methods
[0039]
[0040]
[0041] Calculate the weighted similarity between all elements corresponding to xpath " / html[1] / body[1] / div[6] / div / div[3]" and the target element. When the number of elements with a weighted similarity greater than the threshold γ1 is greater than 1, it indicates that there are elements that are easy to confuse. That is, the node / html[1] / body[1] / div[6] / div[1] is ambiguous. At this time, save the atomic properties (including xpath, className, innerText, id, src, width and height coordinates, etc.) of this node (the element corresponding to / html[1] / body[1] / div[6] / div[1]) and its parent node (the element corresponding to / html[1] / body[1] / div[6]) to inner and outer respectively.
[0042] Step S13: Save the atomic properties of the target element as self, and form the locator of the target element by {ancestors, self};
[0043] After the traversal is complete, the atomic properties of the target element itself are recorded as self. For example, for the like button of person A, its xpath " / html[1] / body[1] / div[6] / div[1] / div[3]" and other atomic properties are saved to self. Finally, the locator of the target element is composed of {ancestors, self}.
[0044] like Figure 3 The diagram shown is a flowchart of the recording process for the locator.
[0045] After a period of time, person C adds new comments, such as Figure 4As shown, the position of the like button on the target element person A has changed. Therefore, the target element can be located by executing the locator through the following steps.
[0046] In one embodiment, step S2 above involves: traversing the ancestor nodes according to the locator; performing fuzzy matching on child nodes from the outer layer to the inner layer based on multi-atomic properties to find the child node with the highest similarity, and comparing it with the target element of the locator. If the similarity is greater than a threshold, the child node is determined to be the target element. Specifically, this includes:
[0047] Step S21: Obtain the locators {ancestors, self} of the target element, and iterate through each pair of outer and inner in ancestors:
[0048] Step S211: Using the XPath of outer as the candidate range, find the element with the largest weighted similarity to each atomic attribute of outer. If the weighted similarity is less than the threshold γ2, the localization fails; otherwise, proceed to step S212.
[0049] At this point, the current element is equivalent to the element described by outer, but when the page structure changes, the current element will differ from the element described by inner;
[0050] Step S212: Using the child nodes of the current node as the candidate range, find the element with the largest weighted similarity value of each atomic attribute of inner; if the weighted similarity is less than the threshold γ2, the localization fails; otherwise, proceed to step S213.
[0051] At this point, the current element is equivalent to the element described by inner. However, when the page structure changes or there is a list with a change in the order of arrangement, the current element will differ from the element described by inner.
[0052] Step S213: Update the xpath attributes of subsequent nodes in the descriptor using the atomic attributes of the current node;
[0053] The XPath and other properties of subsequent nodes in the locator are updated and corrected using the current element. This corrects discrepancies between the current element and the inner description element that may arise when the page structure changes or when the list has a changed order. This ensures the stability of subsequent positioning XPaths.
[0054] Step S22: Within the current node, using the xpath of self as the candidate range, find the element with the largest weighted similarity to each atomic attribute of self. If the weighted similarity is less than the threshold γ2, the localization fails; otherwise, the current node is the target element.
[0055] If the location is successful, the script-recorded action (such as clicking or typing) can be released on the obtained element. Then, the next line of code can be executed. If the location fails, it means that the corresponding element does not exist on the page, and the process will either enter exception handling or terminate execution.
[0056] In the example above, when executing the locator, first find the outer node of the ancestor according to the xpath " / html[1] / body[1] / div[6]", and ensure that its similarity is greater than γ2; then, find the node with the highest similarity to the inner among the child nodes of the current node, and ensure that its similarity is greater than γ2. Here, the "comment A" that is currently the third comment in the comment section will be found; then, according to the xpath " / html[1] / body[1] / div[6] / div[3]" of the "comment A" found at this time, correct the xpath information of the node that has not yet been used (currently only self is left); finally, use the xpath of the self node (corrected to " / html[1] / body[1] / div[6] / div[3] / div[3]") to find the target element and ensure that its similarity is greater than γ2.
[0057] like Figure 5 The diagram shown is a flowchart illustrating the execution of the locator.
[0058] This invention discloses an intelligent test case location method for web applications. Addressing the characteristics of complex web page frames—variability, random arrangement, and numerous similar elements—this method analyzes key nodes with "fuzziness," proceeds layer by layer, and uses fuzzy matching to achieve accurate and reliable automatic location of target elements on web pages. This reduces the number of failed test cases during recording and replay, significantly increasing the usability of automated testing tools.
[0059] Example 2
[0060] like Figure 6 As shown, this embodiment of the invention provides an intelligent test case location system for web applications, comprising the following modules:
[0061] The locator recording module 31 is used to obtain the absolute XPath corresponding to the target element on the Web page; traverse each node on the absolute XPath to obtain all ancestor nodes of the target element; perform fuzzy calculation on the target element and ancestor nodes to determine whether the ancestor node has fuzziness; record the various atomic properties of the inner and outer elements of all fuzzy ancestor nodes, and combine them with the various atomic properties of the target element to construct the locator.
[0062] The locator execution module 32 is used to traverse the ancestor nodes according to the locator; from the outer layer to the inner layer, fuzzy matching is performed on the child nodes based on the multi-atomic properties to find the child node with the highest similarity and compare it with the target element of the locator. When the similarity is greater than the threshold, the child node is determined to be the target element.
[0063] The above embodiments are provided merely for the purpose of describing the present invention and are not intended to limit the scope of the invention. The scope of the invention is defined by the appended claims. Various equivalent substitutions and modifications made without departing from the spirit and principles of the invention should be covered within the scope of the invention.
Claims
1. A method for intelligently locating test cases for Web applications, characterized in that, include: Step S1: Obtain the absolute XPath corresponding to the target element on the web page; Traverse each node on the absolute XPath to obtain all ancestor nodes of the target element, perform fuzzy calculation on the target element and the ancestor nodes, and then determine whether the ancestor node has fuzziness. Record the various atomic properties of all inner and outer elements of ancestor nodes with fuzziness, and combine them with the various atomic properties of the target element to construct a locator, specifically including: Step S11: Obtain the absolute XPath corresponding to the target element on the webpage; Step S12: Traverse each ancestor node on the absolute xpath; for each ancestor node, first ignore the position information of the ancestor node in the xpath, then calculate the weighted similarity of each atomic attribute of all elements located by this xpath and the target element. When the number of elements with a weighted similarity greater than the threshold γ1 is greater than 1, the ancestor node is ambiguous. Save the atomic attributes of the ancestor node and its parent node to inner and outer respectively, and add them to the ancestors list. Step S13: Save the atomic properties of the target element as self, and form the locator of the target element by {ancestors, self}; Step S2: Based on the locator, traverse the ancestor nodes; from the outer layer to the inner layer, perform fuzzy matching on the child nodes based on multi-atomic attributes to find the child node with the highest similarity, and compare it with the target element of the locator. When the similarity is greater than a threshold, determine that the child node is the target element. Specifically, this includes: Step S21: Obtain the locator {ancestors, self} of the target element, and iterate through each pair of outer and inner in ancestors: Step S211: Using the XPath of outer as the candidate range, find the element with the largest weighted similarity to each atomic attribute of outer. If the weighted similarity is less than the threshold γ2, the localization fails; otherwise, proceed to step S212. Step S212: Using the child nodes of the current node as the candidate range, find the element with the largest weighted similarity value to each atomic attribute of inner; if the weighted similarity is less than the threshold γ2, the localization fails; otherwise, proceed to step S213. Step S213: Update the xpath attributes of subsequent nodes in the locator using the atomic attributes of the current node; Step S22: Within the current node, using the xpath of self as the candidate range, find the element with the largest weighted similarity to each atomic attribute of self. If the weighted similarity is less than the threshold γ2, the localization fails; otherwise, the current node is the target element.
2. A test case intelligent location system for web applications, characterized in that... The method for intelligently locating test cases for web applications as described in claim 1 includes the following modules: The locator recording module is used to obtain the absolute xpath corresponding to the target element on the web page; traverse each node on the absolute xpath to obtain all ancestor nodes of the target element; perform fuzzy calculation on the target element and the ancestor nodes; and determine whether the ancestor node has fuzziness. Record the various atomic properties of all inner and outer elements of ancestor nodes with fuzziness, and combine them with the various atomic properties of the target element to construct a locator; The locator execution module is used to traverse the ancestor nodes according to the locator; from the outer layer to the inner layer, perform fuzzy matching on the child nodes based on the multi-atomic attributes, find the child node with the highest similarity, and compare it with the target element of the locator. When the similarity is greater than the threshold, the child node is determined to be the target element.