CN-121979791-A - Automatic test code generation method and device and electronic equipment
Abstract
The application discloses a method and a device for generating an automatic test code and electronic equipment, and belongs to the technical field of software testing. The method comprises the steps of converting a test case described by natural language into formatted data by utilizing a target large model, opening a browser through a target automation test frame, traversing the page information list, accessing a target page indicated by current page information by using the browser, determining the target locator based on a first verification result of an original locator in each item of target element information in the current page information, splicing an operation method in the target locator and the target element information into an operation command, operating on the target page according to the operation command, writing the operation command into a script file when the operation is successful, and generating an automation test code based on the script file and the test case by utilizing the target large model after traversing. The application improves the writing efficiency of the automatic test codes.
Inventors
- ZHANG PEI
- PU LING
Assignees
- 浙江零跑科技股份有限公司
Dates
- Publication Date
- 20260505
- Application Date
- 20260109
Claims (10)
- 1. A method for generating automated test code, comprising: Converting a test case described by using natural language into formatted data by using a target large model, wherein the formatted data comprises a page information list, each piece of page information comprises at least one piece of element information, and each piece of element information comprises an original locator and an operation method; Opening a browser through a target automation test framework, traversing the page information list, and accessing a target page indicated by current page information by using the browser; For each item of target element information in the current page information, determining a target locator based on a first verification result of the original locator in the target element information, splicing the target locator and the operation method in the target element information into an operation command, operating on the target page according to the operation command, and writing the operation command into a script file when the operation is successful; and after the page information list is traversed, generating an automatic test code based on the script file and the test case by using the target large model.
- 2. The method of generating as recited in claim 1, wherein the first check result is that a unique element is located on the target page using the original locator; determining a target locator based on a first verification result of the original locator in the target element information, including: determining the original locator as the target locator.
- 3. The method of generating according to claim 1, wherein the first check result is that a plurality of elements are located on the target page using the original locator; determining a target locator based on a first verification result of the original locator in the target element information, including: sequentially carrying out validity verification on the plurality of elements; And under the condition that only one element in the plurality of elements passes the validity check, adjusting the original locator in the target element information based on the characteristics of the element passing the validity check to obtain the target locator.
- 4. A method of generating as claimed in claim 3, wherein each of said element information further includes an element description and a corresponding test step in said test case; determining a target locator based on a first verification result of the original locator in the target element information, further comprising: outerHTML, when a plurality of candidate elements passing the validity check exist in the plurality of elements, acquiring the plurality of candidate elements; Determining, with the target large model, a target outerHTML in each of the outerHTML based on the element descriptions and the testing steps in each of the outerHTML and the target element information, and generating a first locator based on the target outerHTML; the target locator is determined based on a second check result for the first locator.
- 5. The method of generating of claim 4, wherein determining the target locator based on the second calibration result for the first locator comprises: And if the second check result is that the first locator is not located to the element on the target page, returning the first locator which is located to the failure to the target large model, so that the target large model regenerates the locator until a second locator which is generated by the target large model and can be located to the element is obtained, and determining the target locator based on the second locator.
- 6. The method according to claim 1, wherein each element information further comprises an element description and a corresponding test step in the test case, and the first check result is that the original locator is used for not locating the element on the target page; determining a target locator based on a first verification result of the original locator in the target element information, including: Generating a third locator based on the original locator, the element description and the testing step in the target element information by using the target large model; And determining the target locator based on a third verification result of the third locator.
- 7. The method of generating according to claim 1, wherein the converting the test case described in natural language into the formatted data using the target large model includes: The method comprises the steps of inputting a first prompt word into the target large model, enabling the target large model to convert the test case into formatted data under the guidance of the first prompt word, wherein the first prompt word is used for indicating that the test case is split according to pages, generating operation steps based on locators supported by a target automation test framework, and comprises the test case, locator rules, page and navigation rules, element step rules, output formats and output examples.
- 8. The method of generating of claim 1, wherein generating automated test code based on the script file and the test case using the target large model comprises: And inputting a second prompt word into the target large model, so that the target large model generates the automatic test code based on the operation commands and the test cases in the script file under the guidance of the second prompt word, wherein the second prompt word comprises the operation commands, the test cases, the code requirements and the output requirements.
- 9. An automated test code generating apparatus, comprising: A conversion unit configured to convert a test case described using a natural language into formatted data using a target large model, the formatted data including a list of page information, each piece of page information including at least one piece of element information, each piece of element information including an original locator and an operation method; The access unit is configured to open a browser through the target automation test framework, traverse the page information list and access a target page indicated by current page information by using the browser; The processing unit is configured to determine a target locator based on a first verification result of the original locator in the target element information for each item of target element information in the current page information, splice the operation methods in the target locator and the target element information into an operation command, operate according to the operation command on the target page, and write the operation command into a script file when the operation is successful; and the code generating unit is configured to generate automatic test codes based on the script file and the test cases by using the target large model after the page information list is traversed.
- 10. An electronic device comprising a memory and a processor, the memory having stored thereon a computer program which, when executed by the processor, implements the generating method of any of claims 1-8.
Description
Automatic test code generation method and device and electronic equipment Technical Field The application relates to the technical field of software testing, in particular to a method and a device for generating an automatic test code and electronic equipment. Background During the software project period, a tester needs to test the functions. High-level testers typically write automation frameworks and related business codes manually and then perform more efficient testing and regression tasks by running the automation codes. Because more time is required to clear the business flow before writing, the time for manually debugging and writing codes is long, and the problem of low writing efficiency exists in a manual writing mode. Disclosure of Invention The embodiment of the application provides a method, a device and electronic equipment for generating an automatic test code, and aims to solve the problem of low writing efficiency in the manual writing mode of writing the automatic test code. In a first aspect, a method for generating an automated test code is provided, including: converting a test case described by using natural language into formatted data by using a target large model, wherein the formatted data comprises a page information list, each piece of page information comprises at least one piece of element information, and each piece of element information comprises an original locator and an operation method; Opening a browser through a target automation test framework, traversing a page information list, and accessing a target page indicated by current page information by using the browser; For each item of target element information in the current page information, determining a target locator based on a first verification result of an original locator in the target element information, splicing operation methods in the target locator and the target element information into an operation command, operating on the target page according to the operation command, and writing the operation command into a script file when the operation is successful; After the page information list is traversed, an automatic test code is generated based on the script file and the test cases by using the target large model. In some embodiments, the first verification result is that the original locator is used to locate the unique element on the target page; determining the target locator based on the first verification result of the original locator in the target element information comprises: The original locator is determined to be the target locator. In some embodiments, the first check result is to locate a plurality of elements on the target page using the original locator; determining the target locator based on the first verification result of the original locator in the target element information comprises: sequentially carrying out validity verification on the plurality of elements; And under the condition that only one element in the plurality of elements passes the validity check, adjusting the original locator in the target element information based on the characteristics of the element passing the validity check to obtain the target locator. In some embodiments, each element information further includes an element description and a corresponding test step in the test case; determining the target locator based on the first verification result of the original locator in the target element information, further comprising: If there are a plurality of candidate elements passing the validity check among the plurality of elements, outerHTML of the plurality of candidate elements are acquired; determining a target outerHTML in each outerHTML based on element descriptions and test steps in each outerHTML and target element information using a target large model, and generating a first locator based on the target outerHTML; the target locator is determined based on the second check result for the first locator. In some embodiments, determining the target locator based on the second calibration result for the first locator includes: and under the condition that the second check result is that the first locator is not located to the element on the target page, returning the first locator which fails to be located to the target large model, so that the target large model regenerates the locator until a second locator which is generated by the target large model and can be located to the element is obtained, and determining the target locator based on the second locator. In some embodiments, each element information further comprises an element description and a corresponding test step in the test case, wherein the first check result is that the original locator is used for not locating the element on the target page; determining the target locator based on the first verification result of the original locator in the target element information comprises: Generating a third locator based on the original locator in