Writing an API Automation Script
Background
An API automation test case can include three stages: preparation, testing, and destruction.
The preparation stage corresponds to the Pre-steps tab page to prepare for the test prerequisites. The testing stage corresponds to the Test Procedure tab page to implement the API function test. The destruction stage corresponds to the Post-steps tab page to release or restore the test data in the preparation and testing stages.
- (Optional) Preparation stage
- In this stage, prepare the prerequisite data required in the testing stage. If there is no prerequisite, skip this stage.
- In this stage, the prerequisites are initialized using API calls. If the data of the prerequisites needs to be referenced in the testing stage, you can use parameter passing to parameterize the data. For details, see Setting Response Extraction.
- Testing stage
Define the API core test steps. The test steps in the testing stage can reference the parameters extracted in the preparation stage.
- (Optional) Destruction stage
- To avoid impacting other tests or the following test, clear the test environment data after each test, reset the test environment, and delete the data created in the preparation stage.
- If no data needs to be destroyed, ignore this stage. You can destroy data using API calls. The test steps in the destruction stage can reference the parameters extracted in the preparation stage.
Procedure
- Create API automation test cases by referring to Creating an API Automation Test Case, choose , and click the name of the test case to be edited.
- Click the Scripting tab. On the bottom of the page, click URL Request to generate a test step.
If the Swagger description file of the tested API is available, you can import it to generate a script template, based on which you can orchestrate test cases. For details, see API Keywords.
Select a script template and drag and drop the script template card or click
on the card to add the script to the Test Procedure tab page. - Edit the URL request as required by referring to Setting an API Request, Setting a Test Checkpoint, and Setting Response Extraction. Enter a domain name or IP address as the environment address in the request. If you import a Swagger or Postman file, the address will be automatically generated to the request.
- (Optional) Repeat 2 to 3 to add pre-steps and post-steps.
- Click Save in the upper right corner of the page to save the script.
- Automated API test cases support the use of built-in functions in the request URL path, request header, request body, checkpoint parameters, and URL response. For details about the built-in functions, see Built-in Functions.
- To cut, copy, paste, or delete one or more test steps, right-click the title area of a test step, or press Ctrl and left-click them. Once cut or copied, test steps can be pasted within the current tab, across different tabs, or to other test cases.

- Note that right-clicking in the title area applies to the test step. When you edit the text box there, the system's right-click menu overwrites the browser's, disabling the browser's shortcuts. To copy and paste text, press Ctrl+C and Ctrl+V.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot