Tutorial: Importing a Data Source to your Test Using Event Handler Code

In this tutorial, you will import a data source using event handler code instead of adding the data source in the Data pane. This is useful if your data source is generated dynamically during the test run or is unreachable through the API testing property grid.

In this tutorial, you will be importing a sample Excel file that provides values for the input properties of a Web service 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.

Prerequisite - Import the Web service to your test.

For this tutorial, you are going to use the Web service provided with the sample flight reservation application in the UFT One installation.

  1. In the Start Menu, open the API Flights application (Start > All Programs > Micro Focus > Micro Focus UFT One > Sample Applications > Flight API). The Flights API window opens.
  2. In the UFT One toolbar, click the Import WSDL button .
  3. In the Import WSDL from URL or UDDI dialog box, enter the following URL: http://localhost:8000/Flights_SOAP?WSDL.
  4. Click OK to import the Web service model to your test.

    The Web service model's methods are added to the Toolbox under the Local Activities section.

Back to top

Prerequisite - add a GetFlights step to your test.

From the Local Activities section of the Toolbox pane, expand the Micro Focus Flights_Service and FlightServiceMethods nodes, and drag a GetFlights step to the canvas.

Back to top

Create an event handler for the GetFlights step.

For this test step, you want to import the data source before the test step runs, so that the test step's properties can access the values stored in the data source. Therefore, 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 to import the data source.

  1. In the TestUserCode.cs tab, find the TODO: Add your code here... section under the StServiceCallActivity<#>_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 ExcelFileImportInputArgs. (While you type, UFT One displays an autocomplete list. You can use this list if you want to find the ExcelFileImportInputArgs class.)
  4. After the ExcelFileImportInputArgs class, enter WebServicePropertyData. This provides the name for the class that you will call in another line.
  5. After the Web Service Property Data string, enter an equals sign (=) character. This instructs UFT One that the next information entered provides the value for the ExcelFileImportInputArgs class.
  6. After the equals sign, enter new and enter a space. After you enter the space, UFT One displays an autocomplete list.
  7. From the autocomplete list, select the ExcelFileImportInputArgs class and press ENTER:

  8. After the ExcelFileImportInputArgs class, add a open parenthesis ( character. Note that the text of the class changes color.
  9. After the parenthesis, enter the path to the Sample Excel file provided with the UFT One installation: "C:/Program Files (x86)/Micro Focus/UFT One/samples/Flights Application/SampleAppData.xlsx" (with quotes)
  10. After the path to the data source, enter a comma character (,).
  11. After the comma, enter the name of the Excel file after it is imported: "Web Service Property Values" (with quotes).
  12. After the Excel file import name, add an additional comma (,) character.
  13. After the comma, enter true. (This parameter tells UFT One that the Excel file has a header row.)
  14. After the true string, enter a close parenthesis ) character and a semicolon (;) character.

    Your event handler code should now look like this:

    ExcelFileImportInputArgs WebServicePropertyData = new ExcelFileImportInputArgs("C:/Program Files (x86)/Micro Focus/UFT One/samples/Flights Application/SampleAppData.xlsx","Web Service Property Values", true);
  15. On the next line, enter the GetDataSource method.
  16. After the GetDataSource method, add an open parenthesis ( character. Note that the color of the GetDataSource method changes.
  17. After the parenthesis, enter the name of the data source you provided above: "Web Service Property Values" (with quotes).
  18. After the data source name, add a close parenthesis ) character, followed by a period (.) character.
  19. Enter the Import method, followed by an open parenthesis character (.
  20. After the open parenthesis, enter the name of the ExcelFileImportInputArgs class you assigned earlier: WebServicePropertyData.
  21. After the class name, add a close parenthesis character ) and a semicolon (;) character.

    Your event handler code should now look like this:

    ExcelFileImportInputArgs WebServicePropertyData = new ExcelFileImportInputArgs("C:/Program Files (x86)/Micro Focus/UFT One/samples/Flights Application/SampleAppData.xlsx","Web Service Property Values", true);
    GetDataSource("Web Service Property Values").Import(WebServicePropertyData);
  22. Click Save .

Back to top