Automatic page testing method and device, electronic equipment and medium
By dividing the page to be tested into functional modules and mapping XPath paths and module names, the problem of unreadable positioning and operation of web page elements in the Selenium framework is solved, and the readability and maintainability of test cases are improved.
Patent Information
- Application Number
- CN202410794931.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-19
- Publication Date
- 2024-08-30
AI Technical Summary
Web automated test cases written in the existing Selenium framework have lost readability and maintainability, mainly because web page elements are represented by complex strings in the code, making element positioning and operations difficult to understand and maintain.
By dividing the page to be tested into functional modules, and mapping the XPath path of each module to its corresponding functional module name, the Selenium framework is used to test the page through the mapped XPath path and functional module name to achieve easy readability of the elements. and maintainability.
By matching the XPath path of the page element with the function module name, the code is simplified and the readability and maintainability of the test cases are improved. The page element can be found through the function module name and the XPath path of the element, which significantly improves the code. readability and maintainability.
Smart Images

Figure CN118567996A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of testing technology, and in particular to a method, device, electronic equipment and medium for automatic testing of a page. Background Art
[0002] Selenium is a web automation programming framework that supports mainstream languages such as Python, Java, and CSharp. Code written based on it can be combined with WebDriver to drive mainstream browsers such as Chrome, Firefox, and Edge for end-to-end web automation testing. Its principle is to simulate user operations on the browser to operate the browser.
[0003] The user's operation on the browser is a combination of click, double-click, right-click, drag, fill and other operations. A Web automation programming framework only needs to simulate these operations. However, before performing these operations, it needs to know which elements it wants to operate, which involves the positioning of elements. A web page is essentially a complex structured text. It arranges a series of renderable objects, which are rendered into graphics by the browser kernel. These objects are also called DOM (Document Object Model) objects. In programming behavior, operations and calls on DOM objects are based on strings. Then, the mainstream Web automation programming framework will locate web page elements based on DOM objects, and then perform specified operations.
[0004] However, for humans, web page elements that are visible to the naked eye and have clear meanings, such as a login button or an input text box, become a series of complex strings in the program code. This causes the web automation test cases written based on the Selenium framework to lose readability and maintainability.
[0005] How to write readable and maintainable code based on Selenium's Web automation testing framework has become an urgent problem to be solved. Summary of the invention
[0006] In view of this, it is necessary to provide a method, device, electronic device and medium for page automated testing to solve the problem of poor maintainability of automated testing framework codes.
[0007] In order to solve the above problem, the present invention provides a method for automatic page testing, comprising: Obtain a page to be tested, and divide the page to be tested into several functional modules; Determine the XPath paths corresponding to the page elements under the several functional modules; Mapping the XPath path with its corresponding functional module to obtain a mapped XPath path, wherein the mapped XPath path corresponds to the name of the corresponding functional module one by one; The page to be tested is tested based on the Selenium framework through the mapped XPath path and the function module name.
[0008] In a possible implementation, the step of obtaining a page to be tested and dividing the page to be tested into several functional modules includes: Obtaining a static page of the page to be tested; The page to be tested is divided into several functional modules based on the static page.
[0009] In a possible implementation, dividing the page to be tested into several functional modules based on the static page includes: The pages to be tested are divided into levels based on the static pages to obtain a plurality of functional modules.
[0010] In a possible implementation, the name of the page element under each of the plurality of function modules is different.
[0011] In a possible implementation, the unique identifier of the page element under each of the plurality of functional modules is different.
[0012] In a possible implementation, the testing of the page to be tested based on the Selenium framework through the mapped XPath path and the function module name includes: The Selenium framework determines to test the page to be tested through the function module name and the corresponding page element tag name.
[0013] In a possible implementation, several atomic operations on the pages to be tested are encapsulated into an independent operation based on the Selenium framework.
[0014] On the other hand, the present invention also provides a device for automatic page testing, comprising: A module division module is used to obtain a page to be tested and divide the page to be tested into several functional modules; A path determination module, used to determine the XPath paths corresponding to the page elements under the several functional modules; A mapping module, used for mapping the XPath path with its corresponding functional module to obtain a mapped XPath path, wherein the mapped XPath path has a one-to-one correspondence with the name of the corresponding functional module; The page testing module is used to test the page to be tested based on the Selenium framework through the mapped XPath path and the function module name.
[0015] On the other hand, the present invention also provides an electronic device, including a memory and a processor, wherein: The memory is used to store programs; The processor is coupled to the memory and is used to execute the program stored in the memory to implement the steps in the method for automatic page testing described in any one of the above implementations.
[0016] On the other hand, the present invention also provides a computer-readable storage medium for storing a computer-readable program or instruction, which, when executed by a processor, can implement the steps in a method for automatic page testing described in any of the above implementations.
[0017] The beneficial effect of the present invention is as follows: the present invention provides a method for automated testing of pages, firstly obtaining a page to be tested, and dividing the page to be tested into several functional modules, then determining the XPath paths corresponding to the page elements under the several functional modules, further mapping the XPath paths with the functional modules corresponding to them, obtaining the mapped XPath paths, the mapped XPath paths with the functional module names corresponding to them one by one, and finally testing the page to be tested based on the Selenium framework through the mapped XPath paths and the functional module names. The present invention makes the XPath paths of the page elements correspond to the functional module names, so that the page elements can be found through the functional module names and the XPath paths of the elements, thereby making the code more readable and maintainable. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Figure 1 A flow chart of a structure method of an embodiment of a method for page automation testing provided by the present invention; Figure 2 A schematic diagram of operating a browser based on a selenium framework in a method for automated page testing provided by the present invention; Figure 3 A schematic diagram of the structure of an embodiment of a device for automatic page testing provided by the present invention; Figure 4 A schematic structural diagram of an embodiment of an electronic device provided by the present invention. DETAILED DESCRIPTION
[0019] The preferred embodiments of the present invention will be described in detail below in conjunction with the accompanying drawings, wherein the accompanying drawings constitute a part of this application and are used together with the embodiments of the present invention to illustrate the principles of the present invention, but are not used to limit the scope of the present invention.
[0020] Before presenting the embodiments, the following terms are explained.
[0021] XPath (XML Path Language) was originally a language used to determine the location of a part of an XML document. Later, it was also widely used by web automated testing tools to find and locate elements in HTML.
[0022] Static web pages are actually existing web pages that are directly loaded into the browser and displayed without being compiled by the server. Static pages need to occupy a certain amount of web server space and cannot manage and publish updated pages independently. For example, common static pages include files with extensions of .html, .htm, .shtml, and .xml.
[0023] The present invention provides a method, device, electronic equipment and medium for automatic testing of a page, which are respectively described below.
[0024] Figure 1 A schematic diagram of an embodiment of a method for automatic page testing provided by the present invention is shown in FIG. Figure 1 As shown, a method for automatic testing of a page includes: S101, obtaining a page to be tested, and dividing the page to be tested into several functional modules; S102, determining XPath paths corresponding to page elements under the plurality of functional modules; S103, mapping the XPath path and its corresponding functional module to obtain a mapped XPath path, wherein the mapped XPath path and its corresponding functional module name correspond one to one; S104: Testing the page to be tested based on the Selenium framework through the mapped XPath path and the function module name.
[0025] Compared with the prior art, the method for page automated testing provided in this embodiment first obtains the page to be tested, and divides the page to be tested into several functional modules, then determines the XPath paths corresponding to the page elements under the several functional modules, further maps the XPath paths with the functional modules corresponding to them, obtains the mapped XPath paths, and the mapped XPath paths correspond to the names of the functional modules corresponding to them one by one, and finally tests the page to be tested based on the Selenium framework through the mapped XPath paths and the names of the functional modules. The present invention makes the XPath paths of the page elements correspond to the names of the functional modules, so that the page elements can be found through the names of the functional modules and the XPath paths of the elements, thereby making the code more readable and maintainable.
[0026] In some embodiments of the present invention, the step of obtaining a page to be tested and dividing the page to be tested into several functional modules includes: Obtaining a static page of the page to be tested; The page to be tested is divided into several functional modules based on the static page.
[0027] In some embodiments of the present invention, the step of dividing the page to be tested into several functional modules based on the static page includes: The pages to be tested are divided into levels based on the static pages to obtain a plurality of functional modules.
[0028] In some embodiments of the present invention, the names of the page elements under each of the several functional modules are different.
[0029] In some embodiments of the present invention, the unique identifier of the page element under each of the plurality of functional modules is different.
[0030] In a specific embodiment of the present invention, the static page of the page to be tested is divided into several modules. Specifically, taking the Baidu homepage as an example, the static page of the page to be tested is divided into: navigation bar, user area, function area, news area, and website information area.
[0031] Mapping the functions of all these areas together is obviously unreadable and difficult to maintain. In addition, one thing that needs special attention is that in the previous design, Xpath cannot be named with repeated natural language nouns in a text. The text generated by hierarchical mapping is as follows: [Navigation bar] hao123= / / [@id="s-top-left"] / a[2] Map = / / [@id="s-top-left"] / a[3] Post Bar = / / [@id="s-top-left"] / a[4] Video= / / [@id="s-top-left"] / a[5] Picture = / / [@id="s-top-left"] / a[6] Netdisk= / / [@id="s-top-left"] / a[7] Library = / / [@id="s-top-left"] / a[8] More = / / [@id="s-top-left"] / a[9] [User Area] City= / / [@id="s_mod_weather"] / div[1] / a / div / span[1] / span Temperature = / / [@id="s_mod_weather"] / div[1] / a / div / span[2] / span SETTINGS = / / [@id="s_mod_weather"] / div[1] / a / div / span[3] / span Avatar= / / [@id="s-top-username"] / span[1] / img Username= / / [@id="s-top-username"] / span[2] [Role Area] Input box = / / [@id="kw"] Baidu = / / [@id="su"] [News Area] My attention = / / [@id="s_menu_mine"] Recommended = / / [@id="s_menus_wrapper"] / span[2] [Website Information Area] About Baidu= / / [@id="bottom_layer"] / div / p[1] / a Must read before using Baidu = / / [@id="bottom_layer"] / div / p[3] / a Help Center= / / [@id="bottom_layer"] / div / p[4] / a Registration Number = / / [@id="bottom_layer"] / div / p[6] / a In a text, different XPaths on the same web page will be allowed to have the same name at the natural language level, as long as the elements with the same name are in different modules. In the case of single-level mapping: the behavior of clicking Baidu is described as click("Baidu"), so the word Baidu can only mark one element on the web page. But when performing secondary mapping, the behavior of clicking Baidu will be described by the optimizer as click("function area", "Baidu"). We assume that Baidu also appears in another area, and the click behavior can be described as click("another area", "Baidu"), then theoretically, there are as many web page elements as there are modules.
[0032] In some embodiments of the present invention, the testing of the page to be tested based on the Selenium framework through the mapped XPath path and the function module name includes: The Selenium framework determines to test the page to be tested through the function module name and the corresponding page element tag name.
[0033] In the specific embodiment of the present invention, the original click operation of selenium is as follows: browser.find_element_by_xpath(Xpath).click(), and the content of Xpath is generally difficult to read. If we want to click the search button of Baidu, then selenium should write as follows: browser.find_element_by_xpath(Xpath).click(' / / [@id="su"]'). Then, if we are in / / A simple mapping between [@id="su"] and Baidu can turn the code into browser.find_element_by_xpath(Xpath).click('Baidu'). For this, a mapper is created. Then we can think: Is the writing of browser.find_element_by_xpath(Xpath).click('Baidu') still too long and too complicated, with some unnecessary things? Therefore, a round of optimization is performed to turn the code into click('Baidu'). For this, an optimizer is created. As for why we know that the Xpath of the "Baidu" button is / / [@id="su"], for example, through the developer tools. All major browsers come with a developer tools module.
[0034] Take Google Chrome as an example. To get the Xpath of the big logo on the top of Baidu's homepage, you only need to visit Baidu and click the three dots in the upper right corner of the browser to open the area on the right that displays the source code of the webpage. Then click the small arrow in the upper left corner of the source code area to enter the selection mode. At this time, click the big logo on the webpage to highlight the corresponding code block in the source code area on the right. Then right-click this code and the option to copy Xpath will pop up. After selecting this option, the Xpath of the big logo will be retained on the clipboard. At this time, Ctrl+v can be used to paste the Xpath into any editor.
[0035] So, how to make Baidu equivalent to / / at the program level [@id="su"]? In other words, how to perform the mapping operation. Figure 2 The following is a schematic diagram of operating a browser based on the selenium framework. A mapper is essentially a text parsing code plus a text. The text can be used as input to the code and can be parsed by the code as long as it conforms to a specific format.
[0036] Like the picture above, the parsed text is: Baidu = / / [@id="su"] If we want to perform a complete search operation, we need to do four things: Open Baidu website Enter the content you want to search in the search box Click "Baidu" Open a search result Then the parsed text looks like: URL = http: / / 192.168.1.2:8080 Input box = / / [@id="kw"] Keyword=What is Selenium Baidu = / / [@id="su"] First search result = / / [@id="2"] / div / h3 / a / div / div / p / span / span The corresponding original program code is: browser.get("http: / / 192.168.1.2:8080") browser.find_element_by_xpath( / / [@id="kw"]).send_keys("What is selenium") browser.find_element_by_xpath(' / / [@id="su"]').click() browser.find_element_by_xpath(' / / [@i="2"] / div / h3 / a / div / div / p / span / span').click() After processing by the mapper, the program code can be written like this: change_url("URL") send_keys("input box","keywords") click("Baidu search") click("First search result") The two pieces of code are equivalent because, in addition to the translation of Xpath to natural language by the mapper, the optimizer encapsulates the Selenium native API and optimizes the code writing. That is, the Xpath path of the element in the page corresponds to the element name in the page one by one. Through the element name in the page, the element in the page can be determined, making the test case code easy to maintain.
[0037] In some embodiments of the present invention, several atomic operations on the pages to be tested are encapsulated into an independent operation based on the Selenium framework.
[0038] In a specific embodiment of the present invention, some functions may need to be used repeatedly in the test behavior. For example, login is not a single operation behavior on a web page. It is completed by opening the website, entering the user name and password, and clicking login. In some cases, the login behavior may be more complicated. All the behaviors of entering the website, entering the user name, entering the password, and clicking login are encapsulated together into an operation at the same level as click, and the login behavior is named login.
[0039] In order to better implement a method for automatic page testing in an embodiment of the present invention, based on a method for automatic page testing, correspondingly, Figure 3 As shown, an embodiment of the present invention further provides a device for automatic page testing, and a device 300 for automatic page testing includes: The module division module 301 is used to obtain a page to be tested and divide the page to be tested into several functional modules; A path determination module 302 is used to determine the XPath paths corresponding to the page elements under the plurality of functional modules; A mapping module 303 is used to map the XPath path with its corresponding functional module to obtain a mapped XPath path, wherein the mapped XPath path has a one-to-one correspondence with its corresponding functional module name; The page testing module 304 is used to test the page to be tested based on the Selenium framework through the mapped XPath path and the function module name.
[0040] The device 300 for automatic page testing provided in the above embodiment can implement the technical solution described in the above embodiment of a method for automatic page testing. The specific implementation principles of the above modules or units can refer to the corresponding contents in the above embodiment of a method for automatic page testing, which will not be repeated here.
[0041] like Figure 4 As shown, the present invention also provides an electronic device 400. The electronic device 400 includes a processor 401, a memory 402 and a display 403. Figure 4 Only some components of the electronic device 400 are shown, but it should be understood that it is not required to implement all of the components shown, and more or fewer components may be implemented instead.
[0042] In some embodiments, the processor 401 may be a central processing unit (CPU), a microprocessor or other data processing chip, used to run program codes or process data stored in the memory 402, such as a method for automatic page testing in the present invention.
[0043] In some embodiments, processor 401 may be a single server or a server group. The server group may be centralized or distributed. In some embodiments, processor 401 may be local or remote. In some embodiments, processor 401 may be implemented in a cloud platform. In some embodiments, the cloud platform may include a private cloud, a public cloud, a hybrid cloud, a community cloud, a distributed cloud, an internal cloud, a multi-cloud, etc., or any combination thereof.
[0044] In some embodiments, the memory 402 may be an internal storage unit of the electronic device 400, such as a hard disk or memory of the electronic device 400. In other embodiments, the memory 402 may also be an external storage device of the electronic device 400, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc., equipped on the electronic device 400.
[0045] Furthermore, the memory 402 may include both an internal storage unit of the electronic device 400 and an external storage device. The memory 402 is used to store application software installed in the electronic device 400 and various data.
[0046] In some embodiments, the display 403 may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an OLED (Organic Light-Emitting Diode) touch device, etc. The display 403 is used to display information on the electronic device 400 and to display a visual user interface. The components 401-403 of the electronic device 400 communicate with each other via a system bus.
[0047] In one embodiment, when the processor 401 executes a program for page automatic testing in the memory 402, the following steps may be implemented: Obtain a page to be tested, and divide the page to be tested into several functional modules; Determine the XPath paths corresponding to the page elements under the several functional modules; Mapping the XPath path with its corresponding functional module to obtain a mapped XPath path, wherein the mapped XPath path corresponds to the name of the corresponding functional module one by one; The page to be tested is tested based on the Selenium framework through the mapped XPath path and the function module name.
[0048] It should be understood that: when the processor 401 executes a page automatic test program in the memory 402, in addition to the above functions, other functions can also be implemented. For details, please refer to the description of the above corresponding method embodiments.
[0049] Furthermore, the embodiment of the present invention does not specifically limit the type of the electronic device 400 mentioned, and the electronic device 400 may be a portable electronic device such as a mobile phone, a tablet computer, a personal digital assistant (PDA), a wearable device, a laptop computer, etc. Exemplary embodiments of portable electronic devices include but are not limited to portable electronic devices equipped with IOS, Android, Microsoft or other operating systems. The above-mentioned portable electronic devices may also be other portable electronic devices, such as a laptop computer with a touch-sensitive surface (e.g., a touch panel). It should also be understood that in some other embodiments of the present invention, the electronic device 400 may not be a portable electronic device, but a desktop computer with a touch-sensitive surface (e.g., a touch panel).
[0050] Those skilled in the art will appreciate that all or part of the processes of the above-mentioned embodiments can be implemented by instructing related hardware through a computer program, and the program can be stored in a computer-readable storage medium, wherein the computer-readable storage medium is a disk, an optical disk, a read-only storage memory, or a random access memory, etc.
[0051] The above description is only a preferred specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily conceived by any technician familiar with the technical field within the technical scope disclosed by the present invention should be covered within the protection scope of the present invention.
Claims
1. A method for automated testing of pages. It is characterized in that include: Obtain a page to be tested, and divide the page to be tested into several functional modules; Determine the XPath paths corresponding to the page elements under the several functional modules; Mapping the XPath path with its corresponding functional module to obtain a mapped XPath path, wherein the mapped XPath path corresponds to the name of the corresponding functional module one by one; The page to be tested is tested based on the Selenium framework through the mapped XPath path and the function module name.
2. The method for automated page testing according to claim 1, It is characterized in that The step of obtaining a page to be tested and dividing the page to be tested into several functional modules includes: Obtaining a static page of the page to be tested; The page to be tested is divided into several functional modules based on the static page.
3. The method for automated page testing according to claim 2, It is characterized in that The step of dividing the page to be tested into several functional modules based on the static page includes: The pages to be tested are divided into levels based on the static pages to obtain a plurality of functional modules.
4. The method for automated page testing according to claim 1, It is characterized in that The names of the page elements under each of the several functional modules are different.
5. The method for automated page testing according to claim 1, It is characterized in that The unique identifiers of the page elements under each of the several functional modules are different.
6. The method for automated page testing according to claim 1, It is characterized in that The testing of the page to be tested based on the Selenium framework through the mapped XPath path and the function module name includes: The Selenium framework determines to test the page to be tested through the function module name and the corresponding page element tag name.
7. The method for automated page testing according to claim 1, It is characterized in that Also includes: Based on the Selenium framework, several atomic operations on the page to be tested are encapsulated into an independent operation.
8. A device for automatic testing of pages, It is characterized in that include: A module division module is used to obtain a page to be tested and divide the page to be tested into several functional modules; A path determination module, used to determine the XPath paths corresponding to the page elements under the several functional modules; A mapping module, used for mapping the XPath path with its corresponding functional module to obtain a mapped XPath path, wherein the mapped XPath path has a one-to-one correspondence with the name of the corresponding functional module; The page testing module is used to test the page to be tested based on the Selenium framework through the mapped XPath path and the function module name.
9. An electronic device, It is characterized in that comprising a memory and a processor, wherein: The memory is used to store programs; The processor is coupled to the memory and is used to execute the program stored in the memory to implement the steps in the method for automatic page testing as described in any one of claims 1 to 7.
10. A computer-readable storage medium, It is characterized in that Used to store computer-readable programs or instructions, which, when executed by a processor, can implement the steps in the method for automatic page testing as described in any one of claims 1 to 7.
Citation Information
Cited By
UI page test method and device, equipment, storage medium and product
CN118796709A
