Automatic test script generation method and device and storage medium

By analyzing and optimizing the XPATH path of the automated test script, combined with the webpage DOM tree and the element matrix model, the problem of the ineffectiveness of automated test script in the existing technology in the regression test and mobile UI test, and achieved more efficient and accurate testing Script generation.

CN119377078AActive Publication Date: 2025-01-28CHINA MOBILE GRP GUANGDONG CO LTD +1

Patent Information

Application Number
CN202411318630.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-20
Publication Date
2025-01-28
Estimated Expiration
2044-09-20

AI Technical Summary

Technical Problem

Existing automated test scripts fail in regression testing due to changes in web structure or changes in element attribute values, and the XPath path of mobile UI tests is poorly robust, resulting in the script execution failure.

Method used

By obtaining the XPATH path of the initial automation test script, analyze the web content to generate the DOM tree, establish a webpage element matrix model, and optimize the initial XPath path based on these models, generate the target XPATH path, and finally generate the target UI automated test script.

Benefits of technology

Without manual adjustment, automatically optimize the XPATH path of the test script, which improves the production efficiency and accuracy of test script, thereby improving the efficiency and accuracy of UI testing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119377078A_ABST
    Figure CN119377078A_ABST
Patent Text Reader

Abstract

The invention provides an automatic test script generation method and device and a storage medium, and the method comprises the steps: obtaining an initial automatic test script, and analyzing the initial automatic test script to extract an initial XPath path; obtaining to-be-tested webpage content, and analyzing the webpage content to generate a DOM tree of the to-be-tested webpage; based on the initial XPath path, determining a corresponding test script data table; establishing a webpage element matrix model based on the DOM tree; and optimizing the initial XPath path on the basis of the test script data table and the webpage element matrix model to obtain a target XPath path, and generating a target UI automatic test script on the basis of the target XPath path. Therefore, the initial XPath path can be automatically adjusted through the test script data table and the webpage element matrix model, the target XPath path is obtained, manual adjustment is not needed, the generation efficiency and accuracy of the test script are improved, and then the UI test efficiency and accuracy are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of infrastructure and IT support technology, and in particular to a method, device and storage medium for generating an automated test script. Background Art

[0002] Web UI automated test scripts improve the efficiency of Web testing and are widely used. Automated test scripts usually use XPath paths to locate page elements and record automated test scripts.

[0003] In the prior art, the absolute path of XPath can be obtained from the most original parent element to the element to be located; or, the path expression for positioning relative to the current element node, using the current element as a reference point, locates the target element through certain attributes, tag names, text content and other features to obtain the relative path of XPath.

[0004] However, in the regression test of the above-mentioned prior art, due to slight changes in the Web structure or changes in the attribute values ​​of the web page elements, the web page elements may become invalid, thereby causing the failure of the automated test script. At this time, the automated test script needs to be repaired or regenerated; due to the characteristics of the mobile terminal itself, the same app (device terminal) on different types of mobile phones or different systems of the same type of mobile phone may appear different, making the XPath path on the mobile terminal less robust, and elements may be missing, resulting in script execution failure. Based on this, in order to address the problem of poor robustness of the above-mentioned automated test script, the XPath path of the automated test can be manually adjusted according to different devices or different web page versions, which takes a long time and is prone to omissions, reducing the efficiency and accuracy of the test. Summary of the invention

[0005] In order to overcome the problems existing in the related art, the present disclosure provides a method, device and storage medium for generating an automated test script.

[0006] According to a first aspect of an embodiment of the present disclosure, a method for generating an automated test script is provided, the method comprising:

[0007] Obtaining an initial automated test script, and parsing the initial automated test script to extract an initial XPath path;

[0008] Obtain the content of the web page to be tested, and parse the web page content to generate a DOM tree of the web page to be tested;

[0009] Based on the initial XPath path, determine the corresponding test script data table;

[0010] Based on the DOM tree, a web page element matrix model is established;

[0011] The initial XPath path is optimized based on the test script data table and the web page element matrix model to obtain a target XPath path, and a target UI automation test script is generated based on the target XPath path.

[0012] Optionally, in an embodiment of the present disclosure, the parsing the initial automated test script to extract an 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 a 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 an initial XPath path.

[0015] Optionally, in the embodiment of the present disclosure, the parsing of the webpage content to generate a DOM tree of the webpage to be tested includes:

[0016] Determining the webpage type corresponding to the webpage content;

