Updated on 2024-10-11 GMT+08:00

Setting Environment Parameters of an API Script

During automated testing, there are multiple test environments whose parameter values are different, for example, the domain name and account. These parameters are often used in test scripts. If they are bound to test scripts, the scripts will become highly redundant and hard to be reused.

To solve the preceding problems, you can use environment parameters to manage them in a unified manner. In test scripts, environment parameters are referenced in parameterization mode. During execution, you only need to select an execution environment to use the corresponding environment parameter values to complete testing.

Application Scope

The parameters, checkpoints, variables, and URLs of the test steps in each test case of the current project can reference environment parameters.

Reference Format

The reference format of an environment parameter is $${Parameter name}. For example, if the parameter name is hostname, you can use $${hostname} to reference the parameter.

You can configure and manage environment parameters by group as required. For example, the value of hostname in the quasi-production environment is stage.example.com, and the value in the production environment is prod.example.com. The test script uses $${hostname} to reference this parameter. During the test, select different environments to execute the test so that a set of automated API test cases can be reused in all environments.

Procedure

  1. Go to the Scripting tab page and click .

  2. Click New Variable, enter parameter information, and click OK.

    Configuration Item

    Description

    Name

    Parameter name.

    Type

    Parameter type. Multiple types are supported, such as text, random string, random integer, and timestamp.

    Value

    Parameter value.

    Description

    Parameter description.

    Sensitive

    This service encrypts sensitive parameter values during storage and overwrites them with asterisks (*) in test result logs. Sensitive data includes but is not limited to personal and authentication information, such as names, addresses, and usernames.

    Dynamic

    Dynamic parameter setting. The value of a dynamic parameter can be assigned during test case execution. The initial value of a dynamic parameter can be empty. After a value is assigned, the latest value is displayed.

    After you set the value of the Assign Value to Dynamic Environment Parameter column in the Extract Response tab page of the test step, the extracted value is assigned to the dynamic parameter during test execution. For details, see Dynamic Variable Description.

  3. Change the default environment.

    To set another environment as the default environment, click in the upper right corner of the environment card and select Set as Default.

Built-in Functions

  1. You can use built-in functions to configure environment parameters. Note that nested built-in functions are output as they are.
  2. For details about the supported built-in functions, see Table 1.
    Table 1 Supported built-in functions

    No.

    Function

    Description

    1

    $random_string(intA)

    Generate a random string of a specified length

    2

    $random_int(intA, intB)

    Generate a random number in a specified range

    3

    $timestamp()

    Obtain the current timestamp

    4

    $timestamp_format(String A, String B)

    Convert a timestamp into a date

    5

    $uuid()

    Generate a UUID

    6

    $encode_base64(StringA)

    Generate Base64 encoding

    7

    $md5(StringA)

    Generate an MD5 hash value

    8

    $sha512(StringA)

    Generate SHA512 encoding

  3. Examples:
    • The value of $random_string(intA) is output normally.

    • Built-in functions that concatenate strings are parsed as strings, and their output values are strings as they are. For example, the output values of functions aa$random_string(10) and $random_string(10)aa are aa$random_string(10) and $random_string(10)aa.

    • If two parameters are passed while only one parameter is required, for example, $random_string (1,1), an exception will be thrown.

      If the input and required parameter types do not match, for example, $random_string(fff) or $random_string(), an exception will be thrown.

Synchronization

Right-click the parameter to be synchronized and choose Synchronize to other environments from the shortcut menu to synchronize the current parameter to all environments.

Dynamic Variable Description

Multiple test cases in a test suite may have context relationships. The test case that is executed later depends on the result returned by the API in the test case that is executed earlier. For example, all APIs require authentication information, which has a validity period. If it is obtained in each test case, the test procedure of the test case will be redundant and difficult to maintain.

This problem can be avoided by using dynamic global variables. In the first executed test case, the authentication information is obtained and then assigned to the dynamic global variables. In the subsequent executed test cases, the dynamic global variables can be directly used, avoiding obtaining authentication information repeatedly.

  1. Set dynamic variables.

    1. On the test case list of the Auto API Test tab page, click More on the right of the page and choose Environment Parameters.
    2. Select the check box in the Dynamic column and click Save to set a global variable to a dynamic variable.

  2. Assign values to dynamic variables.

    1. In an automated API test case, click the Extract Response tab of the test step, and click the Add button in the Advanced Extraction Type column.

    2. Select a value from the Assign Value to Dynamic Environment Parameter drop-down list box.

  3. Use a dynamic variable.

    • Use the dynamic variable in a test case.

      Reference the dynamic global variable in a test case as required. For details, see Reference Format.

    • Use a dynamic global variable in a test suite.

      Add the test cases in 2 and 3 to a test suite in sequence, and select Serial. In this way, the latest value assigned to the dynamic global variable can be used during the execution of related test cases.

      You are not advised to use dynamic global variables during parallel execution because the value assigning and sequence of the dynamic variables are uncertain.