Tutorial: Data Driving Test Step Properties from a Data Source Using Event Handler Code

In this tutorial, you will set the input properties from values provided in a data source. You will use event handler code both to access this data source and use the value in the step property. This is useful if you have data sources that you cannot reach from the API testing property grid or are created in a test run-time.

In this tutorial, you will be setting the input properties of a Concatenate Strings step.

Note: We recommend printing this topic or putting the browser window on a second screen before performing this tutorial in a sample API test.

Create a Concatenate Strings step

If necessary, from the String Manipulation section of the Toolbox pane, drag a Concatenate Strings step to the canvas.

Back to top

Add a data source

In order to access values stored in a data source, you must add the data source to your test. For this tutorial, you will add a local table to your test. (You can also add Excel spreadsheets, XML files, or database data sources instead.)

  1. In the Data pane, click the New Data Source button and select Local Table.

  2. In the New Local Table Data Source dialog box, enter Concatenated Strings as the name of the new data source.

  3. Click the Add button . A new row is added to the table.

  4. In the Name column for row 1, rename Column1 to Prefix.

  5. Click the Add button again. Another row is added to the table.

  6. In the Name column row row 2, rename Column2 to Suffix.

  7. Click OK to add the local table to the test.

    A new node is added in the Data pane with the name of your data source (Concatenated Strings), and the table is displayed on the right side of the Data pane.

  8. In the first row under the Prefix column, enter Hello (with a space following it) as the value.

  9. In the first row under the Suffix column, enter World. (with a period at the end).

  10. Click Save .

Back to top

Create an event handler for Concatenate Strings

For this test step, you want to set the value of the input and output properties before the test step runs, so it is important to select the event handler that runs before the test step.

  1. In the Properties pane, select the Events tab .

  2. In the Events tab, in the BeforeExecuteStepEvent row, click the down arrow and select Create a default handler.

    A separate tab, titled TestUserCode.cs, opens in the document pane, and a section of code is added to this file for the BeforeExecuteStepEvent section.

Back to top

Add event handler code

  1. In the TestUserCode.cs tab, find the TODO: Add your code here... section under the ConcatenateStringsActivity<#>_OnBeforeExecuteStepEvent portion of the code.

  2. Delete the //TODO: Add your code here... string.

    Note: Make sure not to delete the brackets on the line above and below the TODO line. These brackets are needed for proper code syntax.

  3. In the same line, enter var. This tells UFT One that your code is creating a variable.

  4. After the var string, enter PrefixProperty. This is the variable name.

  5. After Prefix Property, enter an equals sign (=) character. This instructs UFT One that your code is providing the value for the PrefixProperty variable.

  6. After the equals sign, enter GetDataSource("Concatenated Strings"). This method enables you to access the local table that you created previously.

  7. After the GetDataSource method, enter a period (.) character. UFT One displays an autocomplete list.

  8. From the autocomplete list, select the GetValue method and press ENTER:

  9. After the GetValue method, add a open parenthesis ( character. Note that the text of the GetValue function changes color.

  10. After the parenthesis, enter 1 to specify which row in the local table to access, followed by a comma character (,).

  11. After the comma, enter "Prefix" (with quotes). This is the name of the column in which the value you want to access is located.

  12. After the "Prefix" string, enter a close parenthesis ) character and a period (.) character. UFT One displays another autocomplete list.

  13. From the autocomplete list, select the ToString function and press ENTER:

  14. After the ToString function, add an open and close parenthesis () and a semicolon character (;).

    Your event handler code should now look like this:

    var PrefixProperty = GetDataSource("Concatenated Strings").GetValue(1, "Prefix).ToString();
  15. Click Save .

Back to top

Add event handler code for Prefix property

  1. Under the var PrefixProperty line, enter the this. object (with the period). UFT One displays an autocomplete list.

  2. From the autocomplete list, select the ConcatenateStringsActivity<#> variable and press ENTER:

    Note: The number of your activity differs between UFT One sessions, depending on what other activities you have previously added to the canvas.

  3. After the ConcatenateStringsActivity<#> variable, type a period (.) character. UFT One displays another autocomplete list.

  4. From the autocomplete list, select the Prefix object and press ENTER:

  5. After the Prefix object, enter = PrefixProperty. This assigns the value of the Prefix property from the Concatenate Strings activity to the value you retrieved from the local table.

  6. After the Prefix Property string, enter a semicolon (;) character.

    Your event handler code should now look like this:

    var PrefixProperty = GetDataSource("ConcatenatedStrings").GetValue(1, "Prefix").ToString();
    this.ConcatenateStringsActivity4.Prefix = PrefixProperty;
  7. Click Save .

Back to top

Repeat the process described in steps 4 and 5

  1. Use the same process described above to set the Suffix property from the Suffix column of the local table.

    Make sure to assign the following things:

    • Variable name: Suffix Property

    • Column in the data table (used in the GetValue method): "Suffix"

  2. Click Save .

Back to top

Run the test

  1. In the toolbar, click the Run button .

  2. In the Run dialog box, click Run. UFT One runs the test, using the property values provided and as retrieved from the local data table.

    After the test run is complete, you can see the results to ensure that the event handler code provided the property values correctly. The property values used are listed in the run results with the step's captured data:

Back to top