[0017] If the webpage type is the first type, parsing the webpage content to generate a DOM tree of the webpage to be tested;

[0018] If the webpage type is the second type, the controller controls the program to simulate a user clicking on a UI interface control in the current state to generate a DOM tree of the webpage to be tested.

[0019] Optionally, in the embodiment of the present disclosure, determining the corresponding test script data table based on the initial XPath path includes:

[0020] Determine code location information of the initial XPath path in the initial automated test script;

[0021] Preprocessing the initial XPath path to obtain node information of the initial XPath path;

[0022] Based on the node information of the initial XPath path, generate a corresponding XPath path array;

[0023] Based on the code location information and the XPath path array, a corresponding test script data table is determined.

[0024] Optionally, in the embodiment of the present disclosure, establishing a web page element matrix model based on the DOM tree includes:

[0025] Acquire the position information and attribute information of each node in the DOM tree, and generate a node data storage table based on the position information and attribute information of each node;

[0026] Traverse each node in the node data storage table to generate a node queue;

[0027] A web page element matrix model is established based on the node depth of each node object in the node queue.

[0028] Optionally, in an embodiment of the present disclosure, the test script data table includes code location information and an XPath path array; optimizing the initial XPath path based on the test script data table and the web page element matrix model to obtain a target XPath path, and generating a target UI automation test script based on the target XPath path includes:

[0029] Based on the test script data table and the web page element matrix model, obtaining nodes corresponding to elements in the XPath path array;

[0030] Based on the XPath path array and the corresponding nodes, the initial XPath path is optimized to obtain an optimized XPath path array;

[0031] Convert the optimized XPath path array into valid path nodes according to XPath syntax, and obtain a target XPath path based on each path node;

[0032] The target XPath path is used to generate a target UI automation test script according to the code location information.

[0033] Optionally, in an embodiment of the present disclosure, the initial XPath path is optimized based on the XPath path array and the corresponding node object to obtain an optimized XPath path array, including:

[0034] If the node matched by the first element at the end of the XPath path array is unique, 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, the second element is obtained from the XPath path array in reverse order;

[0035] If the node matched by the second element is unique, the first element and the second element 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, the above steps are repeated until an element matches a unique node to obtain an optimized XPath path array;

[0036] If the node matched by the starting element in the XPath path array is unique or matches the root node, re-traversal matching is performed 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 repeated nodes and attribute information, re-traverse and match based on the node information of the second element until an element matches the node and attribute information that does not include duplicates, retain the matched element, delete the remaining elements of the XPath path array, and obtain the optimized XPath path array; otherwise, retain the first element and the corresponding node information, delete the remaining elements of the XPath path array, and obtain the optimized XPath path array.

[0038] According to a second aspect of an embodiment of the present disclosure, a device for generating an automated test script is provided, the device comprising:

[0039] A first acquisition module is used to acquire an initial automated test script and parse the initial automated test script to extract an initial XPath path;

[0040] The second acquisition module is used to acquire the content of the web page to be tested, and parse the web page content to generate a DOM tree of the web page to be tested;

[0041] A determination module, used to determine a corresponding test script data table based on the initial XPath path;

[0042] A building module is used to build a web page element matrix model 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 a target XPath path, and generate a target UI automation test script based on the target XPath path.

[0044] According to a third aspect of an embodiment of the present disclosure, there is provided an electronic device, including:

[0045] processor;

[0046] a memory for storing 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 an embodiment of the present disclosure, a non-temporary computer-readable storage medium is provided, wherein the computer storage medium stores computer-executable instructions; after the computer-executable instructions are executed by a processor, the method described in the first aspect above can be implemented.

[0049] The technical solution provided by the embodiments of the present disclosure may have the following beneficial effects:

[0050] The present disclosure discloses a method, device and storage medium for generating an automated test script, the method comprising obtaining an initial automated test script, parsing the initial automated test script to extract an initial XPath path; obtaining the content of a web page to be tested, parsing the web page content to generate a DOM tree of the web page to be tested; determining a corresponding test script data table based on the initial XPath path; establishing a web page element matrix model based on the DOM tree; optimizing the initial XPath path based on the test script data table and the web page element matrix model to obtain a target XPath path, and generating a target UI automated test script based on the target XPath path. Thus, the present disclosure can automatically adjust the initial XPath path through the test script data table and the web page element matrix model to obtain a target XPath path without manual adjustment, thereby improving the generation efficiency and accuracy of the test script, and thus improving the efficiency and accuracy of the UI test.

