Method, device and equipment for realizing playwright chain calling based on java and medium
By building the PlaywrightChain class and setting an exception capture mechanism, the smooth chain call of the Playwright Java library is implemented, which solves the problems of cumbersome code writing and difficult maintenance in the existing technology, and improves the efficiency and readability of web page automation operations.
Patent Information
- Application Number
- CN202411077758.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-07
- Publication Date
- 2024-12-06
AI Technical Summary
The existing Playwright Java library lacks a smooth chain calling mechanism, which makes the code written cumbersome and difficult to read and maintain. It is not convenient to use the Playwright library to automate web pages directly.
By building the PlaywrightChain class, splitting and encapsulating each action as a separate method, implementing flexible and configurable chain calls, and setting exception capture and handling mechanisms in the method to ensure that a single operation failure will not affect the entire calling process, and providing statusSave Method saves page status for recovery.
It improves the readability and development efficiency of the code, simplifies the use of the Playwright library, increases the writing efficiency and maintainability of automated test codes, and ensures the integrity and fault tolerance of chain calls.
Smart Images

Figure CN119089059A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of web page automation operation, in particular to a method, device, equipment and medium for realizing playwright chain calling based on Java. Background Art
[0002] With the widespread application of automated testing and web crawlers, the efficiency and maintainability of automated operations are increasingly required. Playwright is a powerful automation library that supports multiple browsers and provides great convenience for automated testing.
[0003] With the rapid development of Web applications, web page automated testing and operation are becoming more and more important.
[0004] Playwright is a powerful browser automation tool that supports multiple browsers and platforms. However, the existing Playwright Java library has some shortcomings when used, especially the lack of a smooth chain call mechanism, which makes the code cumbersome to write and difficult to read and maintain. Moreover, Java is a widely used programming language, so it is not convenient to use the Playwright library directly for web page automation. Summary of the invention
[0005] In view of the needs and shortcomings of current technological development, the present invention provides a method, device, equipment and medium for implementing Playwright chain calling based on Java. By constructing a chain calling class that encapsulates Playwright functions, Playwright API calling is made more concise and smooth, greatly improving the readability of the code and the development efficiency.
[0006] In the first aspect, the present invention provides a method for implementing a playwright chain call based on Java, and the technical solution adopted to solve the above technical problems is as follows:
[0007] A method for implementing Playwright chain call based on Java, which requires the introduction of Playwright Java library, splits and repackages actions, splits each action into a separate method and returns an object that can continue to execute a new action, thereby realizing flexible and configurable chain call;
[0008] The specific implementation process of the method is as follows:
[0009] S1. Create the PlaywrightChain class, which contains multiple methods.
[0010] S2, implement the method in the PlaywrightChain class described in step S1;
[0011] S3. Setting an exception capture and handling mechanism for the methods in the PlaywrightChain class described in step S1. When the current method successfully processes, the exception capture and handling mechanism will continue to pass the processed object to the next method. When the current method fails, the exception will be captured, a new object will be generated and passed to the next method, ensuring that the failure of a single operation will not affect the entire call process, thereby ensuring the integrity of the chain call.
[0012] S4. Implement the statusSave method in the PlaywrightChain class to save the status of the current page. If the page crashes in subsequent operations or needs to be restored to a specified state, use the previously saved state to restore the page.
[0013] Optionally, each method in the PlaywrightChain class implements a single function and returns an object of a uniform format.
[0014] Optionally, the PlaywrightChain class includes the launch method, click method, fill method, check method and statusSave method, among which:
[0015] The launch method is used to start the browser and open a new page to prepare the environment for subsequent operations;
[0016] The click method receives a selector as a parameter, which is used to locate the page element that needs to be clicked;
[0017] The fill method receives a selector to locate the input box and the text to be entered as parameters, which is used to fill the text in the input box or text area on the page;
[0018] The check method requires a selector to determine the element to be operated on, which is used to check the check box or radio button on the page;
[0019] The statusSave method is used to save the status of the current page. Its specific implementation depends on the specific application scenario and requirements so that it can be restored when the page crashes.
[0020] Optionally, step S2 specifically includes:
[0021] Each method calls the corresponding method provided by the Playwright library to implement specific operations and encapsulate the return results, returning an object instance that can continue to call any remaining method in the PlaywrightChain class to support chain calls.
[0022] In the second aspect, the present invention provides a device for implementing playwright chain call based on Java, and the technical solution adopted to solve the above technical problems is as follows:
[0023] A device for implementing playwright chain calling based on Java, the structure of which includes:
[0024] The class creation module is used to create the PlaywrightChain class containing multiple methods;
[0025] Method implementation module, used to implement methods in the PlaywrightChain class;
[0026] The exception setting module is used to set the exception capture and handling mechanism for the methods in the PlaywrightChain class;
[0027] The method processing module is used to capture and handle exceptions according to the mechanism. When the current method succeeds, the processed object is passed to the next method. When the current method fails, the module captures the exception, generates a new object and passes it to the next method. This ensures that the failure of a single operation will not affect the entire call process and ensures the integrity of the chain call.
[0028] The statusSave implementation module is used to implement the statusSave method in the PlaywrightChain class to save the status of the current page, so that when the page crashes in subsequent operations or needs to be restored to a specified state, the previously saved state can be used to restore the page.
[0029] Optionally, each method in the PlaywrightChain class involved implements a single function and returns an object of a uniform format.
[0030] Further optionally, the methods included in the PlaywrightChain class include a launch method, a click method, a fill method, a check method, and a statusSave method, among which:
[0031] The launch method is used to start the browser and open a new page to prepare the environment for subsequent operations;
[0032] The click method receives a selector as a parameter, which is used to locate the page element that needs to be clicked;
[0033] The fill method receives a selector to locate the input box and the text to be entered as parameters, which is used to fill the text in the input box or text area on the page;
[0034] The check method requires a selector to determine the element to be operated on, which is used to check the check box or radio button on the page;
[0035] The statusSave method is used to save the status of the current page. Its specific implementation depends on the specific application scenario and requirements so that it can be restored when the page crashes.
[0036] Optionally, the method implementation module involved implements the methods in the PlaywrightChain class, specifically including:
[0037] Each method in the PlaywrightChain class calls the corresponding method provided by the Playwright library to implement specific operations and encapsulate the return results, returning an object instance that can continue to call any remaining method in the PlaywrightChain class to support chain calls.
[0038] In a third aspect, the present invention provides an electronic device, comprising: a memory and at least one processor;
[0039] Wherein, the memory stores a computer program;
[0040] The at least one processor executes the computer program stored in the memory, so that the at least one processor executes the method for implementing playwright chain call based on Java as described in any one of claims 1 to 4.
[0041] In a fourth aspect, the present invention provides a computer-readable storage medium, wherein a computer program is stored in the computer-readable storage medium, and the computer program can be executed by a processor to implement the method for implementing playwright chain calls based on Java as described in any one of claims 1 to 4.
[0042] The method and device for implementing playwright chain calling based on Java of the present invention have the following beneficial effects compared with the prior art:
[0043] 1. The present invention introduces the Java library of Playwright, and by splitting and repackaging actions, each action is split into a separate method and returns an object that can continue to execute a new action, thereby realizing flexible and configurable chain calls;
[0044] 2. The present invention uses the Playwright library efficiently and concisely to perform web page automation operations in a Java environment, improves the readability of the code and increases the fault tolerance of the entire acquisition process, simplifies the use of the Playwright library, and improves the writing efficiency and maintainability of the automated test code. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] Attached Figure 1 is a flow chart of a method according to Embodiment 1 of the present invention;
[0046] Attached Figure 2 This is a module connection block diagram of the second embodiment of the present invention. DETAILED DESCRIPTION
[0047] In order to make the technical solution, the technical problem solved and the technical effect of the present invention more clearly understood, the technical solution of the present invention is clearly and completely described below in conjunction with specific embodiments.
[0048] Embodiment 1:
[0049] This embodiment proposes a method for implementing Playwright chain calling based on Java. The method needs to introduce the Playwright Java library. By splitting and repackaging the actions, each action is split into a separate method and returns an object that can continue to execute a new action, thereby realizing flexible and configurable chain calling.
[0050] Combined with Figure 1 The specific implementation process of the method is as follows:
[0051] S1. Create the PlaywrightChain class, which contains multiple methods.
[0052] Each method in the PlaywrightChain class implements a single function and returns an object of a uniform format.
[0053] The PlaywrightChain class contains the launch method, click method, fill method, check method and statusSave method, among which:
[0054] The launch method is used to start the browser and open a new page to prepare the environment for subsequent operations;
[0055] The click method receives a selector as a parameter, which is used to locate the page element that needs to be clicked;
[0056] The fill method receives a selector to locate the input box and the text to be entered as parameters, which is used to fill the text in the input box or text area on the page;
[0057] The check method requires a selector to determine the element to be operated on, which is used to check the check box or radio button on the page;
[0058] The statusSave method is used to save the status of the current page. Its specific implementation depends on the specific application scenario and requirements so that it can be restored when the page crashes.
[0059] S2. Implement the methods in the PlaywrightChain class described in step S1. Each method calls the corresponding method provided by the Playwright library to implement a specific operation and encapsulate the return result, and returns an object instance that can continue to call any remaining method in the PlaywrightChain class to support chain calls.
[0060] S3. Setting an exception capture and handling mechanism for the methods in the PlaywrightChain class described in step S1. When the current method successfully processes, the exception capture and handling mechanism will continue to pass the processed object to the next method. When the current method fails, the exception will be captured, a new object will be generated and passed to the next method, ensuring that the failure of a single operation will not affect the entire call process, thereby ensuring the integrity of the chain call.
[0061] S4. Implement the statusSave method in the PlaywrightChain class to save the status of the current page. If the page crashes in subsequent operations or needs to be restored to a specified state, use the previously saved state to restore the page.
[0062] Embodiment 2:
[0063] Combined with Figure 2 This embodiment proposes a device for implementing playwright chain call based on Java, and its structure includes:
[0064] The class creation module is used to create the PlaywrightChain class containing multiple methods;
[0065] Method implementation module, used to implement methods in the PlaywrightChain class;
[0066] The exception setting module is used to set the exception capture and handling mechanism for the methods in the PlaywrightChain class;
[0067] The method processing module is used to capture and handle exceptions according to the mechanism. When the current method succeeds, the processed object is passed to the next method. When the current method fails, the module captures the exception, generates a new object and passes it to the next method. This ensures that the failure of a single operation will not affect the entire call process and ensures the integrity of the chain call.
[0068] The statusSave implementation module is used to implement the statusSave method in the PlaywrightChain class to save the status of the current page, so that when the page crashes in subsequent operations or needs to be restored to a specified state, the previously saved state can be used to restore the page.
[0069] In this embodiment, each method in the PlaywrightChain class implements a single function and returns an object in a unified format.
[0070] The PlaywrightChain class contains the launch method, click method, fill method, check method and statusSave method, among which:
[0071] The launch method is used to start the browser and open a new page to prepare the environment for subsequent operations;
[0072] The click method receives a selector as a parameter, which is used to locate the page element that needs to be clicked;
[0073] The fill method receives a selector to locate the input box and the text to be entered as parameters, which is used to fill the text in the input box or text area on the page;
[0074] The check method requires a selector to determine the element to be operated on, which is used to check the check box or radio button on the page;
[0075] The statusSave method is used to save the status of the current page. Its specific implementation depends on the specific application scenario and requirements so that it can be restored when the page crashes.
[0076] In this embodiment, the method implementation module implements the methods in the PlaywrightChain class, specifically including:
[0077] Each method in the PlaywrightChain class calls the corresponding method provided by the Playwright library to implement specific operations and encapsulate the return results, returning an object instance that can continue to call any remaining method in the PlaywrightChain class to support chain calls.
[0078] Embodiment three:
[0079] This embodiment also provides an electronic device, including: a memory and a processor;
[0080] Wherein, the memory stores computer-executable instructions;
[0081] The processor executes the computer-executable instructions stored in the memory, so that the processor executes the method for implementing the playwright chain call based on Java in the first embodiment.
[0082] The processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The processor may be a microprocessor or any conventional processor, etc.
[0083] The memory can be used to store computer programs and / or modules. The processor realizes various functions of the electronic device by running or executing the computer programs and / or modules stored in the memory, and calling the data stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, at least one application required for a function, etc.; the data storage area can store data created according to the use of the terminal, etc. In addition, the memory can also include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (SMC), a secure digital (SD) card, a flash memory card, at least one disk storage period, a flash memory device, or other volatile solid-state storage devices.
[0084] Embodiment 4:
[0085] This embodiment also provides a computer-readable storage medium, which stores a plurality of instructions, which are loaded by a processor, so that the processor executes the method for implementing the playwright chain call based on Java in Embodiment 1. Specifically, a system or device equipped with a storage medium can be provided, on which the software program code for implementing the method described in Embodiment 1 is stored, and a computer (or CPU or MPU) of the system or device reads and executes the program code stored in the storage medium.
[0086] In this case, the program code itself read from the storage medium can realize the function of any one of the above-mentioned embodiments, and thus the program code and the storage medium storing the program code constitute a part of the present invention.
[0087] The storage medium embodiments for providing the program code include a floppy disk, a hard disk, a magneto-optical disk, an optical disk (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RYM, DVD-RW, DVD+RW), a magnetic tape, a non-volatile memory card, and a ROM. Alternatively, the program code can be downloaded from a server computer via a communication network.
[0088] In addition, it should be clear that the functions of any of the above embodiments can be implemented not only by executing the program code read by the computer, but also by enabling an operating system operating on the computer to complete part or all of the actual operations based on instructions from the program code.
[0089] In addition, it can be understood that the program code read from the storage medium is written to a memory provided in an expansion board inserted into the computer or written to a memory provided in an expansion unit connected to the computer, and then based on the instructions of the program code, a CPU installed on the expansion board or the expansion unit is enabled to perform part or all of the actual operations, thereby realizing the functions of any of the above-mentioned embodiments.
[0090] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for implementing playwright chain calls based on Java, It is characterized in that The method needs to introduce the playwright java library, by splitting and repackaging the actions, each action is split into a separate method and returns an object that can continue to execute the new action, thereby realizing flexible and configurable chain calls; The specific implementation process of the method is as follows: S1. Create the PlaywrightChain class, which contains multiple methods. S2, implement the method in the PlaywrightChain class described in step S1; S3. Setting an exception capture and handling mechanism for the methods in the PlaywrightChain class described in step S1. When the current method successfully processes, the exception capture and handling mechanism will continue to pass the processed object to the next method. When the current method fails, the exception will be captured, a new object will be generated and passed to the next method, ensuring that the failure of a single operation will not affect the entire call process, thereby ensuring the integrity of the chain call. S4. Implement the statusSave method in the PlaywrightChain class to save the status of the current page. If the page crashes in subsequent operations or needs to be restored to a specified state, use the previously saved state to restore the page.
2. A method for implementing playwright chain calling based on Java according to claim 1, It is characterized in that Each method in the PlaywrightChain class implements a single function and returns an object of a uniform format.
3. A method for implementing playwright chain calling based on Java according to claim 2, It is characterized in that The PlaywrightChain class contains the launch method, click method, fill method, check method and statusSave method, among which: The launch method is used to start the browser and open a new page to prepare the environment for subsequent operations; The click method receives a selector as a parameter, which is used to locate the page element that needs to be clicked; The fill method receives a selector to locate the input box and the text to be entered as parameters, which is used to fill the text in the input box or text area on the page; The check method requires a selector to determine the element to be operated on, which is used to check the check box or radio button on the page; The statusSave method is used to save the status of the current page. Its specific implementation depends on the specific application scenario and requirements so that it can be restored when the page crashes.
4. A method for implementing playwright chain calling based on Java according to claim 1, It is characterized in that The step S2 specifically includes: Each method calls the corresponding method provided by the Playwright library to implement specific operations and encapsulate the return results, returning an object instance that can continue to call any remaining method in the PlaywrightChain class to support chain calls.
5. A device for implementing playwright chain call based on java, It is characterized in that Its structure includes: The class creation module is used to create the PlaywrightChain class containing multiple methods; Method implementation module, used to implement methods in the PlaywrightChain class; The exception setting module is used to set the exception capture and handling mechanism for the methods in the PlaywrightChain class; The method processing module is used to capture and handle exceptions according to the mechanism. When the current method succeeds, the processed object is passed to the next method. When the current method fails, the module captures the exception, generates a new object and passes it to the next method. This ensures that the failure of a single operation will not affect the entire call process and ensures the integrity of the chain call. The statusSave implementation module is used to implement the statusSave method in the PlaywrightChain class to save the status of the current page, so that when the page crashes in subsequent operations or needs to be restored to a specified state, the previously saved state can be used to restore the page.
6. The device for implementing the playwright chain call based on Java according to claim 5, It is characterized in that Each method in the PlaywrightChain class implements a single function and returns an object in a unified format.
7. The device for implementing playwright chain call based on Java according to claim 6, It is characterized in that The PlaywrightChain class contains the launch method, click method, fill method, check method and statusSave method, among which: The launch method is used to start the browser and open a new page to prepare the environment for subsequent operations; The click method receives a selector as a parameter, which is used to locate the page element that needs to be clicked; The fill method receives a selector to locate the input box and the text to be entered as parameters, which is used to fill the text in the input box or text area on the page; The check method requires a selector to determine the element to be operated on, which is used to check the check box or radio button on the page; The statusSave method is used to save the status of the current page. Its specific implementation depends on the specific application scenario and requirements so that it can be restored when the page crashes.
8. The device for implementing playwright chain call based on Java according to claim 5, It is characterized in that The method implementation module implements the methods in the PlaywrightChain class, specifically including: Each method in the PlaywrightChain class calls the corresponding method provided by the Playwright library to implement specific operations and encapsulate the return results, returning an object instance that can continue to call any remaining method in the PlaywrightChain class to support chain calls.
9. An electronic device, It is characterized in that include: memory and at least one processor; Wherein, the memory stores a computer program; The at least one processor executes the computer program stored in the memory, so that the at least one processor executes the method for implementing playwright chain call based on Java as described in any one of claims 1 to 4.
10. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores a computer program, which can be executed by a processor to implement the method for implementing playwright chain calls based on Java as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Extensible Web automatic testing method, system and equipment and storage medium
CN115658496A
Page test method and device, equipment and medium
CN116069649A
Cited By
Graphical user interface generation method and device, electronic equipment and storage medium
CN120469687A
