A method for automatically generating Xpath
By automatically generating XPath algorithm and automatically obtaining DOM elements and mouse operations with the browser, the problem of XPath writing relies on manual experience in the existing technology is solved, and efficient web automation testing is achieved, which reduces the test threshold and maintenance costs.
Patent Information
- Application Number
- CN202211585396.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-11
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2042-12-11
AI Technical Summary
In the prior art, XPath preparation relies on the experience and technical capabilities of testers, resulting in high writing and maintenance costs, and frequent rewriting when business needs change, making it difficult to achieve efficient web automation testing.
Through the algorithm that automatically generates XPath, the browser automatically obtains DOM elements, combines JQuery and CSS technology, mouse suspension and click operations, multiple XPaths are automatically generated, and the most efficient execution is selected, reducing the dependence on testers' technical experience.
It realizes that there is no need to write XPath manually, lowers the threshold for web automation testing, improves scripting efficiency and execution efficiency, and reduces maintenance costs and false alarm rates.
Smart Images

Figure 221211162523 
Figure 221211162527 
Figure 221211162530
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of data testing, and particularly relates to a method for automatically generating XPath. Background Art
[0002] XPath, namely XML Path Language, is based on the tree structure of XML and provides the ability to find nodes in the data structure tree and traverse elements and attributes. In automated testing, element location is the most important part, and there are various location methods, among which XPath location plays an important role. Usually, the compilation of XPath is carried out manually by testers. Due to the large differences in the experience and technical capabilities of testers, coupled with the diversity and variability of test requirements, Web automated test scripts also need to change accordingly. Therefore, the manually written XPath scripts often pose many challenges when actually performing automated tests, resulting in low efficiency, high costs for script compilation and maintenance, and even the inability to complete the expected automated tests. Summary of the Invention
[0003] To solve the above problems, the present invention provides an algorithm for automatically generating XPath, which enables the automatic generation of multiple XPaths without manual writing, and selects the most intuitive and highest-execution-efficiency one among them to improve the script compilation efficiency and the script execution efficiency.
[0004] A method for automatically generating XPath includes the following steps
[0005] Step 1: Obtain and parse the DOM elements of the page. The acquisition and parsing of DOM elements are automatically completed by the browser, and the belonging of the tags is reflected according to the hierarchical relationship of the tags, forming a tree structure. After parsing, operations are performed on each element;
[0006] Step 2: Mouse hover on the element, highlight it, and cancel the highlight when the mouse moves away. This is implemented using JS code, and the mouse hover event onMouseOver and the mouse leave event onMouseOut of JQuery are used to control the page style. Set the highlight display CSS style when hovering, and set the CSS style to cancel the highlight when the mouse leaves;
[0007] Step 3: Click on the element attribute instrumentation, highlight the clicked element, and set a display border. Obtain the currently clicked element through Jquery, set a unique attribute and attribute value for the current element, and combine CSS technology to set the highlight display and the border color style for the currently clicked element;
[0008] Step 4: For elements with a border, obtain the tag names of these elements, including the input tag, a tag, and button tag, and obtain the tag name of the current element through Jquery technology;
[0009] Step 5: For elements with a border, obtain all attributes and their values, as well as the parent node, child nodes, and sibling nodes within the tag. The specific attributes include class, href, title, and text. Use the each loop in Jquery technology to obtain all attributes and their values within the tag, and use parent(), children(), prev(), and next() to obtain the parent node, child nodes, previous sibling node, and next sibling node of the current element;
[0010] Step 6: Concatenate the XPath based on the attributes and their values;
[0011] Step 7: Search in the html document according to the XPath concatenated in Step 6, and obtain the number X of found results. If the search value X is equal to 1, then the current XPath is an XPath of the specified attribute of the current element, and generate the current XPath; if the search value X is greater than 1, then the serial number is increased according to the quantity on the basis of the current XPath, and position the element according to the XPath with the serial number. Check whether the positioned element has the staking attribute. If it does, generate the XPath; if not, continue to increment the XPath serial number by 1 until an element with the staking attribute is found and the XPath is generated; at the same time, the element with the staking attribute is the element to be searched, and then the current XPath is the final XPath to be generated;
[0012] Step 8: By analogy, generate multiple XPaths through multiple attribute values of the clicked element;
[0013] In Steps 3, 4, and 5, the border is red.
[0014] The beneficial effects of the present invention are as follows: Through the above XPath automatic generation method, first, when doing Web automation testing, there is no need to master the relevant knowledge of XPath, eliminating the dependence of testers on XPath knowledge when writing scripts, enabling junior testers to perform Web automation testing, thus reducing the threshold of Web automation testing; second, when the business requirements change and the page changes, there is no need for testers to write again, and only need to automatically obtain the xpath again, which improves the script compilation efficiency and reduces the script maintenance cost at the same time. Finally, among the multiple generated XPaths, select the XPath with the highest execution efficiency as the XPath used in the script, which can reduce the execution time of Web automation testing, improve the execution efficiency of Web automation testing, and reduce the false alarm rate of Web automation test results.
[0015] In summary, through the method described in the present invention, XPath can be automatically generated quickly and accurately, and the efficiency of Web automation test script preparation is improved, the cost of Web automation test script maintenance is reduced, and at the same time, the threshold of Web automation test is lowered. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 It is a flowchart of the XPath automatic generation method;
[0017] Figure 2 It is a schematic diagram of the page tree structure;
[0018] Figure 3 It is a schematic diagram of the mouse hovering effect;
[0019] Figure 4 It is a schematic diagram of the instrumentation effect;
[0020] Figure 5 It is a schematic diagram of splicing XPath with attributes and attribute values;
[0021] Figure 6 It is a schematic diagram of marking multiple XPaths;
[0022] Figure 7 Schematic illustration of multiple xpath examples Figure Ⅰ ;
[0023] Figure 8 Schematic illustration of multiple xpath examples Figure Ⅱ 。 DETAILED DESCRIPTION OF THE EMBODIMENTS
[0024] The embodiments of the present invention are summarized below in conjunction with the accompanying drawings, including the following steps: Obtain the interface DOM element: Use languages such as JS to obtain the current DOM element and parse it; Mouse hover highlighting: When the mouse moves to the interface element, add a red border to the current interface element, and when the mouse moves away, the red border is cancelled; Element attribute instrumentation: When the element is clicked, instrument the current element, that is, add an attribute and an attribute value to the current element, and obtain the tag name and specific value of the current element; Concatenate and generate the XPath of each attribute: Set a JS array variable, obtain all the attributes and attribute values of the current element, and loop to store the concatenated and generated XPath. Obtain the Tag name of the tag, and concatenate and generate the XPath according to the name and value of the Tag and store it in the JS variable; Check whether the current element is in the iframe. If so, concatenate and generate the XPath of the iframe and store it in the JS variable; Iterate through all the attributes and attribute values, concatenate and generate the XPath according to each attribute and attribute value, and store them in the JS variable in turn. Obtain the number of elements through XPath: Iterate through the JS array variable, and find the number of elements through each XPath. Generate XPath: Verify whether the number of elements is equal to 1. If it is equal to 1, the current XPath is the final XPath to be generated; If it is greater than 1, add 1 to the base serial number of the current XPath, locate the element according to the numbered XPath, check whether the located element has an instrumented attribute. If it does, generate the XPath. If not, continue to add 1 to the XPath serial number until an element with an instrumented attribute is found to generate the XPath.
[0025] The specific step-by-step method is described as follows. A method for automatically generating XPath includes the following steps:
[0026] Step 1: Obtain and parse the DOM element of the page. The acquisition and parsing of the DOM element are automatically completed by the browser, and the belonging of the tags is reflected according to the hierarchical relationship of the tags, forming a tree structure. After parsing, operate on each element;
[0027] Step 2: Mouse hover over the element to highlight it. When the mouse moves away, the highlighting is cancelled. This is implemented using JS code. The mouse hover event onMouseOver and the mouse leave event onMouseOut of JQuery are used to control the page style. Set the highlighting CSS style when hovering, and set the CSS style to cancel the highlighting when the mouse leaves;
[0028] Step 3: Click on the element to instrument the attribute. Highlight the clicked element and set the display border. Obtain the currently clicked element through Jquery, set a unique attribute and attribute value for the current element, and combine CSS technology to set the highlighting display and the border color style for the currently clicked element;
[0029] Step 4: For elements with borders, obtain the tag names of these elements, including input tags, a tags, and button tags, and use Jquery technology to obtain the tag names of the current elements;
[0030] Step 5: For elements with borders, obtain all attributes and their values, as well as the parent node, child nodes, and sibling nodes within the tag. The specific attributes include class, href, title, and text. Use the each loop in Jquery technology to obtain all attributes and their values within the tag, and use parent(), children(), prev(), and next() to obtain the parent node, child nodes, previous sibling node, and next sibling node of the current element;
[0031] Step 6: Concatenate the XPath based on the attributes and their values;
[0032] Step 7: Search in the html document according to the XPath concatenated in Step 6, and obtain the number X of the found elements. If the search value X is equal to 1, then the current XPath is an XPath of the specified attribute of the current element, and generate the current XPath; if the search value X is greater than 1, then on the basis of the current XPath, the serial number is increased according to the quantity. Locate the element according to the numbered XPath, check whether the located element has the instrumentation attribute. If it has, generate the XPath; if not, continue to increment the XPath serial number by 1 until an element with the instrumentation attribute is found and the XPath is generated; at the same time, the element with the instrumentation attribute is the element being searched for, then the current XPath is the final XPath to be generated;
[0033] Step 8: By analogy, generate multiple XPaths through multiple attribute values of the clicked element. In Steps 3, 4, and 5, the border is red.
[0034] Illustrate with an actual case:
[0035] Step 1: Obtain and parse the DOM elements of the page: The acquisition and parsing of DOM elements are automatically completed by the browser, and the belonging of the tags is reflected according to the hierarchical relationship of the tags, forming a tree structure. After parsing, each element can be operated on. For example, when the browser opens a page, check that the displayed element in the inspection element is a DOM element, which is displayed according to the tag hierarchy, forming a tree structure, as shown in Figure 2 the schematic diagram of the page tree structure;
[0036] Step 2: When the mouse hovers over an element, it is highlighted, and when the mouse moves away, the highlight is cancelled. This is implemented using JS code. The mouse hover event onMouseOver and mouse leave event onMouseOut of JQuery are used to control the page style. When hovering, the CSS style for highlighting is set, and when the mouse leaves, the CSS style for cancelling the highlight is set. For example, Figure 3 In the schematic diagram of the mouse hover effect, when the mouse hovers over [Home], the label border is highlighted and shown in red;
[0037] Step 3: Insert a stake for the click element attribute. When the element is clicked, it is highlighted with a red border. The currently clicked element is obtained through Jquery, a unique attribute and its value are set for the current element, and combined with CSS technology, the current clicked element is set to be highlighted and a red border is set. The effect of staking is as Figure 4 shown in the schematic diagram of the staking effect;
[0038] Step 4: For the element with a red border, obtain the tag name of the element, such as input tag, a tag, button tag, etc. The tag name of the current element is obtained through Jquery technology;
[0039] Step 5: For the element with a red border, obtain all the attributes and their values, as well as the parent node, child nodes, sibling nodes, etc. within the tag. Specific attributes such as class, href, title, text, etc. All the attributes and their values within the tag are obtained through the each loop of Jquery technology, and the parent node, child nodes, previous sibling node, next sibling node, etc. of the current element are obtained through methods such as parent(), children(), prev(), next();
[0040] Step 6: Concatenate the XPath based on the attributes and their values. For example, if the clicked element tag is an a tag and the value of class is [s-tab-item], the concatenated XPath is [ / / a[@class='s-tab-item']]. Similarly, the corresponding XPath is concatenated according to other attributes, parent nodes, child nodes, sibling nodes, etc. Keywords used to locate the parent and sibling nodes of the element: parent (parent::), sibling (following-sibling::, preceding-sibling::). For example, to obtain the previous sibling node of the current element, if the XPath of the current element is [ / / div[@id='loading-bg']], then the XPath of its previous sibling node is: [ / / div[@id='loading-bg'] / / preceding-sibling::img]. For example, Figure 5Schematic diagram of splicing XPath for attributes and attribute values, the XPath obtained through the sibling node after the node with id 'loading-bg';
[0041] Step 7: Search in the html document according to the previously spliced XPath to obtain the number X of found results. If the search value X is equal to 1, then the current XPath is one of the XPath for the specified attribute of the current element, and generate the current XPath; if the search value X is greater than 1, then increment the serial number by 1 on the basis of the current XPath, locate the element according to the XPath with the serial number, check whether the located element has the instrumentation attribute. If it does, generate the XPath; if not, continue to increment the XPath serial number by 1 until an element with the instrumentation attribute is found to generate the XPath. For example: the XPath of the current element is
/ / a[@class='s-tab-item']
lfrm-item-label
( / / label[@class='lfrm-item-label'])[1]
login-form-item
( / / div[@class='login-form-item'] / / label)[1]
[0042] Step 8: By analogy, multiple XPaths can be generated through multiple attribute values of the clicked element;
[0043] The following figure is an example of multiple finally generated xpaths: through Figure 7 Schematic diagram of multiple xpath examples Figure Ⅰ As can be seen, there are XPaths obtained through the classname attribute, XPaths obtained through the parent (parent::) node, and so on. Figure 8 Schematic diagram of multiple xpath examples Figure ⅡExample 2 of the multiple XPaths finally generated: Click on the login element with the mouse to obtain multiple Xpath values for login. ① marked in the figure is the Xpath obtained through the class attribute; ② is the Xapth obtained through the css style, and ③ and ④ are the XPaths obtained through the sibling nodes (following-sibling::), and ⑤ is the Xpath obtained through the parent (parent::) node.
[0044] Features of the prior art:
[0045] 1. The element paths in the test script need to be manually compiled;
[0046] 2. Before compiling the element paths, it is necessary to learn and master Xapth knowledge;
[0047] 3. The compiled script may require the Xpath to be recompiled due to changes in requirements;
[0048] 4. There are significant differences in the technical experience and skills of testers, resulting in low execution efficiency of the compiled xpath.
[0049] Features of this invention application:
[0050] 1. Click on the element to generate the Xapth without manual compilation;
[0051] 2. There is no need to learn and master xpath knowledge, and clicking on the element can automatically generate the xpath;
[0052] 3. After the requirements change, just click on the element to obtain the Xpath again;
[0053] 4. Among the multiple automatically generated XPaths, select the one with the highest execution efficiency, which can improve the execution efficiency and has nothing to do with the technical experience of testers.
[0054] Compared with the prior art - in the technical solution of the web list parsing method and system based on the XPath sequence, it is necessary to first establish the dictionary value of the tag element, then obtain the candidate paths of the list items in the web page, screen out the simplified XPath paths of the list items from the candidate paths, and generate an XPath by comparing according to the dictionary of the simplified XPath path and the label. The present invention only needs to insert a property on the clicked element, traverse and compare according to this inserted property, automatically generate multiple XPath values of the current element, compile and package the code, and install the plugin (the compiled installation data packet) in the browser, and automatically obtain the page element XPath through the mouse click operation to complete the recording of the web automation test case script. The operation method and implementation algorithm of the present invention are simple and intuitive. 1. Implement the automatic generation method of XPath through coding, compile and package it, install the self-developed plugin (the compiled package) in the browser, and automatically obtain the page element XPath through the mouse click operation to complete the recording of the web automation test case script; 2. It is not necessary to log in and authenticate to obtain the XPath of any page element in the browser; 3. The generated XPath can be directly copied; 4. The generated XPath can be directly exported in its entirety to a document. Through the above-mentioned automatic generation method of XPath, first, when performing web automation testing, there is no longer a need to master the relevant knowledge of XPath, eliminating the dependence of testers on XPath knowledge for writing scripts, enabling junior testers to perform web automation testing, thus lowering the threshold of web automation testing; second, when the business requirements change and the page changes, there is no need for testers to write again, and only need to automatically obtain the XPath again, which improves the efficiency of script compilation and reduces the maintenance cost of the script.
Claims
1. A method for automatically generating XPath, characterized in that It includes the following steps Step 1: Obtain and parse the DOM elements of the page. The acquisition and parsing of DOM elements are automatically completed by the browser. The belonging of the tags is reflected according to the hierarchical relationship of the tags, forming a tree structure. After parsing, operations are performed on each element; Step 2: When the mouse hovers over an element, it is highlighted. When the mouse moves away, the highlighting is cancelled. This is implemented using JS code. The mouse hover event onMouseOver and mouse leave event onMouseOut of JQuery are used to control the page style. The CSS style for highlighting is set when hovering, and the CSS style for cancelling highlighting is set when the mouse leaves; Step 3: When an element is clicked, it is highlighted and a display border is set. The currently clicked element is obtained through Jquery, a unique attribute and its value are set for the current element, and the CSS technology is combined to set the highlighting display and the border color style for the current clicked element; Step 4: For elements with a border, obtain the tag names of these elements, including input tags, a tags, and button tags. The tag names of the current elements are obtained through Jquery technology; Step 5: For elements with a border, obtain all the attributes and their values, as well as the parent node, child nodes, and sibling nodes within the tag. The specific attributes include class, href, title, and text. All the attributes and their values within the tag are obtained through the each loop of Jquery technology. The parent node, child nodes, previous sibling node, and next sibling node of the current element are obtained through parent(), children(), prev(), and next(); Step 6: Concatenate the XPath according to the attributes and their values; Step 7: Search in the html document according to the XPath concatenated in Step 6, and obtain the number X of the found results. If the search value X is equal to 1, then the current XPath is an XPath of the specified attribute of the current element, and the current XPath is generated; if the search value X is greater than 1, then the serial number is increased according to the quantity on the basis of the current XPath. Locate the element according to the XPath with the serial number, check whether the located element has the inserted attribute. If it has, generate the XPath. If not, continue to increment the XPath serial number by 1 until an element with the inserted attribute is found to generate the XPath; at the same time, the element with the inserted attribute is the element being searched for, and then the current XPath is the final XPath to be generated; Step 8: By analogy, multiple XPaths are generated through multiple attribute values of the clicked element.
2. The method for automatically generating XPath according to claim 1, wherein In Steps 3, 4, and 5, the border is red.
Citation Information
Patent Citations
Method and device for generating burying point identifier and burying point method
CN112883250A
Page test method and device, equipment and medium
CN113918460A