[0051] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS

[0052] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.

[0053] Figure 1 is a flowchart of a method for generating an automated test script according to some embodiments of the present disclosure;

[0054] Figure 2 is a schematic diagram showing 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 structural schematic diagram of a device for generating an automated test script according to some embodiments of the present disclosure;

[0059] Figure 7 It is a block diagram of an electronic device suitable for executing a method for generating an automated test script according to some embodiments of the present disclosure. DETAILED DESCRIPTION

[0060] Some embodiments of the present disclosure will be described in detail here, and examples thereof are shown in the accompanying drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. Various changes, modifications and equivalents of the methods, devices and / or devices described herein will become apparent after understanding the present disclosure. For example, the order of operations described herein is merely an example and is not limited to those orders set forth herein, but can be changed as becomes apparent after understanding the present disclosure, except for operations that must be performed in a specific order. In addition, for clarity and brevity, descriptions of features known in the art may be omitted.

[0061] The embodiments described in some embodiments of the present disclosure below do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.

[0062] Figure 1 The figure is a flow chart of a method for generating an automated test script according to some embodiments of the present disclosure. As shown in the figure, the method may include the following steps:

[0063] Step 101: Obtain an initial automated test script, and parse the initial automated test script to extract an initial XPath path.

[0064] It should be noted that the execution subject of the above-mentioned method for generating automated test scripts is an automated test script generation device, which can be implemented by software and / or hardware. The automated test script generation device in this embodiment 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 the disclosed embodiment, the initial automated test script can be in any language, such as Python or Java. Based on this, after obtaining the initial automated test script, the initial automated test script can be parsed by a corresponding parser or library according to the language and format of the initial automated test script to extract the initial XPath path.

[0067] Specifically, in the embodiment of the present disclosure, the method for parsing the initial automated test script to extract the initial XPath path 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 through a corresponding parser or library to generate an abstract syntax tree;

[0069] In the disclosed embodiment, if the initial automated test script is a Python script, the initial automated test script can be parsed by a corresponding Python parser or library to generate an abstract syntax tree; if the initial automated test script is a Java script, the initial automated test script can be parsed by a 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 to extract the initial XPath path.

[0071] In the disclosed embodiment, after the abstract syntax tree is obtained through the above steps, the code in the initial automated test script can be traversed based on the abstract syntax tree, and the initial XPath path can be extracted by searching for element locating statements used in the initial automated test script, analyzing element attributes used in the element locating statements and parent-child relationships between elements.

[0072] Step 102: Obtain the content of the web page to be tested, and parse the web page content to generate a DOM tree of the web page to be tested.

[0073] In the disclosed embodiment, the program module of the web page to be tested can be obtained and the URL of the web page can be input to access and obtain the corresponding web page content to be tested. In addition, in the disclosed embodiment, after obtaining the web page content to be tested through the above steps, the web page content can be parsed to generate a DOM tree of the web page to be tested.

[0074] Specifically, in the embodiment of the present disclosure, the method for parsing web page content to generate a DOM tree of the web page to be tested may include the following steps:

[0075] Step 1021, determining the webpage type corresponding to the webpage content;

[0076] Step 1022: If the webpage type is the first type, parse the webpage content to generate a DOM tree of the webpage to be tested;

[0077] Step 1023: If the webpage type is the second type, the controller controls the program to simulate a user clicking on a UI interface control in the current state to generate a DOM tree of the webpage to be tested.

[0078] In the embodiment of the present disclosure, the web page type corresponding to the web page content can be determined by judging whether the web page content to be tested includes a trigger event. If the web page content to be tested includes a trigger event, the web page type corresponding to the web page content is determined to be the second type; if the web page content to be tested does not include a trigger event, the web page type corresponding to the web page content is determined to be the first type. In addition, in the embodiment of the present disclosure, the first type can be a static page, and the second type can be a dynamic page.

[0079] Furthermore, in the disclosed embodiment, the above-mentioned static page can be switched through different URLs, and the webpage status (webpage status is the DOM status of the webpage after being parsed by the browser) corresponding to different URLs is also different. Based on this, if the above-mentioned webpage type is the first type, the webpage content can be parsed through the browser background to generate the DOM tree of the webpage to be tested. Among them, in the disclosed embodiment, the DOM tree can be obtained through the DOM API of JavaScript, and the DOM tree of the HTML document can be accessed using its built-in document object, and the entire DOM tree can be traversed through the traverseDOM method to generate the DOM tree of the webpage to be tested.

