Retrieve and set test or user variables

Relevant for: API testing only

Prerequisite - create user variables.

See Define user variables for details on creating user variables.

Note: You may also want to create multiple test profiles to vary the value of the variables for different users or different test runs. For details on creating and editing user profiles, see Define user variable profiles

Back to top

Optional - set the test profile

If you are using multiple test profiles, you must set the test profile you want to use before running test:

  1. In the canvas, select either the Start or End steps.

  2. In the Properties pane, open the Test Variables tab .

  3. In the Test Variables tab, choose the profile name from the Active Profile drop-down list.

If you have entered default values for any test or user variables, the values for this profile are used in the test run.

Back to top

Retrieve a variable value

You can use code to retrieve the value of a user variable during run-time. This is useful to show you the value of the variable without having to start a debugging session.

  1. In the canvas, select the step during which you want to retrieve a variable value.

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, call the data source value you want to retrieve using the following syntax:

    this.<activity name>.Context.TestProfile.GetVariableValue("<variable name>");

    or

    this.<activity name>.Context.EnvironmentProfile.GetVariableValue("<variable name>");

For example:

this.ConcatenateStringsActivity4.Context.TestProfile.GetVariableValue("Prefix");
this.ConcatenateStringsActivity4.Report("Prefix Variable Value", this.ConcatenateStringsActivity4.Context.TestProfile.GetVariableValue("Prefix"))

Back to top

Set a variable value

  1. In the canvas, select the step during which you want to retrieve a variable value.

  2. In the Properties pane, open the Events tab .

  3. In the Events tab, create an event handler. The TestUserCode.cs file opens.

  4. In the TODO section of the TestUserCode.cs file, call the data source value you want to retrieve using the following syntax:

    this.<activity name>.Context.TestProfile.SetVariableValue("<variable name>","<variable value>");

    or

    this.<activity name>.Context.EnvironmentProfile.SetVariableValue("<variable name>","<variable value>");

For example:

  • The following example retrieves the value of the TestName test variable:

    string testName = this.StServiceCallActivity4.Context.TestProfile.GetVariableValue("TestName");
  • The following example sets the value of the environment variable beforeExecuteStepevent used to verify that the BeforeExecuteStepEvent event ran in the test step for the activity activity.

    activity.Context.EnvironmentProfile.SetVariableValue("beforeExecuteStepEvent","true");

Back to top