Script parser and script parsing method
By providing a script parser for multi-threaded instance management, command parsing, code generation and execution, it solves the high technical threshold of existing browser automation testing methods and the burden brought by version iteration, realizes efficient and flexible automated testing, and provides detailed test reports.
Patent Information
- Application Number
- CN202411768920.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-04
- Publication Date
- 2025-03-28
AI Technical Summary
The existing browser automation testing methods have high technical barriers and learning and maintenance burdens brought by version iterations, and the script parser is poor in scalability and flexibility, so they cannot support complex automated testing scenarios.
It provides a script parser, including a multi-threaded instance management module, a command parsing module, a code generation module and an execution module. It can parse commands entered by users, generate Java Selenium code, and execute corresponding code, supporting parallel testing and automatic adaptation to Selenium version updates.
It lowers the technical barriers for testers, improves work efficiency, reduces the learning and maintenance burden caused by Selenium version iteration, supports complex automated testing scenarios, and provides detailed test reports.
Smart Images

Figure CN119718280A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer software, and in particular to a script parser and a script parsing method. Background Art
[0002] With the rapid development of the Internet, browser automation testing has become an important part of the software development and testing process. Currently, the most common automation testing framework on the market is Selenium, which supports the automation control of mainstream browsers, such as Chrome, Firefox, etc., and is widely used in the automation testing of various Web applications. However, there is a certain threshold for using the Selenium framework, especially for testers without a programming background. Operating Selenium usually requires certain knowledge of Java or other programming languages.
[0003] Therefore, the existing browser automation testing methods have the following deficiencies:
[0004] 1. High technical threshold: Selenium operation requires testers to write a lot of Java code, which requires testers not only to be proficient in Java programming, but also to have a deep understanding of Selenium's API. For example, testers need to know how to use Selenium's various APIs, such as locating elements, executing clicks, inputting text, waiting for page loading, managing browser cookies, etc. This is a huge challenge for test engineers who have no programming experience, hindering them from efficiently performing automated testing tasks.
[0005] 2. The learning burden brought by Selenium version iteration: As an open source project, Selenium is frequently updated. With each version update, the API may change or add new features. Testers need to keep up with these changes, learn the new version of the API, and adjust the existing test scripts. This greatly increases the learning cost of test engineers and the maintenance cost of developers. Especially when the project life cycle is long, compatibility issues caused by different versions of Selenium may cause test scripts to fail.
[0006] Although there are some tools on the market that can help testers generate Selenium scripts, such as recording and playback tools, these tools have poor scalability and flexibility and cannot support complex automated testing scenarios. At the same time, the maintenance and upgrade of such tools often rely on manual operations by users, cannot automatically adapt to Selenium version updates, and have low work efficiency. Summary of the invention
[0007] In view of the above-mentioned defects of the prior art, the technical problem to be solved by the present invention is that the script analyzer has poor extensibility and flexibility, cannot support complex automated testing scenarios, cannot automatically adapt to the update of Selenium versions, and has low work efficiency. The present invention provides a script parser and a script parsing method, a powerful, easy-to-use and highly extensible script parser system, which can greatly improve the work efficiency of testers and reduce the learning and maintenance burden caused by Selenium version iteration.
[0008] To achieve the above-mentioned object, the present invention provides a script parser, including a multi-threaded instance management module, a command parsing module, a code generation module and an execution module, wherein the command parsing module is used to parse the commands input by the user, the code generation module is used to convert the parsed commands into Java Selenium codes, and the execution module is used to execute the generated Java Selenium codes; the multi-threaded instance management module is used to create and manage multiple browser instances; wherein,
[0009] The command parsing module parses the commands entered by the user and identifies the actions, targets and parameters of the commands. The code generation module generates Java Selenium code based on the parsed commands, and then the execution module compiles and executes the corresponding Java Selenium code. Each browser instance operates through an independent thread in the multi-threaded instance management module, thereby achieving parallel testing.
[0010] Furthermore, the Java Selenium code generated by the code generation module includes Java Selenium code for operations such as clicking, inputting, and navigating.
[0011] Furthermore, the command parsing module supports a standardized command format input by a user, and each command includes four parameters: browser instance name webDriverName, operation action action, target or value targetOrValue, and additional parameter additionalParam.
[0012] Furthermore, the code generation module generates Java codes for Selenium WebDriver operations according to the parsed commands, including one or more of get, clickByXPath, clickByName, and sendKeysByName.
[0013] Furthermore, the command parsing module also includes a function for parsing a create command for creating a new browser instance.
[0014] Furthermore, the code generation module is configured to generate Java code for managing and storing multiple browser instances, and supports interaction with the browser via a Selenium WebDriver interface.
[0015] Furthermore, the code generation module also includes generating JavaSelenium code for collecting and adding browser cookies.
[0016] Furthermore, the execution module is configured to also process browser exceptions and provide a corresponding error handling mechanism.
[0017] In a preferred embodiment of the present invention, a script parsing method is provided, and the implementation of the command parsing module includes the following steps:
[0018] The user specifies the required operation by inputting a script command, and the script parser generates the corresponding Selenium code by parsing the input script command. Specifically, a. first parse the first parameter to see if it is create to identify whether it is an operation to create a browser instance;
[0019] b. For the create command, the script parser decomposes the create command into three parts: create action, browser type, and browser instance name. The instance will be stored in the browser instance management module;
[0020] c. For non-create commands, the parser decomposes them into four parts: browser instance name webDriverName, operation action, target or value targetOrValue, and additional parameter additionalParam; select the specific browser instance in the browser management module by identifying webDriverName, select the action to be executed by identifying action, and determine the parameters or target values to be added by identifying targetOrValue and additionalParam. For example: get command: Generate code for the browser to access the target page
[0021] driver.get(targetOrValue), click command: Generate code for clicking page elements
[0022] driver.findElement(By.xpath(targetOrValue)).click();
[0023] The Java Selenium code generated by the analysis controls the browser instance through the execution module to complete the browser automation operation specified by the user.
[0024] Furthermore, the format of non-create commands consists of four parameters, and its format is: subject, action, target or value, additional parameters; the create command is an initialization command, which does not require the browser instance name as the subject, but only requires three parameters, and its format is create action + browser type + browser instance name.
[0025] Technical Effects
[0026] The script parser and script parsing method provided by the present invention have the following technical effects:
[0027] 1. Simplify the automated testing process
[0028] Testers no longer need to write complex Java Selenium code, but can perform various automated testing operations through simple command line syntax, which greatly lowers the testing threshold and enables test engineers without a programming background to quickly get started and perform efficient automated testing.
[0029] 2. Support parallel automated testing
[0030] The present invention supports multiple browser instances running simultaneously, and each instance operates through an independent thread, which can effectively improve the test efficiency. Users can reduce the total time of test execution through parallel testing, which is particularly useful when large-scale regression testing or testing multiple browsers simultaneously is required.
[0031] 3. Reduce maintenance and learning costs
[0032] Since Selenium version updates usually introduce API changes, traditional test scripts need to be frequently adjusted. The present invention minimizes the impact of these changes on users by encapsulating Selenium API, and users do not need to learn and maintain new versions of Selenium, greatly reducing maintenance and learning costs.
[0033] 4. Enhance user session management capabilities
[0034] Managing Cookies is a common requirement in automated testing, especially in cross-session testing. Through the present invention, users can easily obtain and apply Cookies, ensuring that user sessions can remain consistent across multiple test steps, thereby improving the flexibility and applicability of automated testing.
[0035] 5. Detailed test report
[0036] The system automatically generates detailed test reports, including the execution results of each operation, error information, etc. Users can quickly understand the progress of the test, locate problems and optimize the test process through these reports. This greatly facilitates troubleshooting during the test process.
[0037] The concept, specific structure and technical effects of the present invention will be further described below in conjunction with the accompanying drawings to fully understand the purpose, characteristics and effects of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] Figure 1 It is a system framework diagram of a script parser of a preferred embodiment of the present invention;
[0039] Figure 2 It is a flowchart of a script parsing method according to a preferred embodiment of the present invention. DETAILED DESCRIPTION
[0040] In order to make the technical problems, technical solutions and beneficial effects to be solved by the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0041] In the following description, specific details such as specific internal procedures and techniques are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present invention. However, it should be clear to those skilled in the art that the present invention may be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present invention.
[0042] like Figure 1 As shown, an embodiment of the present invention provides a script parser, including a multi-threaded instance management module, a command parsing module, a code generation module and an execution module. The command parsing module is used to parse the commands input by the user, the code generation module is used to convert the parsed commands into Java Selenium codes, and the execution module is used to execute the generated Java Selenium codes; the multi-threaded instance management module is used to create and manage multiple browser instances; wherein,
[0043] The command parsing module parses the commands entered by the user and identifies the actions, targets and parameters of the commands. The code generation module generates Java Selenium code based on the parsed commands, and then the execution module compiles and executes the corresponding Java Selenium code. Each browser instance operates through an independent thread in the multi-threaded instance management module, thereby achieving parallel testing.
[0044] Furthermore, the Java Selenium code generated by the code generation module includes Java Selenium code for operations such as click, input, and navigation. The code generation module generates Java code for Selenium WebDriver operations based on the parsed commands, including one or more of get, clickByXPath, clickByName, and sendKeysByName. The code generation module is configured to generate Java code for managing and storing multiple browser instances, and supports interaction with the browser through the SeleniumWebDriver interface. The code generation module also includes generating Java Selenium code for collecting and adding browser cookies. The code generation module can also generate Java Selenium code for automated testing, including but not limited to unit testing, integration testing, and regression testing.
[0045] The command parsing module supports the standardized command format input by the user. Each command includes four parameters: browser instance name webDriverName, operation action, target or value targetOrValue, and additional parameter additionalParam. The command parsing module also includes a function for parsing the create command, which is used to create a new browser instance, including but not limited to Chrome and Firefox browsers. The command parsing module can support user-defined commands to expand the functionality of the script parser.
[0046] The execution module is configured to also handle browser exceptions and provide corresponding error handling mechanisms. The execution module can execute the generated Java Selenium code to implement browser automation operations, including accessing URLs, clicking elements, entering text, selecting drop-down menus, waiting for elements, collecting links, extracting text, writing files, etc. The execution module can also generate automated test reports and provide detailed test results and log information.
[0047] Specifically, the command parsing module is the core part of the script parser, which is used to parse the commands input by the user. The module receives the command input from the user and decomposes it into different components. The command format received by the module is fixed in the following order: webDriverName action targetOrValue additionalParam, where
[0048] webDriverName: indicates the name of the browser instance to be operated, such as aaadriver, bbbdriver.
[0049] action: indicates the specific operation that the user wants to perform, such as get (access URL), clickByXPath (click element according to XPath), sendKeysByName (input text), getcookies (get Cookies), etc.
[0050] targetOrValue: indicates the target or value of the operation, such as a URL, XPath, or element name.
[0051] additionalParam: used to provide additional parameters (such as input text) for certain operations.
[0052] The command parsing module generates Java Selenium code by passing these command parts to the code generation module.
[0053] Its workflow is as follows: by parsing the input script command, the parser decomposes the command into individual parts through the split method. First, parse whether the first parameter is create to identify whether it is an operation to create a browser instance. The format of non-create commands consists of four parameters, and its format is: subject, action, target or value, additional parameters; the create command is an initialization command, which does not require the browser instance name as the subject, but only requires three parameters, and its format is create action + browser type + browser instance name;
[0054] The parser decomposes the create command into three parts: create action, browser type, and browser instance name. The instance will be stored in the HashMap (HashMap<String,WebDriver> ) data structure;
[0055] For non-create commands, the parser decomposes them into four parts: browser instance name webDriverName, operation action, target or value targetOrValue, and additional parameter additionalParam; by identifying webDriverName, the specific browser instance in the browser management module is selected for calling, for example: zjhdriverget https: / / example.com, this command means accessing https: / / example.com in the browser instance zjhdriver. The command parsing module identifies whether there is a browser instance named zjhdriver in the storage collection by querying the key of HashMap.
[0056] Select the action to be executed by identifying action, and determine the parameters or target values to be added by identifying targetOrValue and additionalParam.
[0057] After parsing, the generated Java Selenium code is executed by the code generation module and the execution module to control the browser instance to complete the browser automation operation specified by the user.
[0058] Code generation module and execution module
[0059] The module generates and executes the corresponding Selenium code according to the parsed command. Through this module, users do not need to understand the specific API of Selenium, and the parser will automatically generate the correct Selenium operation for them.
[0060] Since this module can execute many methods based on the recognition results, for example, including but not limited to the following:
[0061] zjhdriver get https: / / example.com , the script parsing module recognizes that the subject is zjhdriver, the action is get, and the parameters are https: / / example.com , the code generation module generates code zjhdriver.get(" https: / / example.com ") to browse the target web page.
[0062] abcdriver sendkeys a helloworld. The script parsing module recognizes that the subject is abcdriver, the action is sendkeysbyname, the parameter is a, and the additional parameter is helloworld. The code generation module generates the code abcdriver.findElement(By.name("a")).sendKeys("helloworld") to operate the browser to fill in the specified content.
[0063] The multi-thread management module is used to create and manage multiple browser instances. Each browser instance is operated through an independent thread, thereby achieving parallel testing. This allows users to execute different commands on multiple browser instances at the same time, ensuring the efficiency of test tasks.
[0064] The system uses a HashMap<String,Thread> The data structure maintains the mapping relationship between browser instances and threads. The key of the collection is the browser instance name (such as zjhdriver), and the value is the thread object that controls the instance. When the user enters the create command to create a new instance, the module will simultaneously create a new thread and bind the thread to the browser instance name. Whenever a user performs an operation on a browser instance, the system will start a thread to handle the operation. First, check whether there is already a thread running. If there is no active thread, create a new thread to perform the operation.
[0065] The specific tasks executed by the thread are defined by the executeCommand method, which generates the corresponding Selenium operation instructions according to the operation type passed by the parsing module and runs them in the thread. In the case of multiple instances running in parallel, it is necessary to wait for the current thread to complete the operation before starting a new thread to prevent multiple conflicting commands from being issued to the same browser instance at the same time.
[0066] The reasons why the script parser is designed in the embodiment of the present invention are as follows:
[0067] 1. Lower the technical threshold and allow testers without programming background to use it: The present invention provides a script parser that can automatically convert simple user input commands (such as "open a web page", "click a button", etc.) into underlying Java Selenium code and execute it. Through this parser, test engineers no longer need to master the complex Java programming language, and only need to enter simple commands to implement complex automated testing operations. This greatly lowers the threshold for using Selenium, allowing more testers to participate in automated testing work.
[0068] 2. Leave the Selenium version iteration to the parser developer: The frequent iteration and API update of the Selenium framework often cause the existing test scripts to become invalid or need to be adjusted. The script parser of the present invention encapsulates the Selenium API inside the parser, and the developer of the parser maintains and updates the Selenium version. The test engineer does not need to worry about the changes of the underlying Selenium version. No matter how Selenium is updated, the parser will maintain the consistency of the interface and command syntax. Users can always use the same commands to perform operations without having to learn the changes of each new version.
[0069] The script parser in the embodiment of the present invention can be summarized into the following core functions:
[0070] 1. Command parsing and code generation
[0071] The core function of the script parser in the embodiment of the present invention is to parse the simple commands input by the user into Java Selenium code and execute it in the browser instance. The parser supports a variety of operation commands. Users do not need to directly write Java code, and can implement common browser operations such as navigation, clicking, inputting, obtaining and setting Cookies through simple commands.
[0072] 2. Support multiple browser instances and parallel testing
[0073] The script parser system in the embodiment of the present invention supports the creation of multiple browser instances at the same time, and allocates an independent thread to each instance for operation. The parser adopts a multi-thread management mechanism to ensure that the operations between multiple instances do not interfere with each other, realizes efficient parallel automated testing, and improves testing efficiency.
[0074] 3. Block version changes of Selenium API
[0075] Since the Selenium framework is frequently updated, each update may introduce API changes, resulting in the need to adjust existing test scripts. To solve this problem, the present invention encapsulates the Selenium API inside the parser so that users do not need to worry about changes in the underlying Selenium version when using it. Regardless of how Selenium is updated, users can always use the same set of command formats to perform operations, thereby effectively reducing maintenance costs.
[0076] 4.Cookies management function
[0077] The present invention also provides users with a simple Cookies management function. Through simple commands, users can obtain the Cookies of the current browser and save them to a specified file, or load Cookies from a file and apply them to a browser instance, which is convenient for maintaining user status in cross-session automated testing scenarios.
[0078] 5. Automatically generate test reports
[0079] The parser of the present invention can record the execution of each command, including successful and failed operations. After capturing the execution results and error information, the system will automatically generate a test report containing detailed operation records, which is convenient for users to view test results and trace problems. The report can be output in HTML format for later viewing and analysis.
[0080] like Figure 2 As shown, the specific operation process of the script parser includes the following steps:
[0081] 1. S101 determines the number of browser instances for the operation
[0082] The command parsing module receives the create command input by the user, such as "create chrome zjhdriver". The command parsing module parses out the browser type as chrome and the instance name as zjhdriver. The parsing result is passed to the browser instance management module, which calls the corresponding method to create a Chrome browser instance and maps it with the zjhdriver name. The multi-threading management module allocates an independent thread to the newly created browser instance to ensure that the instance can operate in parallel with other instances.
[0083] 2. S102 creates an instance and names each browser through the create command
[0084] When multiple create commands are entered, such as "create firefox mjydriver", the command parsing module parses the commands one by one and passes the browser type and instance name to the browser instance management module. The browser instance management module stores the mapping information of each instance in a HashMap to ensure that users can quickly access and operate the corresponding browser by instance name. The log module records the creation process and results of each browser instance to ensure that users can trace operations.
[0085] 3. S103 executes the get command to access the specified web page
[0086] Enter "zjhdriver get https: / / example.com". The command parsing module identifies zjhdriver as the target instance name, get as the operation type, and https: / / example.com as the target URL. The browser instance management module finds the browser instance named zjhdriver from the HashMap and passes the parsing result to the code generation module. The code generation module generates the corresponding Selenium instruction: driver.get("https: / / example.com"). The execution module loads and executes the generated Selenium instruction to control the browser to access the target web page.
[0087] 4.S104 Waiting for page elements to load
[0088] Input "zjhdriver waitbyxpath / / button[@id='submit']", the command parsing module parses the target instance name as zjhdriver, the operation type as waitbyxpath, and the target as / / button[@id='submit']. The code generation module generates the Selenium instruction for waiting for the button to load: wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(" / / button[@id='submit']"))). The execution module runs the above instruction to ensure that the page element is loaded before continuing the operation. If a timeout occurs during the waiting process, the log module records the error information and feeds back to the user.
[0089] 5. S105 determines whether it is necessary to obtain or add Cookies
[0090] Input "zjhdriver getcookies C:\\Cookies.txt" or "zjhdriver addcookiesC:\\Cookies.txt", the command parsing module parses the command and parses out the getcookies or addcookies operation. If it is getcookies, get the Cookies data of the current page from the browser instance and save it to the specified file path. If it is addcookies, load the Cookies data from the specified file and apply it to the current page.
[0091] 6.S106 performs the required operations such as Click, Select, SendKeys, etc.
[0092] Input "zjhdriver sendkeysbyname username,admin", the command parsing module parses the target instance name, operation type, target locator and additional parameters. The code generation module generates the Selenium operation instruction zjhdriver.findElement(By.name("username")).sendKeys("admin") based on the parsing result.
[0093] 7. S107 waits for each step of the operation element to load
[0094] After each operation is completed, the execution module will use Selenium's waiting mechanism to ensure that the relevant elements are loaded before continuing to the next operation.
[0095] 8.S108 collects links to specified pages, extracts data and saves it to a file
[0096] Input "zjhdriver collectlinks a,https: / / example.com collectedLinks", zjhdriver writetofile links2 C:\Users\30595\Desktop\tle.txt; the command parsing module parses the target instance name, operation type, link prefix, target list name or target file name. The code generation module generates Selenium instructions for collecting page links, stores the collected links into a list and saves them to the specified file.
[0097] 9.S109 exits the specified browser instance through the exit command
[0098] Enter "exit zjhdriver", the command parsing module identifies the instance name that needs to be exited and removes the instance from the HashMap, and calls the quit() method of Selenium to close the browser.
[0099] The preferred specific embodiments of the present invention are described in detail above. It should be understood that a person skilled in the art can make many modifications and changes based on the concept of the present invention without creative work. Therefore, any technical solution that can be obtained by a person skilled in the art through logical analysis, reasoning or limited experiments based on the concept of the present invention on the basis of the prior art should be within the scope of protection determined by the claims.
Claims
1. A script parser, characterized in that: It includes a multi-threaded instance management module, a command parsing module, a code generation module and an execution module. The command parsing module is used to parse the commands input by the user, the code generation module is used to convert the parsed commands into Java Selenium codes, and the execution module is used to execute the generated Java Selenium codes; the multi-threaded instance management module is used to create and manage multiple browser instances; wherein, The command parsing module parses the command input by the user and identifies the action, target and parameters of the command. The code generation module generates Java Selenium code according to the parsed command, and then the execution module compiles and executes the corresponding Java Selenium code. Each browser instance is operated through an independent thread in the multi-threaded instance management module, thereby realizing parallel testing.
2. A script parser as claimed in claim 1, characterized in that: The Java Selenium code generated by the code generation module includes Java Selenium codes for operations such as clicking, inputting, and navigating.
3. A script parser as claimed in claim 1, characterized in that: The command parsing module supports the standardized command format input by the user, and each command includes four parameters: browser instance name webDriverName, operation action action, target or value targetOrValue, and additional parameter additionalParam.
4. A script parser as claimed in claim 2, characterized in that: The code generation module generates Java code for Selenium WebDriver operation according to the parsed command, including one or more of get, clickByXPath, clickByName, and sendKeysByName.
5. A script parser as claimed in claim 1, characterized in that: The command parsing module also includes a command for parsing a create command for creating a new browser instance.
6. A script parser as claimed in claim 1, characterized in that: The code generation module is configured to generate Java code for managing and storing multiple browser instances, and supports interaction with the browser via a Selenium WebDriver interface.
7. A script parser as claimed in claim 6, characterized in that: The code generation module also includes generating Java Selenium code for collecting and adding browser cookies.
8. A script parser as claimed in claim 1, characterized in that: The execution module is configured to also process browser abnormalities and provide a corresponding error handling mechanism.
9. A script parsing method using a script parser according to any one of claims 1 to 8, characterized in that: The implementation of the command parsing module includes the following steps: The script parser generates the corresponding Selenium code by parsing the input script command, specifically including a. First, parse whether the first parameter is create to identify whether it is a browser creation operation; b. When the first parameter is create, the script parser decomposes the create command into three parts, namely, create action, browser type, and browser instance name, and the instance will be stored in the browser instance management module; c. When the first parameter is a non-create command, the script parser decomposes it into four parts: browser instance name webDriverName, operation action action, target or value targetOrValue, and additional parameter additionalParam; select the specific browser instance in the browser management module by identifying webDriverName, select the action to be executed by identifying action, and determine the parameters or target values to be added by identifying targetOrValue and additionalParam; The Java Selenium code generated by the analysis controls the browser instance through the execution module to complete the browser automation operation specified by the user.
10. A script parsing method as claimed in claim 9, characterized in that: The format of non-create commands consists of four parameters, whose format is: subject, action, target or value, additional parameters; The create command is an initialization command. It does not require a browser instance name as a subject. It only requires three parameters, and its format is create action + browser type + browser instance name.