[0080] Furthermore, in the disclosed embodiments, the dynamic page can use JavaScript or Ajax to implement partial refresh of the web page, wherein when the user triggers a trigger event, the JavaScript code bound to the trigger event will be called, and then the DOM content and structure parsed by the browser will be dynamically modified. Also, in the disclosed embodiments, when multiple web page states belong to the same URL, a dynamic page can have multiple different web page states at the same time, and the content and structure of the DOM tree corresponding to different web page states are different. Based on this, in regression testing, the web page elements of the dynamic page may become invalid. Therefore, if the web page type is the second type, the controller control program can be used to simulate the user clicking on the UI interface control in the current state. If the event can be triggered and the DOM tree can be updated, the state recorded in the state machine can be updated through the controller, and the event can be recorded.

[0081] Specifically, Figure 2 Schematic diagram of a dynamic webpage triggering event proposed in an embodiment of the present disclosure. Figure 2As shown, first record the current state, and use the program to simulate the user trigger event to generate a list of triggerable events for the current state; if the current list of triggerable events is empty, determine whether the termination condition is reached (no triggerable events in all states), if the termination condition is met, end, otherwise the state is returned and the original page is reloaded; if the current list of triggerable events is not empty, trigger an event randomly, if it causes the web page elements and their Xpath paths in the area to be tested to change, set it to a new state, otherwise trigger another event, repeat the above steps until all events in the above list of triggerable events have been triggered, and generate the DOM tree of the web page to be tested.

[0082] Step 103: Based on the initial XPath path, determine the corresponding test script data table.

[0083] In the disclosed embodiment, after the initial XPath path is obtained through the above steps, the corresponding test script data table can be determined based on the initial XPath path.

[0084] Specifically, in the embodiment of the present 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, determining the code location information of the initial XPath path in the initial automated test script;

[0086] Step 1032: pre-process the initial XPath path to obtain node information of the initial XPath path;

[0087] Step 1033: Generate a corresponding XPath path array based on the node information of the initial XPath path;

[0088] Step 1034: Determine the corresponding test script data table based on the code location information and the XPath path array.

[0089] In the disclosed embodiment, the code file of the initial automated test script may be a Python file, a Java file or a JavaScript file, wherein the code location information of the initial XPath path in the initial automated test script may be determined by searching for code segments related to the location of page elements in the web page.

[0090] For example, in the Selenium automated testing framework, in the element location code found by find_element_by_xpath, the initial XPath path can be the string in find_element_by_xpath(' / div / xpath / input). Also, in the disclosed embodiment, if the initial XPath path is dynamically generated by a variable or expression, the target XPath path can be determined by tracking the value of the variable or the calculation result of the expression.

[0091] Furthermore, in the embodiment of the present disclosure, the initial XPath path is preprocessed to obtain node information of the initial XPath path, wherein the node information may include element nodes, element positions, and element attributes. Specifically, the element nodes may be elements in an HTML or XML document, such as , , ; The position of an element can be the depth of the element, such as [2] of the book element in / bookstore / book[2] / title; The attributes of an element can be the attributes of the element node, such as <a href="https: example.com">The "href" in the .

[0092] Furthermore, in the embodiment of the present 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 where the node information is located to generate a corresponding XPath path array.

[0093] Furthermore, in the embodiment of the present disclosure, after obtaining the code location information and the XPath path array through the above steps, the code location information and the 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] in the test script data table, wherein [code location id] and [XPath path array] are in one-to-one correspondence.

[0094] Step 104: Building a web page element matrix model based on the DOM tree.

[0095] In the disclosed embodiment, after the DOM tree of the web page to be tested is generated through the above steps, a web page element matrix model can be established based on the DOM tree.

[0096] Specifically, in the embodiment of the present disclosure, the method for establishing a web page element matrix model based on a DOM tree may include the following steps:

[0097] Step 1041: Obtain the location information and attribute information of each node in the DOM tree, and generate a node data storage table based on the location information and attribute information of each node;

[0098] Step 1042, traverse each node in the node data storage table to generate a node queue;

[0099] Step 1043: Establish a web page element matrix model based on the node depth of each node object in the node queue.

[0100] In the disclosed embodiment, the position information of the node of the DOM tree can be obtained through getBoundingClientRect in JavaScript, and the attribute information of the node can be obtained through attributes, wherein the attribute information of the node can include the element node, the position of the element and the attribute of the element.

