Declaring and assigning test variables

You can declare a variable, create a container for it, initialize it to a string or a dataset value, and set its scope. Then, within the test, you can reassign another value to the variable.

About this task

If the data that you want to assign to a variable is only available after a specific test step, instead of initializing the variable, you need to add a variable assignment further down in the test, so that when the assignment occurs, the data that you need to use is available. Otherwise, when you try to initialize the variable (or do the assignment), the value that you want to use will not be available and will not show up as an option to select.

If a variable is initialized at various places such as test, compound test, schedule, or user group, the product uses the following order to initialize the value of the variable when running the test. The variable set in the variable table of the compound test editor takes the highest precedence followed by others:
  1. Compound test setting in the variable table UI
  2. Compound test specified in a var file
  3. User group setting in the variable table UI
  4. User group specified in a var file
  5. Schedule specified setting in the variable table UI
  6. Schedule specified in a var file
  7. Command line
Note: You must select All tests for this user from the Visible in drop-down list to take the precedence of variable initialization.

Procedure

  1. Double-click the test to open it from the Test Navigator view.
    The test opens.
  2. Create a container for the test variables that you create in a test:
    1. Click Test Variables under Test Resources in theTest Contents area.
    2. Click Add > Test Variables Container.
      A container named Test Variables is created for the user-defined variables.
    3. Select the container, and type a new name in the Name field in the Test Variables Details pane.
  3. Declare and define a test variable:
    1. Click the user-defined container to contain the variable.
    2. Select Add > Variable Declaration to create a variable or select Add > Array Variable Declaration to create an array variable.
    3. Type a name for the variable, and then click OK.
      The variable is added as the last element in the container and the Variable Details pane is displayed.
    4. Set the scope and initial value for the variable in the Test Variables Details area:
      Option Description Action
      Visible in This option lists the options to set the scope or visibility for the variable. Select one of the following options:
      • Select This test only to restrict data to the current test only. Even if another test has a variable with the same name, that variable will not change.
      • Select All tests for this user to share the value of this variable when the test runs. For the variable to be shared, both tests must have a variable with the same name and must have this option enabled.
      Check Value This option is displayed only when you select All tests for this user in the previous step. This option allows you to verify whether the variable is initialized at the start of the test or when it is first used. Select one of the following options:
      • Select When first used to check whether or not a variable is initialized only after the test execution reaches the first request that uses a variable.
      • Select At test start to check whether or not a variable is initialized when starting the execution of the test. If the variable is not initialized, then an error message is displayed, depending on the behavior set.
      Initialize to This option lists the options to select the data type and assign a value for the variable. Select one of the following options:
      • Select Text to initialize the variable to a specific value whenever the test runs in the schedule.
      • Select Dataset value, and in the Select Data Source window, select the dataset that will initialize the variable.
      Run-time error if variable not initialized This option lists the possible actions for the run when it encounters an uninitialized test variable. Select one of the following options from the list:
      • Select Do nothing for the test to continue to run.
      • Select Issue test log warning to log the warning message. You must verify that the Test log page in the schedule sets errors, failures, and warnings to All, which is the default setting.
      • Select Issue test log errorto log the error. You must verify that the Test log page in the schedule sets errors, failures, and warnings to All, which is the default setting.
      • Select Exit the test for the schedule to continue to run although the virtual users that have the uninitialized variable stop.
  4. Assign a test variable to a test element and set a value:
    1. Select a test element in the Test Contents area.
    2. Select Insert > Variable Assignment to insert the assignment before the selected element.
      The Test Editor window is displayed with the list of variables available for the test.
    3. Select the variable.
      The variable details are displayed in the Test Assignment Details area.
    4. Set a value for the variable in the Set to box:
      You can select one of the following options:
      • Select Text to initialize the variable to a specific value whenever the test runs.
      • Select Dataset value, and in the Select Data Source window, select the dataset that will initialize the variable.
      • Select Not initialized to not to set any value to the variable.
      • Select UI object property to assign the value of a UI object property to the variable.
      A Set statement is added to the test, with the value you chose.

Results

You have declared a variable and assigned a value to the variable.