Updated on 2024-04-25 GMT+08:00

Writing an API Automation Script

Background

An API automation test case consists of 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 affecting other tests or the next test, you are advised to clear the test environment data, restore the initial status of the test environment, and destroy the data created in the preparation stage after each test.
    • 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

  1. After the operations described in Creating an API Automation Test Case are complete, choose Testing Case > Auto API Test and click the name of the test case to be edited.
  2. Choose Scripting > 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.

  3. 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, the address will be automatically generated to the request.
  4. (Optional) Repeat 2 to 3 to add pre-steps and post-steps.
  5. After the editing is complete, click Save in the upper right corner of the page to save the script.
  • API automation 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 built-in functions, see Built-in Functions.
  • When editing API automation test cases, you can right-click the title area of a test step to cut, copy, paste, and delete a test step. If there are multiple test steps, you can use the "Ctrl+left click" combination to select multiple test steps and right-click them in batches. After a test step is cut or copied, the test step can be pasted on the current tab page, across tab pages, or across test cases.

  • Note that the right-click response in the title area is the operation on the test step. Therefore, when you edit the text box in the title area of the test step, the shortcut menu of the browser is overwritten by the shortcut menu of the system, and the shortcut menu of the browser does not take effect. To copy and paste text in the text box of the test step title area, press Ctrl+C and Ctrl+V.