[0101] Furthermore, in the disclosed embodiment, the node ID can be generated by using the hash algorithm through the location information of each node, and the node ID, element node, element location, element attribute and parent node ID of the node are stored in the node data storage table. If the node has no parent node, a null value is stored.

[0102] Furthermore, in the 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, and a corresponding node tree is generated, and a corresponding node queue is generated by storing the node tree in a queue, wherein the node id of the root node in each node tree is used as an object attribute to facilitate subsequent accurate traversal.

[0103] Further, in the embodiment of the present disclosure, after obtaining the node queue through the above steps, the method of establishing the web page 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 the web page element matrix model according to the maximum node depth of all node objects; storing each node object in the web page element matrix model in sequence based on the node depth of each node object. Among them, in the embodiment of the present disclosure, the method of storing each node object in sequence in the web page element matrix model based on the node depth of each node object may include: storing each node object in sequence in the web page element matrix model in order from small to large 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 a target XPath path, and generate a target UI automation test script based on the target XPath path.

[0105] Among them, in the embodiment of the present 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 based on the target XPath path, the target UI automation test script can be generated.

[0106] Specifically, in the embodiment of the present 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 automation 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 node corresponding to the element in the XPath path array;

[0108] Step 1052: Based on the XPath path array and the corresponding nodes, the initial XPath path is optimized to obtain an 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 a target UI automation test script based on the target XPath path and the code location information.

[0111] In the disclosed embodiment, an XPath path array in the first data in the test script data table is obtained, and elements of the XPath path array are matched with a web page element matrix model to obtain nodes corresponding to the elements in the XPath path array.

[0112] Specifically, in an embodiment of the present disclosure, a method for matching elements of an XPath path array with a web page element matrix model to obtain nodes corresponding to the elements in the XPath path array may include: searching, based on the length of the XPath path array, for a node depth that matches the length of the XPath path array in the web page element matrix model; and obtaining nodes corresponding to the elements in the XPath path array based on element nodes of the elements in the XPath path array and the matched node depth.

[0113] Among them, in the embodiment of the present disclosure, reverse matching is performed from the first element n at the end of the XPath path array, and the tree nodes of the node tree corresponding to the node queue of the above step 1042 are traversed according to the above-mentioned matched node depth, and the tree nodes corresponding to the tree nodes and the element nodes of the element whose attribute information is the same as the element node of the element are determined as nodes matching element n, and the matched nodes and the parent nodes and child nodes of each node 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 matching the second element are stored in a temporary queue B, and the third element of the XPath path array is obtained in reverse order, and the third element is traversed in the node tree until all elements of the XPath path array are matched to corresponding nodes.

[0114] Furthermore, in the embodiment of the present disclosure, based on the requirements of simplicity (shortest path) and uniqueness (no repeated paths), the XPath path array in the test script data table is optimized.

[0115] Specifically, in the embodiment of the present disclosure, if the node matched by the first element at the end of the XPath path array is unique, 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, the second element is obtained from the XPath path array in reverse order; if the node matched by the second element is unique, the first element and the second element 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, the above steps are repeated until there is an element that matches a unique node to obtain an optimized XPath path array. path array; if the node matched by the starting element in the XPath path array is unique 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 repeated nodes and attribute information, then re-traverse and match based on the node information of the second element until an element matches the node and attribute information that does not include duplication, retain the matched element, delete the remaining elements of the XPath path array, and obtain the optimized XPath path array; otherwise, retain the first element and the corresponding node information, delete the remaining elements of the XPath path array, and obtain the optimized XPath path array.

[0116] For example, in the embodiment of the present disclosure, assuming that the end element n (the last element) of the XPath path array is traversed in the nodes, if element n matches a node to which the node object C belongs ( Figure 3 As shown), the node object C is unique, that is, there are no other nodes, then the element n is unique. According to the uniqueness and simplicity requirements of the XPath path, the element in the corresponding optimized XPath path only needs element n, then the other elements in the XPath path array are non-essential elements. Based on this, delete other elements and retain element n to obtain the optimized XPath path array.

[0117] And, in the embodiment of the present disclosure, if the element n matches multiple nodes ( Figure 4 As shown), that is, if there are other identical nodes in node object D, element n is repeated, and 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 in the parent nodes to which multiple node objects D belong. If there is only one node matched by element m, it means that element m and element n can form a unique XPath path, then other non-essential 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 are continued until a unique element is matched, and the elements before the element in reverse order are deleted to obtain the optimized XPath path array.

[0118] Furthermore, in the embodiment of the present 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 means that the XPath path constructed by the elements of the XPath path array has multiple repeated paths in the DOM tree, and then the traversal and matching is re-performed 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 the embodiment of the present disclosure, if element n matches the unique node corresponding to the node object D and its element's attribute id=1 (such as Figure 5 ), there are no multiple duplicate nodes, indicating that the node information of element n is unique, then delete the other elements of the XPath path array, retain element n and its node information, and obtain the optimized XPath path array; if element n matches multiple duplicate nodes and their node information, then get the next element of the XPath path array in reverse order to continue traversing and matching until there are no duplicate nodes and their attribute information, then delete the other elements of the XPath path array, retain the currently matched element and its node information, and obtain the optimized XPath path array.

[0120] Furthermore, in the embodiment of the present disclosure, the optimized XPath path array is obtained through the above steps, and the optimized XPath path array can be updated to the test script data table, wherein, if a valid optimized XPath path array cannot be obtained according to the above steps, an exception mark is added to the XPath path array and updated to the test script data table.

[0121] In the disclosed embodiment, after obtaining the optimized XPath path array through the above steps, the optimized XPath path array can be converted into a valid path node according to the XPath syntax, and " / " is added in the middle of each path node, and the target XPath path is formed by reverse splicing, and the target UI automation test script is regenerated according to the code location information corresponding to the target XPath path. If there is an XPath path array with an abnormal mark, it is synchronously output in the target UI automation test script to indicate the invalidity of the XPath path.

[0122] The present disclosure proposes a method for generating an automated test script, which includes obtaining an initial automated test script, parsing the initial automated test script to extract an initial XPath path; obtaining the content of a web page to be tested, parsing the web page content to generate a DOM tree of the web page to be tested; determining a corresponding test script data table based on the initial XPath path; establishing a web page element matrix model based on the DOM tree; optimizing the initial XPath path based on the test script data table and the web page element matrix model to obtain a target XPath path, and generating a target UI automated test script based on the target XPath path. Thus, the present disclosure can automatically adjust the initial XPath path through the test script data table and the web page element matrix model to obtain a target XPath path without manual adjustment, thereby improving the generation efficiency and accuracy of the test script, and thus improving the efficiency and accuracy of the UI test.

[0123] Figure 6 According to some embodiments of the present disclosure, a device for generating an automated test script is shown, and the device may include:

[0124] The first acquisition module 601 is used to acquire an initial automated test script and parse the initial automated test script to extract an initial XPath path;

[0125] The second acquisition module 602 is used to acquire the content of the web page to be tested, and parse the web page content to generate a DOM tree of the web page to be tested;

[0126] A determination module 603 is used to determine a corresponding test script data table based on the initial XPath path;

[0127] Establishing module 604, used to establish a web page element matrix model 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 a target XPath path, and generate a target UI automation test script based on the target XPath path.

[0129] In the embodiment of the present disclosure, the first acquisition module 601 is specifically used to:

[0130] Based on the language and format of the initial automated test script, the initial automated test script is parsed by a corresponding parser or library to obtain a parsing result;

[0131] Generate an abstract syntax tree based on the parsing results;

