Manipulate data programmatically

Relevant for: API testing only

Using code, you can retrieve or set test step property/parameter values, import or export property/parameter values, or data-drive the property/parameter values of your test steps.

Before you start, you must add at least one data source to your test and save the test.

Retrieve a value from a data source

In order to get a value from a data source, you must use the GetDataSource and GetValue methods:

  1. Select the step for which you want to retrieve a data source 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:

    GetDataSource("<data source name>").GetValue(<row index>, "<column name>");

    Note: When entering the parameters for the GetValue function, your row index is based on 0.

  5. (Optional) If you want to retrieve the value corresponding to the value in the current iteration, you replace the <row index> with the CurrentIterationNumber property, using the following syntax:

    GetDataSource(<data source name>).GetValue(<this.Loop<#>.CurrentIterationNumber, "<column name>");

    Notes:

    • When running a data-driven test, UFT One runs one iteration for each row in the data source. Therefore, the loop number you enter corresponds to the row of the data source, unless you specify a different starting row in the Data Source navigation policies.

    • The CurrentIterationNumber is one-based, meaning that the number entered for the current loop must be 1 or higher.

Back to top

Set a property value from a data source

You can use the SetValue method to insert a property value in a data source. This enables you to populate a data source with manually-entered values or values taken from another source.

  1. Select the step for which you want to set a data source 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 enter using the following syntax:

    GetDataSource("<data source name>").SetValue(<row index>, "<column name>", "<value to enter>");

Back to top

Import a data source file to your test

You can import data to your test using the Import and ImportFromExcelFile (if you are importing an Excel file) methods. This enables you to add data in runtime and populate your property values with this data.

  1. Select the step to which you want to import data values

  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 enter using the following syntax:

    ExcelFileImportInputArgs <name> = new ExcelFileImportInputArgs(@"<path to data source>", "<data source name>", <boolean whether there is a header>);
    GetDataSource("<data source name>").Import(<name>);

    or

    GetDataSource("<data source name>").ImportFromExcelFile(@"<path to data source>", "<data source name>", <boolean whether there is a header>);

Back to top

Export the property values to a file

You can also export the data from a test step to an external file using the Export and ExportToExcelFile methods. This enables you to export values to a file that other test steps can access to provide values for their properties/parameters in runtime.

  1. Select the step for which you want to export its data values

  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 enter using the following syntax:

    ExcelFileExportInputArgs <name> = new ExcelFileExportInputArgs(@"<path to file>");
    GetDataSource("<data source name>").Export(<name>);

    or

    GetDataSource("<data source name>").ExporttoExcelFile(@"<path to file>");

Back to top

Data drive test step property/parameter values

You can also data drive test step property/parameter values using code. This is useful in custom scenarios where you cannot link to your data source with the user interface options or you need to link to a data source created in the test runtime.

  1. Link the Test Flow/test loop with the data source. For details, see Add a data source to the Test Flow or test loop.

  2. Set the Data Navigation policy for the data source. For details, see Navigate within a data source.

  3. In the canvas, select the step to data drive.

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

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

    Tip: If you are populating values for the currently selected test step, use the BeforeExecuteEvent step. This ensures that the properties/parameters are mapped to the appropriate data source values before the step is run.

  6. Connect your property value to the data source using the following syntax:

    • For test step properties are not based on an array:

      var <variable name> = GetDataSource("<data source>").GetValue(<row index>, "<column name>").ToString();
      <activity name>.<property name> = <variable name>;
    • For test step properties based on an array:

      var <variable name> = GetDataSource("<data source>").GetValue(<row index>, "<column name>").ToString();
      <activity name>.InputEnvelope.SelectSingleNode("<fully qualified xpath to property value>").InnerText = <variable name>;

    Notes:

    • You can retrieve the XPath to a property name stored in an array by right-clicking the Property name in the Input/Checkpoints tab and selecting Copy Fully Qualified XPath.

    • If you want to set the value of an output value or checkpoint stored in an array, change the InputEnvelope to OutputEnvelope in the syntax displayed above.

    • If you are running multiple iterations using data-driving, you can use the this.Loop<#>.CurrentIterationValue property as the row index. However, since the CurrentIterationProperty is one-based, but the row-index is zero-based, add a -1 to the CurrentIterationProperty to ensure that your last iteration does not fail.

Back to top

See also: