A method and device for generating an automated test script, and a storage medium
By generating a DOM tree and a matrix model of web page elements to optimize XPath paths and automatically adjusting test scripts, the problem of poor robustness of test scripts in existing technologies is solved, and testing efficiency and accuracy are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHINA MOBILE GRP GUANGDONG CO LTD
- Filing Date
- 2024-09-20
- Publication Date
- 2026-05-01
AI Technical Summary
Existing automated test scripts have poor robustness in regression testing due to changes in web page structure or differences in mobile devices, causing test scripts to fail. Manual adjustments are time-consuming and prone to omissions, reducing testing efficiency and accuracy.
By obtaining the initial automated test script, parsing the initial XPath path, generating the DOM tree, establishing the web page element matrix model, optimizing the initial XPath path based on the test script data table and the web page element matrix model, generating the target XPath path, and automatically adjusting the test script.
This improved the efficiency and accuracy of test script generation, which in turn improved the efficiency and accuracy of UI testing, reducing the need for manual adjustments.
Smart Images

Figure CN119377078B_ABST
Abstract
Description
A method, apparatus, and storage medium for generating automated test scripts. Technical Field
[0001] This disclosure relates to the fields of infrastructure and IT support technology, and in particular to a method, apparatus and storage medium for generating automated test scripts. Background Technology
[0002] Web UI automated test scripts have improved the efficiency of web testing and are widely used. These automated test scripts typically use XPath paths to locate page elements and record the test process.
[0003] In existing technologies, the absolute path of XPath can be obtained from the original parent element to the element to be located; or, the path expression for positioning relative to the current element node can be used, with the current element as the reference point, and the target element can be located through certain attributes, tag names, text content and other features to obtain the relative path of XPath.
[0004] However, in regression testing, the aforementioned existing technologies are susceptible to failure due to subtle changes in the web structure or attribute values of web page elements. These changes can cause web page elements to malfunction, leading to the failure of automated test scripts. In such cases, the automated test scripts need to be repaired or regenerated. Furthermore, mobile UI automated test scripts suffer from poor robustness on mobile devices. The same app (device terminal) may behave differently on different types of phones or even on different operating systems of the same type of phone. This results in the XPath path having poor robustness on mobile devices, potentially leading to missing elements and script execution failure. Therefore, to address the aforementioned issue of poor robustness in automated test scripts, manually adjusting the XPath path for automated testing based on different devices or web page versions is time-consuming and prone to omissions, reducing testing efficiency and accuracy. Summary of the Invention
[0005] To overcome the problems existing in related technologies, this disclosure provides a method, apparatus and storage medium for generating automated test scripts.
[0006] According to a first aspect of the present disclosure, a method for generating automated test scripts is provided, the method comprising:
[0007] Obtain the initial automated test script and parse the initial automated test script to extract the initial XPath path;
[0008] Obtain the content of the webpage to be tested, parse the webpage content to generate the DOM tree of the webpage to be tested;
[0009] Based on the initial XPath path, determine the corresponding test script data table;
[0010] Based on the DOM tree, establish a webpage element matrix model;
[0011] The initial XPath path is optimized based on the test script data table and the web page element matrix model to obtain the target XPath path, and the target UI automated test script is generated based on the target XPath path.
[0012] Optionally, in this embodiment of the disclosure, the step of parsing the initial automated test script to extract the initial XPath path includes:
[0013] Based on the language and format of the initial automated test script, the initial automated test script is parsed by the corresponding parser or library to generate an abstract syntax tree;
[0014] Based on the abstract syntax tree, the code in the initial automated test script is traversed to extract the initial XPath path.
[0015] Optionally, in this embodiment of the disclosure, the step of parsing the webpage content to generate the DOM tree of the webpage to be tested includes:
[0016] Determine the webpage type corresponding to the webpage content;
[0017] If the webpage type is the first type, then the webpage content is parsed to generate the DOM tree of the webpage to be tested;
[0018] If the webpage type is the second type, the controller program simulates the user clicking on the UI interface controls in the current state to generate the DOM tree of the webpage to be tested.
[0019] Optionally, in this embodiment of the disclosure, determining the corresponding test script data table based on the initial XPath path includes:
[0020] Determine the code location information of the initial XPath path in the initial automated test script;
[0021] The initial XPath path is preprocessed to obtain the node information of the initial XPath path;
[0022] Based on the node information of the initial XPath path, a corresponding XPath path array is generated;
[0023] Based on the code location information and the XPath path array, the corresponding test script data table is determined.
[0024] Optionally, in this embodiment of the disclosure, establishing a webpage element matrix model based on the DOM tree includes:
[0025] Obtain the position and attribute information of each node in the DOM tree, and generate a node data storage table based on the position and attribute information of each node;
[0026] Traverse each node in the node data storage table to generate a node queue;
[0027] A webpage element matrix model is established based on the node depth of each node object in the node queue.
[0028] Optionally, in this embodiment of the disclosure, the test script data table includes code location information and an XPath path array; the step of optimizing the initial XPath path based on the test script data table and the webpage element matrix model to obtain the target XPath path, and generating a target UI automated test script based on the target XPath path, includes:
[0029] Based on the test script data table and the web page element matrix model, the nodes corresponding to the elements in the XPath path array are obtained;
[0030] Based on the XPath path array and the corresponding nodes, the initial XPath path is optimized to obtain the optimized XPath path array;
[0031] The optimized XPath path array is converted into valid path nodes according to XPath syntax, and the target XPath path is obtained based on each path node.
[0032] The target XPath is used to generate a target UI automated test script based on the code location information.
[0033] Optionally, in this embodiment of the disclosure, optimizing the initial XPath based on the XPath path array and the corresponding node objects to obtain an optimized XPath path array includes:
[0034] If the first element at the end of the XPath path array matches a unique node, then delete the remaining elements in the XPath path array to obtain an optimized XPath path array; if the first element at the end of the XPath path array does not match a unique node, then obtain the second element from the XPath path array in reverse order.
[0035] If the node matched by the second element is unique, then the first and second elements are retained, and the remaining elements in the XPath path array are deleted to obtain the optimized XPath path array; if the node matched by the second element is not unique, then the above steps are repeated until there is an element that matches a unique node to obtain the optimized XPath path array.
[0036] If the starting element in the XPath path array does not match a unique node or matches the root node, then the traversal and matching are performed again based on the node information of the first element at the end of the XPath path array.
[0037] If the node information of the first element matches multiple duplicate nodes and attribute information, then the traversal and matching are performed again based on the node information of the second element until an element matches a node and attribute information that does not contain duplicates. The matched element is retained, and the remaining elements of the XPath path array are deleted to obtain the optimized XPath path array. Otherwise, the first element and its corresponding node information are retained, and the remaining elements of the XPath path array are deleted to obtain the optimized XPath path array.
[0038] According to a second aspect of the present disclosure, an apparatus for generating automated test scripts is provided, the apparatus comprising:
[0039] The first acquisition module is used to acquire the initial automated test script and parse the initial automated test script to extract the initial XPath path;
[0040] The second acquisition module is used to acquire the content of the webpage to be tested, and parse the webpage content to generate the DOM tree of the webpage to be tested;
[0041] The determination module is used to determine the corresponding test script data table based on the initial XPath path;
[0042] A module is established to create a matrix model of web page elements based on the DOM tree;
[0043] The optimization module is used to optimize the initial XPath path based on the test script data table and the web page element matrix model to obtain the target XPath path, and generate the target UI automated test script based on the target XPath path.
[0044] According to a third aspect of the present disclosure, an electronic device is provided, comprising:
[0045] processor;
[0046] Memory used to store processor-executable instructions;
[0047] The processor is configured to implement the method described in the first aspect above.
[0048] According to a fourth aspect of the present disclosure, a non-transitory computer-readable storage medium is provided, the computer storage medium storing computer-executable instructions; the computer-executable instructions, when executed by a processor, are capable of implementing the method described in the first aspect above.
[0049] The technical solutions provided by the embodiments of this disclosure may include the following beneficial effects:
[0050] This disclosure presents a method, apparatus, and storage medium for generating automated test scripts. The method includes obtaining an initial automated test script and parsing it to extract an initial XPath path; obtaining the content of a webpage to be tested and parsing it to generate a DOM tree for the webpage; determining a corresponding test script data table based on the initial XPath path; establishing a webpage element matrix model based on the DOM tree; optimizing the initial XPath path based on the test script data table and the webpage element matrix model to obtain a target XPath path; and generating a target UI automated test script based on the target XPath path. Therefore, this disclosure can automatically adjust the initial XPath path using the test script data table and the webpage element matrix model to obtain the target XPath path, eliminating the need for manual adjustments and improving the efficiency and accuracy of test script generation, thereby improving the efficiency and accuracy of UI testing.
[0051] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description
[0052] The accompanying drawings, which are incorporated in and form a part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure.
[0053] Figure 1 is a flowchart illustrating an automated test script generation method according to some embodiments of the present disclosure;
[0054] Figure 2 is a schematic diagram illustrating a dynamic webpage triggering event according to some embodiments of the present disclosure;
[0055] Figure 3 is a schematic diagram of a node tree according to some embodiments of the present disclosure;
[0056] Figure 4 is a schematic diagram of a node tree according to some embodiments of the present disclosure;
[0057] Figure 5 is a schematic diagram of a node tree according to some embodiments of the present disclosure;
[0058] Figure 6 is a schematic diagram of the structure of an automated test script generation apparatus according to some embodiments of the present disclosure;
[0059] Figure 7 is a block diagram of an electronic device suitable for generating automated test scripts according to some embodiments of the present disclosure. Detailed Implementation
[0060] Some embodiments of this disclosure will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description refers to the drawings, unless otherwise indicated, the same numerals in different drawings denote the same or similar elements. Various modifications, variations, and equivalents of the methods, apparatus, and / or devices described herein will become apparent upon understanding this disclosure. For example, the order of operations described herein is merely illustrative and is not limited to those orders set forth herein, but can be changed as will become apparent upon understanding this disclosure, except for operations that must be performed in a particular order. Furthermore, for clarity and brevity, descriptions of features known in the art may be omitted.
[0061] The embodiments described in the following examples of this disclosure are not representative of all embodiments consistent with this disclosure. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this disclosure as detailed in the appended claims.
[0062] Figure 1 is a flowchart illustrating an automated test script generation method according to some embodiments of the present disclosure. As shown in the figure, the method may include the following steps:
[0063] Step 101: Obtain the initial automated test script and parse the initial automated test script to extract the initial XPath path.
[0064] It should be noted that the execution entity of the above-mentioned automated test script generation method is an automated test script generation device, which can be implemented by software and / or hardware. In this embodiment, the automated test script generation device can be configured in an electronic device.
[0065] In this example embodiment, the electronic device may include devices such as terminal devices and servers, and this embodiment does not limit the electronic device.
[0066] In this embodiment of the disclosure, the initial automated test script can be any language, such as Python or Java. Based on this, after obtaining the initial automated test script, the initial XPath path can be extracted by parsing the initial automated test script using the corresponding parser or library, according to the language and format of the initial automated test script.
[0067] Specifically, in this embodiment of the disclosure, the method for extracting the initial XPath path from the initial automated test script may include the following steps:
[0068] Step 1011: Based on the language and format of the initial automated test script, parse the initial automated test script using the corresponding parser or library to generate an abstract syntax tree;
[0069] In this embodiment of the disclosure, if the initial automated test script is a Python script, it can be parsed by the corresponding Python parser or library to generate an abstract syntax tree; if the initial automated test script is a Java script, it can be parsed by the corresponding Java parser or library to generate an abstract syntax tree.
[0070] Step 1012: Based on the abstract syntax tree, traverse the code in the initial automated test script and extract the initial XPath path.
[0071] In this embodiment of the disclosure, after obtaining the abstract syntax tree through the above steps, the code in the initial automated test script can be traversed based on the abstract syntax tree. By finding the element location statements used in the initial automated test script, the element attributes used in the element location statements and the parent-child relationships between elements can be analyzed to extract the initial XPath path.
[0072] Step 102: Obtain the content of the webpage to be tested, parse the webpage content to generate the DOM tree of the webpage to be tested.
[0073] In this embodiment of the disclosure, the program module that obtains the webpage to be tested can access and obtain the corresponding content of the webpage to be tested by inputting the URL of the webpage. Furthermore, in this embodiment of the disclosure, after obtaining the content of the webpage to be tested through the above steps, the webpage content can be parsed to generate the DOM tree of the webpage to be tested.
[0074] Specifically, in this embodiment of the disclosure, the method for parsing webpage content to generate the DOM tree of the webpage to be tested may include the following steps:
[0075] Step 1021: Determine the webpage type corresponding to the webpage content;
[0076] Step 1022: If the webpage type is the first type, then parse the webpage content and generate the DOM tree of the webpage to be tested;
[0077] Step 1023: If the webpage type is the second type, then the controller program simulates the user clicking on the UI interface controls in the current state to generate the DOM tree of the webpage to be tested.
[0078] In this embodiment of the disclosure, the webpage type corresponding to the webpage content can be determined by judging whether the webpage content to be tested includes a triggering event. If the webpage content to be tested includes a triggering event, the webpage type corresponding to the webpage content is determined to be the second type; if the webpage content to be tested does not include a triggering event, the webpage type corresponding to the webpage content is determined to be the first type. Furthermore, in this embodiment of the disclosure, the first type can be a static page, and the second type can be a dynamic page.
[0079] Furthermore, in this embodiment of the disclosure, the aforementioned static pages can be switched using different URLs, and the webpage states (the webpage state being the DOM state after the webpage is parsed by the browser) corresponding to different URLs are also different. Based on this, if the webpage type is the first type, the DOM tree of the webpage to be tested can be generated after the webpage content is parsed in the browser backend. Specifically, in this embodiment of the disclosure, the DOM tree can be obtained through the JavaScript DOM API, using its built-in `document` object to access the DOM tree of the HTML document, and the `traverseDOM` method can be used to traverse the entire DOM tree, thereby generating the DOM tree of the webpage to be tested.
[0080] Furthermore, in this embodiment, the dynamic page can use JavaScript or Ajax to implement partial page refresh. When a user triggers a certain event, the JavaScript code bound to that event is invoked, and the DOM content and structure parsed by the browser are dynamically modified. Also, in this embodiment, when multiple webpage states belong to the same URL, a dynamic page can simultaneously have multiple different webpage states, and the content and structure of the DOM tree corresponding to different webpage states are different. Based on this, in regression testing, it may cause dynamic page elements to malfunction. Therefore, if the webpage type is the second type, the controller can simulate the user clicking on the UI control in the current state. If an event is triggered and the DOM tree is updated, the controller can update the state recorded in the state machine and record the event.
[0081] Specifically, Figure 2 is a schematic diagram of a dynamic webpage triggering event proposed in an embodiment of this disclosure. As shown in Figure 2, the current state is first recorded, and the user triggers events through a program to generate a list of triggerable events for the current state. If the current list of triggerable events is empty, it is determined whether the termination condition has been reached (no triggerable events in any state). If the termination condition is met, the process ends; otherwise, the state is reverted, and the original page is reloaded. If the current list of triggerable events is not empty, an event is randomly triggered. If it causes changes to the webpage elements and their XPath paths within the test area, the new state is set; otherwise, another event is triggered. The above steps are repeated until all events in the above list of triggerable events have been triggered, generating the DOM tree of the webpage to be tested.
[0082] Step 103: Based on the initial XPath path, determine the corresponding test script data table.
[0083] In this embodiment of the disclosure, after obtaining the initial XPath path through the above steps, the corresponding test script data table can be determined based on the initial XPath path.
[0084] Specifically, in this embodiment of the disclosure, the method for determining the corresponding test script data table based on the initial XPath path may include the following steps:
[0085] Step 1031: Determine the code location information of the initial XPath path in the initial automated test script;
[0086] Step 1032: Preprocess the initial XPath path to obtain the node information of the initial XPath path;
[0087] Step 1033: Generate the corresponding XPath path array based on the node information of the initial XPath path;
[0088] Step 1034: Based on the code location information and the XPath path array, determine the corresponding test script data table.
[0089] In this embodiment of the disclosure, the code file for the initial automated test script can be a Python file, a Java file, or a JavaScript file. The location of the initial XPath path within the initial automated test script can be determined by searching for code segments related to locating page elements on the webpage.
[0090] For example, in the Selenium automated testing framework, in the element location code found via find_element_by_xpath, the initial XPath path can be a string in find_element_by_xpath(' / div / xpath / input'). Furthermore, in this embodiment of the disclosure, if the initial XPath path is dynamically generated through variables or expressions, the target XPath path can be determined by tracking the values of the variables or the evaluation results of the expressions.
[0091] Furthermore, in this embodiment of the disclosure, the initial XPath path is preprocessed to obtain node information of the initial XPath path. This node information may include element nodes, element positions, and element attributes. Specifically, element nodes can be elements in an HTML or XML document, such as... , , The position of an element can be its depth, for example, the position of the book element in / bookstore / book[2] / title[2]; the attributes of an element can be the attributes of the element node, for example The "href" in the text.
[0092] Furthermore, in this embodiment of the disclosure, after obtaining the node information of the initial XPath path through the above steps, the node information can be sequentially filled into the XPath path array according to the position order of the path, thereby generating the corresponding XPath path array.
[0093] Furthermore, in this embodiment of the disclosure, after obtaining the code location information and XPath path array through the above steps, the code location information and XPath path array of the initial XPath path in the initial automated test script can be stored in the fields [code location id] and [XPath path array] of the test script data table, wherein [code location id] and [XPath path array] are in one-to-one correspondence.
[0094] Step 104: Based on the DOM tree, establish a matrix model of web page elements.
[0095] In this embodiment of the disclosure, after generating the DOM tree of the webpage to be tested through the above steps, a webpage element matrix model can be established based on the DOM tree.
[0096] Specifically, in this embodiment of the disclosure, the method for establishing a webpage element matrix model based on a DOM tree may include the following steps:
[0097] Step 1041: Obtain the position and attribute information of each node in the DOM tree, and generate a node data storage table based on the position and attribute information of each node;
[0098] Step 1042: Traverse each node in the node data storage table and generate a node queue;
[0099] Step 1043: Based on the node depth of each node object in the node queue, establish a web page element matrix model.
[0100] In this embodiment of the disclosure, the position information of the nodes in the DOM tree can be obtained through getBoundingClientRect in JavaScript, and the attribute information of the nodes can be obtained through attributes. The attribute information of the nodes may include the element node, the position of the element, and the attributes of the element.
[0101] Furthermore, in this embodiment of the disclosure, a node ID can be generated using a hash algorithm based on the location information of each node. The node ID, element node, element position, element attributes, and the ID of the node's parent node are then stored in a node data storage table. If a node has no parent node, a null value is stored.
[0102] Furthermore, in this embodiment of the present disclosure, each node in the node data storage table is traversed, and each node with a parent relationship is traversed one by one according to the parent-child relationship in each node until the next node has no child nodes, thereby generating a corresponding node tree, and a corresponding node queue is generated by storing the node tree in a queue. The node ID of the root node in each node tree is used as an object attribute to facilitate accurate traversal in the future.
[0103] Further, in this embodiment of the disclosure, after obtaining the node queue through the above steps, the method for establishing a webpage element matrix model based on the node depth of each node object in the node queue includes: obtaining the node depth of each node object in the node queue, and establishing a webpage element matrix model based on the maximum node depth among all node objects; and storing each node object sequentially into the webpage element matrix model based on the node depth of each node object. In this embodiment of the disclosure, the method of storing each node object sequentially into the webpage element matrix model based on the node depth of each node object may include: storing each node object sequentially into the webpage element matrix model in ascending order based on the node depth of each node object.
[0104] Step 105: Optimize the initial XPath path based on the test script data table and the web page element matrix model to obtain the target XPath path, and generate the target UI automated test script based on the target XPath path.
[0105] In this embodiment of the disclosure, after obtaining the test script data table and the web page element matrix model through the above steps, the initial XPath path can be optimized based on the test script data table and the web page element matrix model to obtain the target XPath path, and the target UI automated test script can be generated based on the target XPath path.
[0106] Specifically, in this embodiment of the disclosure, the method for optimizing the initial XPath path based on the test script data table and the web page element matrix model to obtain the target XPath path, and generating the target UI automated test script based on the target XPath path, may include the following steps:
[0107] Step 1051: Based on the test script data table and the web page element matrix model, obtain the nodes corresponding to the elements in the XPath path array;
[0108] Step 1052: Based on the XPath path array and the corresponding nodes, optimize the initial XPath path to obtain the optimized XPath path array;
[0109] Step 1053: Convert the optimized XPath path array into valid path nodes according to XPath syntax, and obtain the target XPath path based on each path node;
[0110] Step 1054: Generate the target UI automated test script based on the target XPath path and code location information.
[0111] In this embodiment of the disclosure, the XPath path array in the first data of the test script data table is obtained, and the elements of the XPath path array are matched with the web page element matrix model to obtain the nodes corresponding to the elements in the XPath path array.
[0112] Specifically, in this embodiment of the disclosure, the method of matching the elements of the XPath path array with the web page element matrix model to obtain the node corresponding to the element in the XPath path array may include: finding the node depth in the web page element matrix model that matches the length of the XPath path array based on the length of the XPath path array; and obtaining the node corresponding to the element in the XPath path array based on the element node in the XPath path array and the depth of the matched node.
[0113] In this embodiment of the disclosure, the first element n at the end of the XPath path array is matched in reverse order. The tree nodes of the corresponding node tree in the node queue of step 1042 are traversed according to the depth of the matched nodes. The tree nodes whose attribute information is the same as that of the element node are determined as the nodes that match element n. The matched nodes and their parent and child nodes are stored in a temporary queue C. The second element of the XPath path array is obtained in reverse order. The second element is traversed in the temporary queue C. All nodes that match the second element are stored in a temporary queue B. The third element of the XPath path array is obtained in reverse order. The third element is traversed in the node tree until all elements of the XPath path array are matched with the corresponding nodes.
[0114] Furthermore, in this embodiment of the disclosure, the XPath path array in the test script data table is optimized based on the requirements of simplicity (shortest path) and uniqueness (no duplicate paths).
[0115] Specifically, in this embodiment, if the node matched by the first element at the end of the XPath path array is unique, then the remaining elements in the XPath path array are deleted to obtain an optimized XPath path array; if the node matched by the first element at the end of the XPath path array is not unique, then the second element is obtained from the XPath path array in reverse order; if the node matched by the second element is unique, then the first and second elements are retained, and the remaining elements in the XPath path array are deleted to obtain an optimized XPath path array; if the node matched by the second element is not unique, then the above steps are repeated until there is an element that matches a unique node to obtain an optimized XPath path. The XPath path array is used as an array. If the starting element in the XPath path array matches a non-unique node or matches the root node, the traversal and matching are performed again based on the node information of the first element at the end of the XPath path array. If the node information of the first element matches multiple duplicate nodes and attribute information, the traversal and matching are performed again based on the node information of the second element, until an element matches a node and attribute information that does not contain duplicates. The matched element is retained, and the remaining elements of the XPath path array are deleted to obtain the optimized XPath path array. Otherwise, the first element and its corresponding node information are retained, and the remaining elements of the XPath path array are deleted to obtain the optimized XPath path array.
[0116] For example, in this embodiment of the disclosure, assuming that the end element n (the last element) of the XPath path array is traversed among the nodes, if element n matches a node to which node object C belongs (as shown in Figure 3), and node object C is unique, that is, there are no other nodes, then element n has uniqueness. According to the requirements of uniqueness and simplicity of XPath path, only element n is needed in the corresponding optimized XPath path. Therefore, other elements in the XPath path array are unnecessary. Based on this, other elements are deleted, and element n is retained to obtain the optimized XPath path array.
[0117] Furthermore, in this embodiment, if element n matches multiple nodes belonging to node object D (as shown in Figure 4), that is, if node object D has other identical nodes, then element n has repetition. In this case, the second element m is obtained from the XPath path array in reverse order. According to the XPath path syntax, element m is the parent element of element n. Based on this, according to the parent-child relationship, element m is traversed and matched among the parent nodes belonging to multiple node objects D. If element m matches only one node, it means that element m and element n can form a unique XPath path. In this case, other unnecessary elements in array X are deleted, and element n and element m are retained to obtain the optimized XPath path array. If element m matches multiple nodes, the next element of the XPath path array is obtained in reverse order, and the traversal and matching continue until a unique element is matched. The elements before this element in reverse order are deleted to obtain the optimized XPath path array.
[0118] Furthermore, in this embodiment of the disclosure, if the starting element (i.e. the first element) in the XPath path array matches multiple nodes or matches the root node of the node tree, it indicates that the XPath path constructed by the elements of the XPath path array has multiple duplicate paths in the DOM tree. In this case, the traversal and matching are performed again based on the node information (the position of the element or the attribute of the element) of the first element n in the XPath path array.
[0119] In this embodiment, if element n matches a node object D and the unique node corresponding to the attribute id=1 of its element (as shown in Figure 5), and there are no multiple duplicate nodes, it indicates that the node information of element n is unique. In this case, other elements of the XPath path array are deleted, and element n and its node information are retained to obtain the optimized XPath path array. If element n matches multiple duplicate nodes and their node information, the next element of the XPath path array is obtained in reverse order and the matching continues until there are no duplicate nodes and their attribute information. Then, other elements of the XPath path array are deleted, and the currently matched element and its node information are retained to obtain the optimized XPath path array.
[0120] Furthermore, in this embodiment of the disclosure, the optimized XPath path array obtained through the above steps can be updated to the test script data table. If a valid optimized XPath path array cannot be obtained by following the above steps, an exception flag is added to the XPath path array and updated to the test script data table.
[0121] In this embodiment of the disclosure, after obtaining the optimized XPath path array through the above steps, the optimized XPath path array can be converted into valid path nodes according to XPath syntax. A " / " is added between each path node, and the nodes are concatenated in reverse order to form the target XPath path. The target UI automated test script is then regenerated based on the code location information corresponding to the target XPath path. If an XPath path array with an exception flag exists, it is synchronously output to the target UI automated test script to indicate the invalidity of the XPath path.
[0122] This disclosure proposes a method for generating automated test scripts. The method includes: obtaining an initial automated test script and parsing it to extract an initial XPath path; obtaining the content of the webpage to be tested and parsing it to generate a DOM tree for the webpage; determining the corresponding test script data table based on the initial XPath path; establishing a webpage element matrix model based on the DOM tree; optimizing the initial XPath path based on the test script data table and the webpage element matrix model to obtain a target XPath path; and generating a target UI automated test script based on the target XPath path. Therefore, this disclosure can automatically adjust the initial XPath path using the test script data table and the webpage element matrix model to obtain the target XPath path, eliminating the need for manual adjustments and improving the efficiency and accuracy of test script generation, thereby improving the efficiency and accuracy of UI testing.
[0123] Figure 6 illustrates an automated test script generation apparatus according to some embodiments of the present disclosure, the apparatus including:
[0124] The first acquisition module 601 is used to acquire the initial automated test script and parse the initial automated test script to extract the initial XPath path;
[0125] The second acquisition module 602 is used to acquire the content of the webpage to be tested, parse the webpage content, and generate the DOM tree of the webpage to be tested.
[0126] Module 603 is used to determine the corresponding test script data table based on the initial XPath path;
[0127] Module 604 is created to build a matrix model of web page elements based on the DOM tree.
[0128] The optimization module 605 is used to optimize the initial XPath path based on the test script data table and the web page element matrix model to obtain the target XPath path, and generate the target UI automated test script based on the target XPath path.
[0129] In this embodiment of the disclosure, the first acquisition module 601 is specifically used for:
[0130] Based on the language and format of the initial automated test script, the initial automated test script is parsed by the corresponding parser or library to obtain the parsing result;
[0131] Based on the parsing results, an abstract syntax tree is generated;
[0132] Based on the abstract syntax tree, the code in the initial automated test script is traversed to extract the initial XPath path.
[0133] Optionally, in this embodiment of the disclosure, the second acquisition module 602 is specifically used for:
[0134] Determine the webpage type corresponding to the webpage content;
[0135] If the webpage type is the first type, then the webpage content is parsed to generate the DOM tree of the webpage to be tested;
[0136] If the webpage type is type 2, the controller program simulates the user clicking on the UI controls in the current state to generate the DOM tree of the webpage to be tested.
[0137] Optionally, in this embodiment of the disclosure, the determining module 603 is specifically used for:
[0138] Determine the code location information of the initial XPath path in the initial automated test script;
[0139] The initial XPath path is preprocessed to obtain the node information of the initial XPath path;
[0140] Based on the node information of the initial XPath path, generate the corresponding XPath path array;
[0141] Based on the code location information and the XPath path array, the corresponding test script data table is determined.
[0142] Optionally, in this embodiment of the disclosure, the aforementioned establishment module 604 is specifically used for:
[0143] Obtain the position and attribute information of each node in the DOM tree, and generate a node data storage table based on the position and attribute information of each node;
[0144] Iterate through each node in the node data storage table and generate a node queue;
[0145] A matrix model of web page elements is established based on the node depth of each node object in the node queue.
[0146] Optionally, in this embodiment of the disclosure, the test script data table includes code location information and an XPath path array; the optimization module 605 is specifically used for:
[0147] Based on the test script data table and the web page element matrix model, the nodes corresponding to the elements in the XPath path array are obtained;
[0148] Based on the XPath path array and the corresponding nodes, the initial XPath path is optimized to obtain the optimized XPath path array.
[0149] The optimized XPath path array is converted into valid path nodes according to XPath syntax, and the target XPath path is obtained based on each path node.
[0150] Generate a target UI automated test script based on the target XPath path and code location information.
[0151] Optionally, in this embodiment of the disclosure, the optimization module 605 is further configured to:
[0152] If the first element at the end of the XPath path array matches a unique node, then delete the remaining elements in the XPath path array to obtain the optimized XPath path array; if the first element at the end of the XPath path array does not match a unique node, then retrieve the second element from the XPath path array in reverse order.
[0153] If the second element matches a unique node, then keep the first and second elements and delete the remaining elements in the XPath path array to obtain the optimized XPath path array; if the second element matches a non-unique node, repeat the above steps until there is an element that matches a unique node to obtain the optimized XPath path array.
[0154] If the starting element in the XPath path array does not match a unique node or matches the root node, then the traversal and matching are performed again based on the node information of the first element at the end of the XPath path array.
[0155] If the node information of the first element matches multiple duplicate nodes and node information, then the traversal and matching are performed again based on the node information of the second element until an element matches a node and node information that does not contain duplicates. The matched element is retained, and the remaining elements of the XPath path array are deleted to obtain the optimized XPath path array. Otherwise, the first element and its corresponding node information are retained, and the remaining elements of the XPath path array are deleted to obtain the optimized XPath path array.
[0156] In one or more embodiments of this disclosure, the method includes obtaining an initial automated test script and parsing the initial automated test script to extract an initial XPath path; obtaining the content of the webpage to be tested and parsing the webpage content to generate a DOM tree of the webpage to be tested; determining the corresponding test script data table based on the initial XPath path; establishing a webpage element matrix model based on the DOM tree; optimizing the initial XPath path based on the test script data table and the webpage element matrix model to obtain a target XPath path; and generating a target UI automated test script based on the target XPath path. Therefore, this disclosure can automatically adjust the initial XPath path using the test script data table and the webpage element matrix model to obtain the target XPath path, eliminating the need for manual adjustment, thus improving the efficiency and accuracy of test script generation, and consequently improving the efficiency and accuracy of UI testing.
[0157] As shown in Figure 7, the electronic device 700 includes a computing unit 701, which can perform various appropriate actions and processes based on a computer program stored in a read-only memory (ROM) 702 or a computer program loaded from a storage unit 708 into a random access memory (RAM) 703. The RAM 703 can also store various programs and data required for the operation of the electronic device 700. The computing unit 701, ROM 702, and RAM 703 are interconnected via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.
[0158] Multiple components in electronic device 700 are connected to I / O interface 705, including: input unit 706, such as keyboard, mouse, etc.; output unit 707, such as various types of displays, speakers, etc.; storage unit 708, such as disk, optical disk, etc.; and communication unit, such as network card, modem, wireless transceiver, etc. The communication unit allows electronic device 700 to exchange information / data with other electronic devices through computer networks such as the Internet and / or various telecommunications networks.
[0159] The computing unit 701 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of the computing unit 701 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. The computing unit 701 performs the various methods and processes described above, such as the method for generating automated test scripts. For example, in some embodiments, the method for generating automated test scripts may be implemented as a computer software program tangibly contained in a machine-readable medium, such as storage unit 708. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 700 via ROM 702 and / or a communication unit. When the computer program is loaded into RAM 703 and executed by the computing unit 701, one or more steps of the method for generating automated test scripts described above may be performed. Alternatively, in other embodiments, the computing unit 701 may be configured to perform the method for generating automated test scripts by any other suitable means (e.g., by means of firmware).
[0160] Various embodiments of the apparatuses and techniques described above herein can be implemented in digital electronic circuit devices, integrated circuit devices, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), device-on-a-chip (SoC) devices, payload-programmable logic electronic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable device including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage device, at least one input device, and at least one output device, and transmitting data and instructions to the storage device, the at least one input device, and the at least one output device.
[0161] The program code used to implement the methods of this disclosure may be written in any combination of one or more programming languages. This program code may be provided to a processor or controller of a general-purpose computer, special-purpose computer, or other programmable data processing apparatus, such that when executed by the processor or controller, the program code causes the functions / operations specified in the flowcharts and / or block diagrams to be implemented. The program code may be executed entirely on a machine, partially on a machine, as a standalone software package partially on a machine and partially on a remote machine, or entirely on a remote machine or server.
[0162] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution device, apparatus, or electronic device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor device, apparatus, or electronic device, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage electronics, magnetic storage electronics, or any suitable combination of the foregoing.
[0163] To provide interaction with a user, the apparatus and techniques described herein can be implemented on a computer having: a display device for displaying information to the user (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor); and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the computer. Other types of apparatus can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).
[0164] The apparatus and techniques described herein can be implemented in computing devices that include backend components (e.g., as a data server), or computing devices that include middleware components (e.g., an application server), or computing devices that include frontend components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with embodiments of the apparatus and techniques described herein), or computing devices that include any combination of such backend, middleware, or frontend components. The components of the apparatus can be interconnected via digital data communication of any form or medium (e.g., a communication network). Examples of communication networks include local area networks (LANs), wide area networks (WANs), the Internet, and blockchain networks.
[0165] Computer devices can include clients and servers. Clients and servers are generally located far apart and typically interact via communication networks. The client-server relationship is created by computer programs running on the respective computers and having a client-server relationship with each other. A server can be a cloud server, also known as a cloud computing server or cloud host, a hosting product within the cloud computing service system that addresses the shortcomings of traditional physical hosts and VPS (Virtual Private Server, or simply "VPS") services, such as high management difficulty and weak business scalability. Servers can also be distributed server devices or servers incorporating blockchain technology.
[0166] It should be understood that the various forms of processes shown above can be used to rearrange, add, or delete steps. For example, the steps described in this disclosure can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution disclosed in this disclosure can be achieved, and this is not limited herein.
[0167] The specific embodiments described above do not constitute a limitation on the scope of protection of this disclosure. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this disclosure should be included within the scope of protection of this disclosure.
Claims
1. A method for generating automated test scripts, characterized in that, The method includes: obtaining an initial automated test script and parsing the initial automated test script to extract an initial XPath path; obtaining the content of a webpage to be tested and parsing the webpage content to generate a DOM tree of the webpage to be tested; determining a corresponding test script data table based on the initial XPath path; establishing a webpage element matrix model based on the DOM tree; optimizing the initial XPath path based on the test script data table and the webpage element matrix model to obtain a target XPath path, and generating a target UI automated test script based on the target XPath path; wherein, the test script data table includes code location information and an XPath path array, and optimizing the initial XPath path based on the test script data table and the webpage element matrix model to obtain the target XPath path includes: obtaining the XPath path based on the test script data table and the webpage element matrix model. The XPath array is used to find the nodes corresponding to the elements in the XPath array. If the node matched by the first element at the end of the XPath array is unique, the remaining elements in the XPath array are deleted to obtain an optimized XPath array. If the node matched by the first element at the end of the XPath array is not unique, the second element is obtained from the XPath array in reverse order. If the node matched by the second element is unique, the first and second elements are retained, and the remaining elements in the XPath array are deleted to obtain an optimized XPath array. If the node matched by the second element is not unique, the next element is obtained from the XPath array in reverse order for node matching until an element matches a unique node, thus obtaining an optimized XPath array. The optimized XPath array is then converted into valid path nodes according to XPath syntax, and the target XPath path is obtained based on each path node.
2. The method as described in claim 1, characterized in that, The step of parsing the initial automated test script to extract the initial XPath path includes: based on the language and format of the initial automated test script, parsing the initial automated test script using a corresponding parser or library to generate an abstract syntax tree; and based on the abstract syntax tree, traversing the code in the initial automated test script to extract the initial XPath path.
3. The method as described in claim 1, characterized in that, The step of parsing the webpage content to generate the DOM tree of the webpage to be tested includes: determining the webpage type corresponding to the webpage content; if the webpage type is a first type, then parsing the webpage content to generate the DOM tree of the webpage to be tested; if the webpage type is a second type, then using a controller to control the program to simulate the user clicking on the UI interface controls in the current state to generate the DOM tree of the webpage to be tested.
4. The method as described in claim 1, characterized in that, The step of determining the corresponding test script data table based on the initial XPath path includes: determining the code location information of the initial XPath path in the initial automated test script; preprocessing the initial XPath path to obtain the node information of the initial XPath path; generating a corresponding XPath path array based on the node information of the initial XPath path; and determining the corresponding test script data table based on the code location information and the XPath path array.
5. The method as described in claim 1, characterized in that, The step of establishing a web page element matrix model based on the DOM tree includes: obtaining the position information and attribute information of each node in the DOM tree, and generating a node data storage table based on the position information and attribute information of each node; traversing each node in the node data storage table to generate a node queue; and establishing a web page element matrix model based on the node depth of each node object in the node queue.
6. The method as described in claim 1, characterized in that, The step of generating a target UI automated test script based on the target XPath path includes: generating a target UI automated test script based on the target XPath path and the code location information.
7. The method as described in claim 1, characterized in that, The method further includes: if the starting element in the XPath path array matches a non-unique node or matches the root node, then re-traverse and match based on the node information of the first element at the end of the XPath path array; if the node information of the first element matches multiple duplicate nodes and attribute information, then re-traverse and match based on the node information of the second element until an element matches a node and attribute information that does not contain duplicates, retain the matched element, delete the remaining elements of the XPath path array, and obtain an optimized XPath path array; otherwise, retain the first element and its corresponding node information, delete the remaining elements of the XPath path array, and obtain an optimized XPath path array.
8. An apparatus for generating automated test scripts, characterized in that, The apparatus includes: a first acquisition module, configured to acquire an initial automated test script and parse the initial automated test script to extract an initial XPath path; a second acquisition module, configured to acquire the content of a webpage to be tested and parse the webpage content to generate a DOM tree of the webpage to be tested; a determination module, configured to determine a corresponding test script data table based on the initial XPath path; an establishment module, configured to establish a webpage element matrix model based on the DOM tree; and an optimization module, configured to optimize the initial XPath path based on the test script data table and the webpage element matrix model to obtain a target XPath path, and generate a target UI automated test script based on the target XPath path; wherein, the test script data table includes code location information and an XPath path array, and the optimization module is specifically configured to: obtain the XPath path based on the test script data table and the webpage element matrix model. The nodes corresponding to the elements in the array; if the node matched by the first element at the end of the XPath path array is unique, then delete the remaining elements in the XPath path array to obtain the optimized XPath path array; if the node matched by the first element at the end of the XPath path array is not unique, then obtain the second element from the XPath path array in reverse order; if the node matched by the second element is unique, then keep the first and second elements, delete the remaining elements in the XPath path array to obtain the optimized XPath path array; if the node matched by the second element is not unique, continue to obtain the next element from the XPath path array in reverse order for node matching, until there is an element that matches a unique node, to obtain the optimized XPath path array; the optimized XPath path array is converted into valid path nodes according to XPath syntax, and the target XPath path is obtained based on each path node.
9. An electronic device, characterized in that, include: At least one processor; And a memory communicatively connected to the at least one processor, wherein the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the method of any one of claims 1-7.
10. A non-transitory computer-readable storage medium storing computer-executable instructions; wherein the computer-executable instructions, when executed by a processor, are capable of implementing the method of any one of claims 1-7.
Citation Information
Patent Citations
Element operation method and device for automatic test
CN116775447A
Test script transformation analyzer with change guide engine
US20090217303A1