[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 the embodiment of the present disclosure, the second acquisition module 602 is specifically configured to:

[0134] Determine the web page type corresponding to the web page content;

[0135] If the web page type is the first type, the web page content is parsed to generate a DOM tree of the web page to be tested;

[0136] If the webpage type is the second type, the controller controls the program to simulate a user clicking on a UI interface control in the current state to generate a DOM tree of the webpage to be tested.

[0137] Optionally, in the embodiment of the present disclosure, the determination module 603 is specifically configured to:

[0138] Determine code location information of an initial XPath path in the initial automated test script;

[0139] Preprocess the initial XPath path to obtain node information of the initial XPath path;

[0140] Generate the corresponding XPath path array based on the node information of the initial XPath path;

[0141] Based on the code location information and the XPath path array, the corresponding test script data table is determined.

[0142] Optionally, in the embodiment of the present disclosure, the establishment module 604 is specifically used to:

[0143] Obtain the location information and attribute information of each node in the DOM tree, and generate a node data storage table based on the location information and attribute information of each node;

[0144] Traverse each node in the node data storage table and generate a node queue;

[0145] A web page element matrix model is established based on the node depth of each node object in the node queue.

[0146] Optionally, in an embodiment of the present disclosure, the test script data table includes code location information and an XPath path array; the optimization module 605 is specifically used to:

[0147] 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;

[0148] Based on the XPath path array and the corresponding nodes, the initial XPath path is optimized to obtain an optimized XPath path array;

[0149] 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;

[0150] Generate the target UI automation test script based on the target XPath path and code location information.

[0151] Optionally, in the embodiment of the present disclosure, the optimization module 605 is further used to:

[0152] If the node matched by the first element at the end of the XPath path array is unique, 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, get the second element from the XPath path array in reverse order;

[0153] If the node matched by the second element is unique, keep the first and second elements, delete the remaining elements in the XPath path array, and obtain the optimized XPath path array; if the node matched by the second element is not unique, repeat the above steps until an element matches the node uniquely, and obtain the optimized XPath path array;

[0154] If the node matched by the starting element in the XPath path array is unique or matches the root node, re-traverse and match 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 repeated nodes and node information, re-traverse and match based on the node information of the second element until an element matches a node and node information that does not include duplicates, retain the matched element, delete the remaining elements of the XPath path array, and obtain the optimized XPath path array; otherwise, retain the first element and the corresponding node information, delete the remaining elements of the XPath path array, and obtain the optimized XPath path array.

[0156] In one or more embodiments of the present disclosure, the method includes obtaining an initial automated test script, parsing the initial automated test script to extract an initial XPath path, obtaining the content of a web page to be tested, parsing the web page content to generate a DOM tree of the web page to be tested, determining a corresponding test script data table based on the initial XPath path, establishing a web page element matrix model based on the DOM tree, optimizing the initial XPath path based on the test script data table and the web page element matrix model to obtain a target XPath path, and generating a target UI automated test script based on the target XPath path. Thus, the present disclosure can automatically adjust the initial XPath path through the test script data table and the web page element matrix model to obtain a target XPath path without manual adjustment, thereby improving the generation efficiency and accuracy of the test script, and thus improving the efficiency and accuracy of the UI test.

[0157] like Figure 7 As shown, the electronic device 700 includes a computing unit 701, which can perform various appropriate actions and processes according to 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) 707. In RAM 703, various programs and data required for the operation of the electronic device 700 can also be stored. The computing unit 701, ROM 702, and RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.

[0158] A number of components in the electronic device 700 are connected to the I / O interface 705, including: an input unit 708, such as a keyboard, a mouse, etc.; an output unit 707, such as various types of displays, speakers, etc.; a storage unit 708, such as a disk, an optical disk, etc.; and a communication unit 709, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 709 allows the electronic device 700 to exchange information / data with other electronic devices through a computer network such as the Internet and / or various telecommunication networks.

[0159] The computing unit 701 may be a variety of general and / or special 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 dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 701 performs the various methods and processes described above, such as the generation method of the automated test script. For example, in some embodiments, the generation method of the automated test script may be implemented as a computer software program, which is tangibly contained in a machine-readable medium, such as a 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 communication unit 709. When the computer program is loaded into RAM 703 and executed by the computing unit 701, one or more steps of the generation method of the automated test script described above may be performed. Alternatively, in other embodiments, the computing unit 701 may be configured to perform the generation method of the automated test script by any other appropriate means (e.g., by means of firmware).

[0160] Various embodiments of the devices 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), devices on a chip (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable device including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage device, at least one input device, and at least one output device, and transmit data and instructions to the storage device, the at least one input device, and the at least one output device.

[0161] The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that the program code, when executed by the processor or controller, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The program code may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.

[0162] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution device, device, or electronic device. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor devices, devices, or electronic devices, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage electronic device, a magnetic storage electronic device, or any suitable combination of the foregoing.

[0163] To provide interaction with a user, the devices and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the 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 acoustic input, voice input, or tactile input).

[0164] The apparatus and techniques described herein may be implemented in a computing device including backend components (e.g., as a data server), or a computing device including middleware components (e.g., an application server), or a computing device including frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the apparatus and techniques described herein), or a computing device including any combination of such backend components, middleware components, or frontend components. The components of the apparatus may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), the Internet, and a blockchain network.

[0165] A computer device may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship between the client and the server is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or a cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services ("Virtual Private Server", or "VPS" for short). The server may also be a server of a distributed device, or a server combined with a blockchain.

[0166] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this disclosure can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document does not limit this.

[0167] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.

Claims

