XML Path Obtaining Method and Device for Document Element
By obtaining the unique attribute characteristics of the target element, using preset templates and component library paths, the problem of unreliable XML paths in web-side automation testing is solved, and the writing efficiency of test cases and the accuracy of tests are improved.
Patent Information
- Application Number
- CN202211126969.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-16
- Publication Date
- 2025-07-18
- Estimated Expiration
- 2042-09-16
AI Technical Summary
In web-side automation testing, it is difficult for the existing technology to obtain high reliability XML paths, resulting in test execution failure.
By obtaining the unique attribute characteristics of the target element, such as id, class, placeholder and text attributes, using preset templates and component library paths, a relatively reliable XML path is obtained.
Improve the reliability of XML paths, improve the efficiency of testers in writing automated test cases, and ensure the correct execution of automated tests on the web side.
Smart Images

Figure CN115481287B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular, to a method and device for obtaining an XML path of a document element. Background Art
[0002] In web - side automated testing, element positioning is usually performed through an XML path (XML Path, XPath). Currently, XML paths need to be captured relying on the developer tools built into the browser. This is not only cumbersome to operate, but also many of the captured XML paths are absolute paths. During actual testing, due to version iteration and modifications to the page by developers, absolute paths may cause the XML paths that could previously locate the correct elements to become invalid, resulting in the inability to locate document elements and causing test execution to fail.
[0003] Therefore, how to obtain a more reliable XML path for document elements has become a technical problem that urgently needs to be solved by those skilled in the art. Summary of the Invention
[0004] In view of the above problems, the present disclosure provides a method and device for obtaining an XML path of a document element that overcomes or at least partially solves the above problems. The technical solutions are as follows:
[0005] A method for obtaining an XML path of a document element includes:
[0006] Obtain a target element of a path to be captured in the document;
[0007] Determine the parent element of the target element in the document;
[0008] Determine whether the id attribute or class attribute of the parent element is unique. If so, use the unique id attribute or class attribute to obtain a first XML path of the target element. If not, obtain the text attribute of the target element;
[0009] Input the text attribute into a preset XPath universal template corresponding to a target component library to obtain a component library path;
[0010] Determine whether the component library path matches the target element. If so, determine the component library path as a second XML path of the target element. If not, determine whether the target element has a unique placeholder attribute;
[0011] In the case where it is determined that the target element has a unique placeholder attribute, use the placeholder attribute to obtain a third XML path of the target element;
[0012] In the case where it is determined that the target element does not have a unique said placeholder attribute, then determine whether the text attribute is unique. If so, use the text attribute to obtain the fourth XML path of the target element.
[0013] Optionally, the obtaining the first XML path of the target element by using the unique said id attribute or class attribute includes:
[0014] Based on the shortest path algorithm, use the unique said id attribute or class attribute, take the relative path of the parent element as the starting path, and obtain the first XML path of the target element.
[0015] Optionally, the determining whether the id attribute or class attribute of the parent element is unique includes:
[0016] Input the id attribute of the parent element into a preset id template to obtain an id path;
[0017] In the case where the id path uniquely matches the parent element, determine that the id attribute is unique;
[0018] Input the class attribute of the parent element into a preset class template to obtain a class path;
[0019] In the case where the class path uniquely matches the parent element, determine that the class attribute is unique.
[0020] Optionally, the determining whether the target element has a unique placeholder attribute includes:
[0021] Obtain the placeholder attribute of the target element;
[0022] Input the placeholder attribute into a preset placeholder template to obtain a placeholder path;
[0023] In the case where the placeholder path uniquely matches the target element, determine that the target element has a unique said placeholder attribute.
[0024] Optionally, the obtaining the third XML path of the target element by using the placeholder attribute includes:
[0025] Determine the placeholder path corresponding to the placeholder attribute as the third XML path of the target element.
[0026] Optionally, determining whether the text attribute is unique includes:
[0027] Input the text attribute into a preset text template to obtain a text path;
[0028] When the target element is uniquely matched in the text path, determine that the target element has the unique text attribute.
[0029] Optionally, obtaining a fourth XML path of the target element by using the text attribute includes:
[0030] Determine the text path corresponding to the text attribute as the fourth XML path of the target element.
[0031] Optionally, the method is applied to a browser plugin, which provides an XML path editing box. When an XML path is input in the XML path editing box, the browser plugin is used to mark and display the element pointed to by the XML path in the target document.
[0032] Optionally, obtaining the target element of the path to be crawled in the document includes:
[0033] Obtain a preset element crawling instruction of the document;
[0034] In response to the preset element crawling instruction, determine the target element of the path to be crawled in the document.
[0035] An XML path obtaining device for document elements includes: a first obtaining unit, a first determining unit, a second determining unit, a second obtaining unit, a third obtaining unit, a fourth obtaining unit, a third determining unit, a fourth determining unit, a fifth determining unit, a fifth obtaining unit, a sixth determining unit, and a sixth obtaining unit,
[0036] The first obtaining unit is configured to obtain the target element of the path to be crawled in the document;
[0037] The first determining unit is configured to determine the parent element of the target element in the document;
[0038] The second determining unit is configured to determine whether the id attribute or class attribute of the parent element is unique. If so, trigger the second obtaining unit; if not, trigger the third obtaining unit;
[0039] The second obtaining unit is configured to obtain a first XML path of the target element by using the unique id attribute or class attribute;
[0040] The third obtaining unit is configured to obtain the text attribute of the target element;
[0041] The fourth obtaining unit is configured to input the text attribute into a preset XPath universal template corresponding to the target component library to obtain a component library path;
[0042] The third determining unit is configured to determine whether the component library path matches the target element. If so, it triggers the fourth determining unit; if not, it triggers the fifth determining unit;
[0043] The fourth determining unit is configured to determine the component library path as the second XML path of the target element;
[0044] The fifth determining unit is configured to determine whether the target element has a unique placeholder attribute;
[0045] The fifth obtaining unit is configured to, when it is determined that the target element has a unique placeholder attribute, use the placeholder attribute to obtain the third XML path of the target element;
[0046] The sixth determining unit is configured to, when it is determined that the target element does not have a unique placeholder attribute, determine whether the text attribute is unique. If so, it triggers the sixth obtaining unit;
[0047] The sixth obtaining unit is configured to use the text attribute to obtain the fourth XML path of the target element.
[0048] By means of the above technical solution, a method and apparatus for obtaining the XML path of a document element provided by the present disclosure can obtain a target element of a path to be crawled in a document; determine a parent element of the target element in the document; determine whether the id attribute or class attribute of the parent element is unique. If so, use the unique id attribute or class attribute to obtain a first XML path of the target element. If not, obtain the text attribute of the target element; input the text attribute into a preset XPath universal template corresponding to the target component library to obtain a component library path; determine whether the component library path matches the target element. If so, determine the component library path as a second XML path of the target element. If not, determine whether the target element has a unique placeholder attribute; in the case where it is determined that the target element has a unique placeholder attribute, use the placeholder attribute to obtain a third XML path of the target element; in the case where it is determined that the target element does not have a unique placeholder attribute, determine whether the text attribute is unique. If so, use the text attribute to obtain a fourth XML path of the target element. The present disclosure obtains the relative XML path of the target element through the unique attribute characteristics of the target element, which can improve the reliability of the XML path of the document element, help improve the efficiency of testers in writing automated test cases, and thus ensure the correct execution of Web-side automated testing.
[0049] The above description is only an overview of the technical solution of the present disclosure. In order to be able to understand the technical means of the present disclosure more clearly, it can be implemented according to the content of the specification. And in order to make the above and other purposes, features and advantages of the present disclosure more obvious and understandable, the following specifically illustrates the specific embodiments of the present disclosure. Brief Description of the Drawings
[0050] By reading the following detailed description of the preferred embodiments, various other advantages and benefits will become clear to those of ordinary skill in the art. The drawings are only for the purpose of showing the preferred embodiments and are not considered to be a limitation of the present disclosure. And throughout the drawings, the same reference numerals are used to represent the same components. In the drawings:
[0051] Figure 1 A flowchart showing an embodiment of a method for obtaining the XML path of a document element provided by an embodiment of the present disclosure;
[0052] Figure 2 A schematic structural diagram showing an apparatus for obtaining the XML path of a document element provided by an embodiment of the present disclosure;
[0053] Figure 3 A schematic structural diagram showing an electronic device provided by an embodiment of the present disclosure. Detailed Description of the Preferred Embodiments
[0054] Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided so that the present disclosure can be more thoroughly understood and the scope of the present disclosure can be fully communicated to those skilled in the art.
[0055] In the automated testing of the Web - end UI layer, testers mainly locate the elements to be operated in the document through XML Path (XPath). Currently, the XML paths for locating elements need to be captured through browser developer tools. This is not only cumbersome to operate, but also most of the captured XML paths are absolute paths. In the case where the relevant document page is modified, these absolute paths may become invalid, resulting in element location failure, thus causing the execution failure of relevant test cases and increasing the difficulty of maintaining automated scripts.
[0056] As Figure 1 shown, a flowchart of an implementation manner of the method for obtaining the XML path of a document element provided by an embodiment of the present disclosure. The method for obtaining the XML path of a document element may include:
[0057] S01. Obtain a target element in the document whose path is to be captured.
[0058] Wherein, the document may be a page composed of Extensible Markup Language (XML).
[0059] Wherein, an element is the basic unit constituting the document. Common document elements may include text, image, hyperlink, animation, table, frame, and form.
[0060] Optionally, the method for obtaining the XML path of a document element provided by an embodiment of the present disclosure can be applied to a browser plugin. The browser plugin is provided with an XML path editing box. When an XML path is input in the XML path editing box, the browser plugin is used to mark and display the element pointed to by the XML path in the target document.
[0061] It can be understood that the browser plugin can be installed in the browser to facilitate using the browser plugin in the browser to execute the method for obtaining the XML path of a document element provided by an embodiment of the present disclosure. The browser plugin can provide a preset element capture instruction. Optionally, an embodiment of the present disclosure can obtain a preset element capture instruction of the document. In response to the preset element capture instruction, a target element whose path is to be captured is determined in the document.
[0062] Optionally, the preset element grabbing instruction may be an instruction generated after a pre-designed path grabbing shortcut key is triggered. Optionally, the path grabbing shortcut key may be a CTRL+SHIFT key combination. When the user holds down the CTRL+SHIFT key combination and places the mouse on any element of the document, the element can be identified as the target element, thereby starting to obtain the XML path of the target element. The disclosed embodiment can automatically copy the XML path to a related automated test case, thereby improving the editing efficiency of the test case.
[0063] Optionally, the browser plug-in can be a Chrome plug-in.
[0064] The disclosed embodiment can obtain the XML path of an element through the browser plug-in without entering the developer tool interface. The XML path edit box provided by the browser plug-in is used to display the XML path of the element currently being captured. The tester can manually modify the XML path generated by the system directly in the XML path edit box. If the manually modified XML path correctly matches the element, the element will be highlighted on the document.
[0065] Optionally, the embodiment of the present disclosure may use a yellow background to mark and display the element pointed to by the XML path in the target document.
[0066] The disclosed embodiment can help testers directly capture the XML path of elements on the page by designing a browser plug-in without entering the developer tool interface. At the same time, the most reliable XML path of the element can be obtained through a series of algorithms, which not only improves the efficiency of writing automated test cases, but also improves the maintainability of the test cases.
[0067] S02. Determine the parent element of the target element in the document.
[0068] Specifically, the embodiment of the present disclosure may start from the target element, search for the parent element level by level in the document, and obtain the id attribute and the class attribute of the parent element.
[0069] S03. Determine whether the id attribute or the class attribute of the parent element is unique. If yes, execute step S04; if no, execute step S05.
[0070] The id attribute may be a unique identifier of the parent element in the document.
[0071] Among them, the class attribute can be used to indicate the class of the parent element pointing to the style sheet.
[0072] Optionally, the embodiment of the present disclosure may input the id attribute of the parent element into a preset id template to obtain an id path. When the id path uniquely matches the parent element, the id attribute is determined to be unique.
[0073] Optionally, the preset id template can be "*[@id=\\\"xxx\\\"]", where "*" represents matching all elements in the document, and "xxx" represents filling in the id attribute.
[0074] In the embodiments of the present disclosure, the id path can be matched with each element in the document. When only this parent element is matched, it is determined that the id attribute of this parent element is unique.
[0075] Optionally, in the embodiments of the present disclosure, the class attribute of the parent element can be input into the preset class template to obtain the class path. When the class path uniquely matches the parent element, it is determined that the class attribute is unique.
[0076] Optionally, the preset class template can be "*[@class=\\\"xxx\\\"]", where "*" represents matching all elements in the document, and "xxx" represents filling in the class attribute.
[0077] In the embodiments of the present disclosure, the class path can be matched with each element in the document. When only this parent element is matched, it is determined that the class attribute of this parent element is unique.
[0078] S04. Obtain the first XML path of the target element by using the unique id attribute or class attribute.
[0079] Optionally, in the embodiments of the present disclosure, based on the shortest path algorithm, using the unique id attribute or class attribute, the relative path of the parent element is used as the starting path to obtain the first XML path of the target element. For example: the first XML path can be " / / div[@class=\\\"xxx\\\"] / / ..." or " / / div[@id=\\\"xxx\\\"] / / ...".
[0080] S05. Obtain the text attribute of the target element.
[0081] Among them, the text attribute is the text attribute of the target element. Optionally, the text attribute can include color attribute, text alignment attribute, character spacing attribute, text line height attribute, and text decoration attribute.
[0082] S06. Input the text attribute into the preset XPath universal template corresponding to the target component library to obtain the component library path.
[0083] Currently, there are many UI component libraries with fixed styles and class names. For example: Element-ui component library and Antd component library. During actual use, developers rarely modify the default properties of components in the component library. Therefore, in the embodiments of the present disclosure, a preset XPath universal template can be used, and by using the backward deduction method, the text attribute is input into the preset XPath universal template to obtain the component library path, and then it is determined whether the component library path matches the target element. For example: Select the path " / / *[@class="el-select-dropdown el-popper"and not(contains(@style,"display:none;"))] / / span[text()='Display']" in the Element-ui component library, remove "Display" from this path as the preset XPath universal template, input the text attribute into the original "Display" position in this preset XPath universal template, and then match it with the target element.
[0084] S07. Determine whether the component library path matches the target element. If it is, execute step S08; if not, execute step S09.
[0085] S08. Determine the component library path as the second XML path of the target element.
[0086] S09. Determine whether the target element has a unique placeholder attribute.
[0087] Among them, the placeholder attribute can be a short hint message that specifies the expected value of the input field for the target element. During actual use, the placeholder attribute is an attribute that developers often fill in, and the placeholder attribute is rarely modified.
[0088] Optionally, the embodiments of the present disclosure can obtain the placeholder attribute of the target element. Input the placeholder attribute into the preset placeholder template to obtain the placeholder path. In the case where the placeholder path uniquely matches the target element, it is determined that the target element has a unique placeholder attribute.
[0089] S10. In the case where it is determined that the target element has a unique placeholder attribute, use the placeholder attribute to obtain the third XML path of the target element.
[0090] Optionally, the embodiments of the present disclosure can determine the placeholder path corresponding to the placeholder attribute as the third XML path of the target element.
[0091] Optionally, the preset placeholder template can be " / / input[contains(@placeholder, \"xxx*\")]", where "xxx" represents the value filled in the placeholder attribute. For example, the third XML path can be " / / input[contains(@placeholder, \"Please enter the Chinese name\")]".
[0092] S11. When it is determined that the target element does not have a unique placeholder attribute, determine whether the text attribute is unique. If so, execute step S12.
[0093] Optionally, embodiments of the present disclosure can input the text attribute into a preset text template to obtain a text path. When the text path uniquely matches the target element, it is determined that the target element has a unique text attribute.
[0094] Optionally, when it is determined that the text attribute is not unique, a message indicating that the XML path acquisition fails is output.
[0095] S12. Use the text attribute to obtain a fourth XML path for the target element.
[0096] Optionally, embodiments of the present disclosure determine the text path corresponding to the text attribute as the fourth XML path of the target element.
[0097] Optionally, the fourth XML path can be " / / *[text() = 'xxx']", where "*" represents matching all elements in the document, and "xxx" represents the value filled in the text attribute. For example, the fourth XML path can be " / / *[text() = 'Click']".
[0098] A method for obtaining the XML path of a document element provided by the present disclosure can obtain a target element with a path to be crawled in a document; determine a parent element of the target element in the document; determine whether the id attribute or class attribute of the parent element is unique. If so, use the unique id attribute or class attribute to obtain the first XML path of the target element. If not, obtain the text attribute of the target element; input the text attribute into a preset XPath universal template corresponding to the target component library to obtain a component library path; determine whether the component library path matches the target element. If so, determine the component library path as the second XML path of the target element. If not, determine whether the target element has a unique placeholder attribute; in the case where it is determined that the target element has a unique placeholder attribute, use the placeholder attribute to obtain the third XML path of the target element; in the case where it is determined that the target element does not have a unique placeholder attribute, determine whether the text attribute is unique. If so, use the text attribute to obtain the fourth XML path of the target element. The present disclosure can obtain the relative XML path of the target element through the unique attribute characteristics of the target element, improve the reliability of the XML path of the document element, help improve the efficiency of testers in writing automated test cases, and thus ensure the correct execution of Web-end automated testing.
[0099] To facilitate understanding that the reliability of relative paths is higher than that of absolute paths, an example is given here: Suppose the same element can be located through the following two XML paths:
[0100] XML path 1: " / html / body / div[2] / form[2] / button[1]".
[0101] XML path 2: " / / button[@id='submit']".
[0102] Among them, XML path 1 is an absolute path, which locates the target element layer by layer from the root element. If any modification is made at any place on this path (such as adding a new element), then XML path 1 will become invalid, and this situation is very common in the actual development process. XML path 2 is a relative path, which does not depend on its upper-level elements and can be located through the id. In the actual development process, the id is hardly modified, so no matter how other parts of the document page are modified, the target element can still be located through XML path 2. In summary, the relative path is more reliable than the absolute path.
[0103] A method for obtaining the XML path of a document element provided by an embodiment of the present disclosure can directly capture and debug the XML path on a Web page without entering the developer tool, improving the operation efficiency. At the same time, the XML path calculated by the algorithm provided by the present disclosure is more reliable than the XML path captured by using the browser developer tool, not only improving the accuracy of XPath positioning, but also reducing the maintenance cost of automated test cases.
[0104] Although the operations are depicted in a particular order, this should not be construed as requiring that the operations be performed in the particular order shown or in sequential order. In certain circumstances, multitasking and parallel processing may be advantageous.
[0105] It should be understood that the various steps recited in the method embodiments of the present disclosure may be executed in a different order and / or in parallel. In addition, the method embodiments may include additional steps and / or omit executing the steps shown. The scope of the present disclosure is not limited in this regard.
[0106] Corresponding to the above method embodiment, an apparatus for obtaining the XML path of a document element provided by an embodiment of the present disclosure has a structure as Figure 2 shown, and may include: a first obtaining unit 01, a first determining unit 02, a second determining unit 03, a second obtaining unit 04, a third obtaining unit 05, a fourth obtaining unit 06, a third determining unit 07, a fourth determining unit 08, a fifth determining unit 09, a fifth obtaining unit 10, a sixth determining unit 11, and a sixth obtaining unit 12.
[0107] The first obtaining unit 01 is configured to obtain a target element of a path to be captured in a document.
[0108] The first determining unit 02 is configured to determine a parent element of the target element in the document.
[0109] The second determining unit 03 is configured to determine whether the id attribute or class attribute of the parent element is unique. If so, it triggers the second obtaining unit 04. If not, it triggers the third obtaining unit 05.
[0110] The second obtaining unit 04 is configured to obtain a first XML path of the target element by using the unique id attribute or class attribute.
[0111] The third obtaining unit 05 is configured to obtain the text attribute of the target element.
[0112] The fourth obtaining unit 06 is configured to input the text attribute into a preset XPath universal template corresponding to the target component library to obtain a component library path.
[0113] The third determination unit 07 is configured to determine whether the component library path matches the target element. If so, it triggers the fourth determination unit 08. If not, it triggers the fifth determination unit 09.
[0114] The fourth determination unit 08 is configured to determine the component library path as the second XML path of the target element.
[0115] The fifth determination unit 09 is configured to determine whether the target element has a unique placeholder attribute.
[0116] The fifth acquisition unit 10 is configured to, when it is determined that the target element has a unique placeholder attribute, use the placeholder attribute to obtain the third XML path of the target element.
[0117] The sixth determination unit 11 is configured to, when it is determined that the target element does not have a unique placeholder attribute, determine whether the text attribute is unique. If so, it triggers the sixth acquisition unit 12.
[0118] The sixth acquisition unit 12 is configured to use the text attribute to obtain the fourth XML path of the target element.
[0119] Optionally, the second acquisition unit 04 is specifically configured to, based on the shortest path algorithm, use the unique id attribute or class attribute, take the relative path of the parent element as the starting path, and obtain the first XML path of the target element.
[0120] Optionally, the second determination unit 03 is specifically configured to input the id attribute of the parent element into a preset id template to obtain an id path; when the id path uniquely matches the parent element, determine that the id attribute is unique.
[0121] Optionally, the second determination unit 03 is specifically configured to input the class attribute of the parent element into a preset class template to obtain a class path.
[0122] When the class path uniquely matches the parent element, determine that the class attribute is unique.
[0123] Optionally, the fifth determination unit 09 is specifically configured to obtain the placeholder attribute of the target element; input the placeholder attribute into a preset placeholder template to obtain a placeholder path; when the placeholder path uniquely matches the target element, determine that the target element has a unique placeholder attribute.
[0124] Optionally, the fifth obtaining unit 10 is specifically configured to determine the placeholder path corresponding to the placeholder attribute as the third XML path of the target element.
[0125] Optionally, the sixth determining unit 11 is specifically configured to input the text attribute into a preset text template to obtain a text path; when the text path uniquely matches the target element, it is determined that the target element has a unique text attribute.
[0126] Optionally, the sixth obtaining unit 12 is specifically configured to determine the text path corresponding to the text attribute as the fourth XML path of the target element.
[0127] Optionally, the XML path obtaining device for document elements is applied to a browser plug-in. The browser plug-in is provided with an XML path editing box, and is configured to, when an XML path is input in the XML path editing box, mark and display the element pointed to by the XML path in the target document.
[0128] Optionally, the first obtaining unit 01 is specifically configured to obtain a preset element scraping instruction of the document; in response to the preset element scraping instruction, determine a target element of the path to be scraped in the document.
[0129] An XML path obtaining device for document elements provided by the present disclosure can obtain a target element of a path to be scraped in a document; determine a parent element of the target element in the document; determine whether the id attribute or class attribute of the parent element is unique. If so, use the unique id attribute or class attribute to obtain the first XML path of the target element. If not, obtain the text attribute of the target element; input the text attribute into a preset XPath universal template corresponding to the target component library to obtain a component library path; determine whether the component library path matches the target element. If so, determine the component library path as the second XML path of the target element. If not, determine whether the target element has a unique placeholder attribute; in the case where it is determined that the target element has a unique placeholder attribute, use the placeholder attribute to obtain the third XML path of the target element; in the case where it is determined that the target element does not have a unique placeholder attribute, determine whether the text attribute is unique. If so, use the text attribute to obtain the fourth XML path corresponding to the target element. The present disclosure can obtain the relative XML path of the target element through the unique attribute feature of the target element, improve the reliability of the XML path of the document element, help improve the efficiency of testers in writing automated test cases, and thus ensure the correct execution of Web-side automated testing.
[0130] Regarding the device in the above embodiments, the specific manner in which each module performs operations has been described in detail in the embodiments related to the method, and will not be elaborated here.
[0131] The XML path acquisition device for the document element includes a processor and a memory. The above first acquisition unit 01, first determination unit 02, second determination unit 03, second acquisition unit 04, third acquisition unit 05, fourth acquisition unit 06, third determination unit 07, fourth determination unit 08, fifth determination unit 09, fifth acquisition unit 10, sixth determination unit 11, and sixth acquisition unit 12 are all stored in the memory as program units, and the processor executes the above program units stored in the memory to implement corresponding functions.
[0132] The processor contains a kernel, and the kernel retrieves the corresponding program unit from the memory. One or more kernels can be set. By adjusting the kernel parameters, the XML path of the target element can be obtained through the unique attribute characteristics of the target element, improving the reliability of the XML path of the document element, helping to improve the efficiency of testers in writing automated test cases, and thus ensuring the correct execution of web - side automated testing.
[0133] The embodiments of the present disclosure provide a computer - readable storage medium, on which a program is stored. When the program is executed by a processor, it implements the XML path acquisition method for the document element.
[0134] The embodiments of the present disclosure provide a processor, and the processor is used to run a program. When the program runs, it executes the XML path acquisition method for the document element.
[0135] As Figure 3 shown, the embodiments of the present disclosure provide an electronic device 1000. The electronic device 1000 includes at least one processor 1001, at least one memory 1002 connected to the processor 1001, and a bus 1003. Among them, the processor 1001 and the memory 1002 communicate with each other through the bus 1003. The processor 1001 is used to call program instructions in the memory 1002 to execute the above - mentioned XML path acquisition method for the document element. The electronic device herein can be a server, a PC, a PAD, a mobile phone, etc.
[0136] The present disclosure also provides a computer program product, which is suitable for executing a program initialized with the steps of the XML path acquisition method for the document element when executed on an electronic device.
[0137] The present disclosure is described with reference to the flowcharts and / or block diagrams of methods, apparatuses, electronic devices (systems), and computer program products according to embodiments of the present disclosure. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, and combinations of flows and / or blocks in the flowcharts and / or block diagrams can be implemented by computer program instructions. These computer program instructions can be provided to the processors of general purpose computers, special purpose computers, embedded processors, or other programmable devices to generate a machine, such that the instructions executed by the processors of the computer or other programmable devices produce means for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 or in multiple blocks or multiple blocks.
[0138] In a typical configuration, an electronic device includes one or more processors (CPUs), a memory, and a bus. The electronic device may also include an input / output interface, a network interface, etc.
[0139] The memory may include non-permanent memory in the form of computer-readable media, random access memory (RAM) and / or non-volatile memory such as read-only memory (ROM) or flash memory (flash RAM), and the memory includes at least one storage chip. The memory is an example of computer-readable media.
[0140] Computer-readable media includes permanent and non-permanent, removable and non-removable media that can store information by any method or technology. The information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission media that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transitory computer-readable media such as modulated data signals and carrier waves.
[0141] In the description of the present disclosure, it should be understood that if terms such as "upper", "lower", "front", "rear", "left" and "right" are used to indicate the orientation or positional relationship, they are based on the orientation or positional relationship shown in the drawings. This is only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the indicated position or element must have a specific orientation, be constructed and operated in a specific orientation. Therefore, it should not be construed as a limitation of the present disclosure.
[0142] It should be noted that in this document, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. It should also be noted that the term "comprising", "including" or any other variation thereof is intended to cover non-exclusive inclusion, so that a process, method, commodity or device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or elements inherent to such process, method, commodity or device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the presence of additional identical elements in the process, method, commodity or device comprising the element.
[0143] Those skilled in the art should understand that the embodiments of the present disclosure can be provided as a method, a system or a computer program product. Therefore, the present disclosure can take the form of a complete hardware embodiment, a complete software embodiment or an embodiment combining software and hardware aspects. Moreover, the present disclosure can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0144] The above are only the embodiments of the present disclosure and are not used to limit the present disclosure. For those skilled in the art, various changes and modifications can be made to the present disclosure. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present disclosure shall be included within the scope of the claims of the present disclosure.
Claims
1. A method for obtaining the XML path of a document element, characterized in that, Including: Obtain the target element of the path to be crawled in the document; Determine the parent element of the target element in the document; Determine whether the id attribute or class attribute of the parent element is unique. If so, use the unique id attribute or class attribute to obtain the first XML path of the target element. If not, obtain the text attribute of the target element; Input the text attribute into a preset XPath universal template corresponding to the target component library to obtain the component library path; Determine whether the component library path matches the target element. If so, determine the component library path as the second XML path of the target element. If not, determine whether the target element has a unique placeholder attribute; In the case where it is determined that the target element has a unique placeholder attribute, use the placeholder attribute to obtain the third XML path of the target element; In the case where it is determined that the target element does not have a unique placeholder attribute, determine whether the text attribute is unique. If so, use the text attribute to obtain the fourth XML path of the target element.
2. The method according to claim 1, wherein The step of using the unique id attribute or class attribute to obtain the first XML path of the target element includes: Based on the shortest path algorithm, use the unique id attribute or class attribute, and use the relative path of the parent element as the starting path to obtain the first XML path of the target element.
3. The method according to claim 1, wherein The step of determining whether the id attribute or class attribute of the parent element is unique includes: Input the id attribute of the parent element into a preset id template to obtain the id path; In the case where the id path uniquely matches the parent element, determine that the id attribute is unique; Input the class attribute of the parent element into a preset class template to obtain the class path; In the case where the class path uniquely matches the parent element, determine that the class attribute is unique.
4. The method according to claim 1, wherein The step of determining whether the target element has a unique placeholder attribute includes: Obtain the placeholder attribute of the target element; Input the placeholder attribute into a preset placeholder template to obtain the placeholder path; In the case where the placeholder path uniquely matches the target element, determine that the target element has a unique placeholder attribute.
5. The method according to claim 4, wherein The step of using the placeholder attribute to obtain the third XML path of the target element includes: Determine the placeholder path corresponding to the placeholder attribute as the third XML path of the target element.
6. The method according to claim 1, characterized in that, The step of determining whether the text attribute is unique includes: Input the text attribute into a preset text template to obtain a text path; When the target element is uniquely matched to the text path, determine that the target element has the unique text attribute.
7. The method according to claim 6, characterized in that, The obtaining the fourth XML path of the target element by using the text attribute includes: Determine the text path corresponding to the text attribute as the fourth XML path of the target element.
8. The method according to claim 1, wherein The method is applied to a browser plugin that provides an XML path editing box. The browser plugin is used to mark and display the element pointed to by the XML path in the document when an XML path is input in the XML path editing box.
9. The method according to claim 8, wherein The obtaining the target element of the path to be crawled in the document includes: Obtain a preset element crawling instruction for the document; In response to the preset element crawling instruction, determine the target element of the path to be crawled in the document.
10. An XML path acquisition device for a document element, characterized in that, including: A first obtaining unit, a first determining unit, a second determining unit, a second obtaining unit, a third obtaining unit, a fourth obtaining unit, a third determining unit, a fourth determining unit, a fifth determining unit, a fifth obtaining unit, a sixth determining unit, and a sixth obtaining unit, The first obtaining unit is used to obtain the target element of the path to be crawled in the document; The first determining unit is used to determine the parent element of the target element in the document; The second determining unit is used to determine whether the id attribute or class attribute of the parent element is unique. If so, trigger the second obtaining unit; if not, trigger the third obtaining unit; The second obtaining unit is used to obtain the first XML path of the target element by using the unique id attribute or class attribute; The third obtaining unit is used to obtain the text attribute of the target element; The fourth obtaining unit is used to input the text attribute into a preset XPath universal template corresponding to a target component library to obtain a component library path; The third determining unit is used to determine whether the component library path matches the target element. If so, trigger the fourth determining unit; if not, trigger the fifth determining unit; The fourth determining unit is used to determine the component library path as the second XML path of the target element; The fifth determining unit is used to determine whether the target element has a unique placeholder attribute; The fifth obtaining unit is used to obtain the third XML path of the target element by using the placeholder attribute when it is determined that the target element has the unique placeholder attribute; The sixth determining unit is used to determine whether the text attribute is unique when it is determined that the target element does not have the unique placeholder attribute. If so, trigger the sixth obtaining unit; The sixth obtaining unit is used to obtain the fourth XML path of the target element by using the text attribute.
Citation Information
Patent Citations
Application page traversal detection method, device and equipment, and readable storage medium
CN112115051A
Web element attribute feature-based positioning method
CN113177168A