Tutorial: Setting Web Service Call Property Values Using Event Handler Code

In this tutorial, you will set the input properties of a Web service call step using event handler code instead of the property value grid. This is useful if you have property values that are generated dynamically during a test run, as you cannot use the Input/Checkpoint Properties tab in the Properties pane to set a property that does not exist before the test run.

Note: Setting input, output, and checkpoint properties for a REST Service or WADL step is done in the same way as a standard activity. To learn how to set these properties, see Tutorial: Setting Test Step Input Properties Using Event Handler Code and Tutorial: Setting Test Step Checkpoint Properties Using Event Handler Code.

In this tutorial, you will be setting the input value properties for the GetFlights method included with the sample application API.

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

Import the Web service

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

  1. Open the Flights API application (available from the Windows Start menu).

  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

Add a GetFlights step

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

Back to top

Create an event handler for GetFlights

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 OnBeforeExecuteStep section.

Back to top

Add event handler code

  1. In the TestUserCode.cs tab, find the TODO://Add your code here... section under the StServiceCallActivity<#>_OnAfterExecuteStep 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 this. (with the period). (While you type, UFT One displays an autocomplete list. You can use this list if you want find the this object.)

    After you type the this. object, UFT One displays another autocomplete list.

  4. From the autocomplete list, select the StServiceCallActivity<#> 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.

  5. After the StServiceCallActivity<#> variable, type a period (.) character. Another autocomplete list is displayed.

  6. From the autocomplete list, select the InputEnvelope object and press ENTER:

  7. After the InputEnvelope object, type a period (.) character. Another autocomplete list is displayed.
  8. From the autocomplete list, select the SelectSingleNode method and press ENTER:

  9. After the SelectSingleNode method, enter a open parenthesis ( character. Note that the color of the SelectSingleNode method changes.

  10. In the canvas, select the GetFlights step.

  11. In the Properties pane, open the Input/Checkpoints tab .

  12. In the Value cell of the Departure City row, click the triangle at the right side of the cell until it is green.

  13. In the Value cell, right click and select Copy Fully Qualified XPath.

  14. In the TestUserCode.cs file, after the open parenthesis that you entered after the SelectSingleNode method, paste the fully qualified XPath you just copied (with quotes before and after the XPath expression).

  15. After the XPath expression, enter a close parenthesis ) character and a period (.)character. Another autocomplete list is displayed.

  16. From the autocomplete list, select the InnerText object and press ENTER:

  17. After the InnerText method, add an equals (=) character.

  18. After the equals character, enter "Denver" (with quotes). This sets Denver as the value for the DepartureCity property of the step.

  19. After "Denver", type a semicolon (;) character.

    Your event handler code should now look like this:

    this.StServiceCallActivity4.InputEnvelope.SelectSingleNode("/*[local-name(.)='Envelope'][1]/*[local-name(.)='Body'][1]/*[local-name(.)='GetFlights'][1]/*[local-name(.)='DepartureCity'][1]").InnerText="Denver";
  20. Click Save .

Back to top

Repeat the process

Use the same process described above to set the ArrivalCity property to be "Los Angeles".

After you enter the ArrivalCity property, your event handler code should look like this:

this.StServiceCallActivity4.InputEnvelope.SelectSingleNode("/*[local-name(.)='Envelope'][1]/*[local-name(.)='Bthis.ody'][1]/*[local-name(.)='GetFlights'][1]/*[local-name(.)='DepartureCity'][1]").InnerText="Denver";
this.StServiceCallActivity4.InputEnvelope.SelectSingleNode("/*[local-name(.)='Envelope'][1]/*[local-name(.)='Body'][1]/*[local-name(.)='GetFlights'][1]/*[local-name(.)='ArrivalCity'][1]").InnerText="LosAngeles";

Back to top

Run the test

  1. In the toolbar, click the Run button .

  2. In the Run dialog box, click RunUFT One runs the test, using the property values provided in your event handler code as the input property values for the test step.

    After the test run is complete, you can see the results to ensure that the event handler code provided the property values correctly:

    Output pane

    In the User Logger section of the output pane, you can see the output log. In the log, there is an entry for the Departure City and Arrival City values:

    Run results

    In the run results, if you look in the captured data for the step, you can see the values used:

If you want to set checkpoint properties, the process is slightly different:

  • You cannot set the value of existing checkpoints (which are defined in your WSDL file that is imported).

  • You can add additional checkpoints to the test run. To add these checkpoints, you do a couple of different things:

Back to top