1. A method for generating an automated test script, characterized in that: The method comprises: Obtaining an initial automated test script, and parsing the initial automated test script to extract an initial XPath path; Obtain the content of the web page to be tested, and parse the web page content to generate a DOM tree of the web page to be tested; Based on the initial XPath path, determine the corresponding test script data table; Based on the DOM tree, a web page element matrix model is established; The initial XPath path is optimized based on the test script data table and the web page element matrix model to obtain a target XPath path, and a target UI automation test script is generated based on the target XPath path.

2. The method according to claim 1, characterized in that The step of parsing the initial automated test script to extract an initial XPath path includes: Based on the language and format of the initial automated test script, the initial automated test script is parsed by a corresponding parser or library to generate an abstract syntax tree; Based on the abstract syntax tree, the code in the initial automated test script is traversed to extract an initial XPath path.

3. The method according to 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 the first type, parsing the webpage content to generate a DOM tree of the webpage to be tested; If the webpage type is the second type, the controller controls the program to simulate a user clicking on a UI interface control in the current state to generate a DOM tree of the webpage to be tested.

4. The method according to claim 1, characterized in that The determining of the corresponding test script data table based on the initial XPath path includes: Determine code location information of the initial XPath path in the initial automated test script; Preprocessing the initial XPath path to obtain node information of the initial XPath path; Based on the node information of the initial XPath path, generate a corresponding XPath path array; Based on the code location information and the XPath path array, a corresponding test script data table is determined.

5. The method according to claim 1, characterized in that The step of establishing a web page element matrix model based on the DOM tree includes: Acquire the position information and attribute information of each node in the DOM tree, and generate a node data storage table based on the position information and attribute information of each node; Traverse each node in the node data storage table to generate a node queue; A web page element matrix model is established based on the node depth of each node object in the node queue.

6. The method according to claim 1, characterized in that The test script data table includes code location information and an XPath path array; optimizing the initial XPath path based on the test script data table and the web page element matrix model to obtain a target XPath path, and generating a target UI automation test script based on the target XPath path, including: Based on the test script data table and the web page element matrix model, obtaining nodes corresponding to elements in the XPath path array; Based on the XPath path array and the corresponding nodes, the initial XPath path is optimized to obtain an optimized XPath path array; Convert the optimized XPath path array into valid path nodes according to XPath syntax, and obtain a target XPath path based on each path node; The target XPath path is used to generate a target UI automation test script according to the code location information.

7. The method according to claim 1, characterized in that The step of optimizing the initial XPath path based on the XPath path array and the corresponding nodes to obtain an optimized XPath path array includes: If the node matched by the first element at the end of the XPath path array is unique, 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, the second element is obtained from the XPath path array in reverse order; If the node matched by the second element is unique, the first element and the second element 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, the above steps are repeated until an element matches a unique node to obtain an optimized XPath path array; If the node matched by the starting element in the XPath path array is unique or matches the root node, re-traversal matching is performed 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 repeated nodes and attribute information, re-traverse and match based on the node information of the second element until an element matches the node and attribute information that does not include duplicates, retain the matched element, delete the remaining elements of the XPath path array, and obtain the optimized XPath path array; otherwise, retain the first element and the corresponding node information, delete the remaining elements of the XPath path array, and obtain the optimized XPath path array.

8. A device for generating an automated test script, characterized in that: The device comprises: A first acquisition module is used to acquire an initial automated test script and parse the initial automated test script to extract an initial XPath path; The second acquisition module is used to acquire the content of the web page to be tested, and parse the web page content to generate a DOM tree of the web page to be tested; A determination module, used to determine a corresponding test script data table based on the initial XPath path; A building module is used to build a web page element matrix model based on the DOM tree; 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 a target XPath path, and generate a target UI automation test script based on the target XPath path.

9. An electronic device, characterized in that: include: at least one processor; as well as a memory in communication with the at least one processor, wherein: The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method according to any one of claims 1 to 7.

10. A non-transitory computer-readable storage medium, wherein the computer storage medium stores computer-executable instructions; after the computer-executable instructions are executed by a processor, the method according to any one of claims 1 to 7 can be implemented.

Citation Information

Patent Citations

  • Method and device for identifying control element

    CN105224462A

  • Error correction method and device for page test script

    CN115687075A

  • Element operation method and device for automatic test

    CN116775447A

  • Test script transformation analyzer with change guide engine

    US20090217303A1

  • WEB UI Automation Maintenance Tool

    US20190340224A1

Cited By

  • Test script generation method and device, storage medium and program product

    CN120723657A